目录
介绍
传统的Servlet类只能处理前端传来的一个请求,要想一个servlet类要获取多个请求,则在UserServlet类上加入@WebServlet("/user/*"),接收某一所要操作的一类的所有请求。因为每一个请求路径不同,所以可以获取其路径不同之处,写出对应的方法,最后通过反射,将不同对象的不同请求执行对应的方法。
项目结构
BaseServlet
注意:项目虚拟路径为/Module01
package com.xc;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("get启动!");
//获取URI路径
String uri = request.getRequestURI();
//把"/Module01/indexServlet/"删除,并追加”Method“
String methodName = new StringBuffer(uri.substring(uri.lastIndexOf("/") + 1)).append("Method").toString();
//根据方法名获取对应的方法对象,最后执行方法
Method method = null;
try {
//哪一个对象调用doGet方法,则this代表哪个对象
method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request, response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post启动!");
this.doGet(req, resp);
}
}
UserServlet
package com.xc;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/user/*") //接收所有/Module01/indexServlet/下的请求
public class UserServlet extends BaseServlet {
public void addMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("用户...addMethod启动");
}
public void deleteMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("用户...deleteMethod启动");
}
public void updateMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("用户...updateMethod启动");
}
public void selectMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("用户...selectMethod启动");
}
}
GoodsServlet
package com.xc;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet {
public void addMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("商品...addMethod启动");
}
public void deleteMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("商品...deleteMethod启动");
}
public void updateMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("商品...updateMethod启动");
}
public void selectMethod(HttpServletRequest request, HttpServletResponse response) {
System.out.println("商品...selectMethod启动");
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>反射测试</title>
</head>
<body>
用户操作:
<a href="${pageContext.request.contextPath}/user/add">添加</a>
<a href="${pageContext.request.contextPath}/user/delete">删除</a>
<a href="${pageContext.request.contextPath}/user/update">修改</a>
<a href="${pageContext.request.contextPath}/user/select">查询</a>
<br>
<br>
商品操作:
<a href="${pageContext.request.contextPath}/goods/add">添加</a>
<a href="${pageContext.request.contextPath}/goods/delete">删除</a>
<a href="${pageContext.request.contextPath}/goods/update">修改</a>
<a href="${pageContext.request.contextPath}/goods/select">查询</a>
</body>
</html>
实现:
点击 用户操作的添加和商品操作的删除