后端代码:
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 method = request.getParameter("method");
String method2 = request.getMethod();
//System.out.println(method);
System.out.println(method2);
if(method2.equals("GET")){
System.out.println(111);
String id = request.getParameter("id");
System.out.println("id:"+id);
}
else if(method2.equals("POST")){
String jsonString = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
System.out.println(jsonString);
System.out.println("这里2");
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println("这里1");
String methodName = jsonObject.getString("method");
System.out.println(2212);
System.out.println("这是:"+methodName);
Login_rigister_control lc = new Login_rigister_control();
Method[] methods = lc.getClass().getDeclaredMethods();
Method methodToInvoke = null;
for (Method method : methods) {
DynamicMethod annotation = method.getAnnotation(DynamicMethod.class);
if (annotation != null && annotation.value().equals(methodName)) {
methodToInvoke = method;
break;
}
}
if (methodToInvoke != null) {
try {
methodToInvoke.invoke(lc, request, response);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
} else {
// 如果找不到对应的方法,处理异常情况
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.getWriter().println("Method not found");
}
}
}
}
报错:
12-Jul-2024 15:48:25.836 严重 [http-nio-8080-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke 在路径为[/java_web_final2_war_exploded]的上下文中,Servlet[servlet.controller.Login_rigister_control]的Servlet.service()引发了具有根本原因的异常[Servlet执行抛出一个异常]
java.lang.ClassNotFoundException: org.json.JSONObject
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1014)
at servlet.BaseServlet.Login_rigister_BaseServlet.service(Login_rigister_BaseServlet.java:38)
我的依赖正确填写了:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>