servlet中:
String servletPath = request.getServletPath();
String methodName = servletPath.substring(1);
methodName = methodName.substring(0, methodName.length() - 3);
Method method;
try {
method = getClass().getDeclaredMethod(methodName,
HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request,response);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("update");
}
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> list = dao.getAll();
request.setAttribute("list", list);
request.getRequestDispatcher("/index.jsp").forward(request, response);;
System.out.println("query");
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("delete");
}private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("add");
}
web.xml文件:
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
jsp:
<a href="add.do">add</a>
<a href="delete.do">delete</a>
<a href="query.do">query</a>
<a href="update.do">update</a>