CustomerServlet
package com.aff.mvcapp.servlet; import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/customerServlet") public class CustomerServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1. 获取ServletPath: /edit.do 或 addCustomer.do String servletPath = request.getServletPath(); // 2.去除 / 和 .do 得到类似于 edit 或 addCustomer 这样的字符串 String methodName = servletPath.substring(1); methodName = methodName.substring(0, methodName.length() - 3); try { // 3.利用反射获取 methodName 对应的方法 Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); // 4.利用反射调用对应的方法 method.invoke(this, request, response); } catch (Exception e) { //e.printStackTrace(); //可以有一些响应 response.sendRedirect("error.jsp"); } } private void edit(HttpServletRequest request, HttpServletResponse response) { System.out.println("edit"); } private void update(HttpServletRequest request, HttpServletResponse response) { System.out.println("update"); } private void query(HttpServletRequest request, HttpServletResponse response) { System.out.println("query"); } private void delete(HttpServletRequest request, HttpServletResponse response) { System.out.println("delete"); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>CustomerServlet</servlet-name> <servlet-class>com.aff.mvcapp.servlet.CustomerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CustomerServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="addCustomer.do">Add</a> <br><br> <a href="query.do">Query</a> <br><br> <a href="delete.do">Delete</a> <br><br> <a href="edit.do">Edit</a> <br><br> </body> </html>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="addCustomer.do">Add</a> <br><br> <a href="query.do">Query</a> <br><br> <a href="delete.do">Delete</a> <br><br> <a href="edit.do">Edit</a> <br><br> </body> </html>
response.sendRedirect("error.jsp");
目录