public class Login_rigister_BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String method2 = request.getMethod();
System.out.println(method2);
String method=null;
Login_rigister_control lc = new Login_rigister_control();
if(method2.equals("GET")){
method = request.getParameter("method");
System.out.println("id:"+method);
}
else if(method2.equals("POST")){
/*String jsonString = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
System.out.println(jsonString);*/
method = request.getParameter("method");
System.out.println(method);
}
Class Class = lc.getClass();
try {
System.out.println("这里执行了吗");
Method method1 = Class.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
method1.setAccessible(true);
method1.invoke(lc,request,response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
@WebServlet("/demo5")
public class Login_rigister_control extends Login_rigister_BaseServlet {
public void getlogin(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
String method = request.getParameter("method");
System.out.println(method);
System.out.println(uname);
System.out.println(pwd);
System.out.println(3434);
Login_rigister lr = new Login_rigister();
System.out.println(123);
Class Class = lr.getClass();
Method m = Class.getMethod(method, String.class, String.class);
boolean glogin = (boolean)m.invoke(lr, uname, pwd);
System.out.println(glogin);
boolean getlogin = lr.getlogin(uname, pwd);
System.out.println(234);
if (getlogin==false){
response.getWriter().write("密码错误!");
}
else{
response.getWriter().write("登录成功!");
}
/*String jsonString = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
System.out.println(jsonString);*/
}
public class Login_rigister {
public boolean getlogin(String uname,String pwd) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行sql(可以在这里设置自动提交事务)
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取UserMapper接口的代理对象
Account account = sqlSession.getMapper(Account.class);
boolean q = account.getlogin("1234", "qqqq");
System.out.println(q);
return q;
}
}
我的程序死活会卡在这里:
boolean glogin = (boolean)m.invoke(lr, uname, pwd);
报错信息:
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at servlet.BaseServlet.Login_rigister_BaseServlet.service(Login_rigister_BaseServlet.java:42)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:716)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:130)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:109)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:663)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:387)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:904)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1746)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1148)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
at java.base/java.lang.Thread.run(Thread.java:842)
Caused by: java.lang.NoClassDefFoundError: org/apache/ibatis/io/Resources
at servlet.controller.Login_rigister_control.getlogin(Login_rigister_control.java:41)
... 26 more
Caused by: java.lang.ClassNotFoundException: org.apache.ibatis.io.Resources
... 27 more
我的maven也正确配置了:
输出卡在了这里:
我的Login_rigister如果改成主函数然后自己给值就可以用,我在Login_rigister_control怎么调用都没用