一、问题:针对某一个模块,需要编写多个servlet
如用户的增删改查、登录、注册:
FindServlet
LoginServlet
RegisterServlet
...............
二、解决:一个模块对应一个Servlet处理,并实现不同业务逻辑
2.1、每个模块定义一个Servlet,比如用户模块对应的UserServlet
2.2、UserServlet是用来处理用户模块的所有请求,那么就要区分不同请求,通过请求参数区分
<a th:href="@{/UserServlet(method='1ogin')}">登录请求</a>
2.3、如果所有的请求处理都放在doPost或doGet里面
(1)如果请求过多,会造成doGet和doPost代码量特别大,那么把不同的请求代码放到指定的方法,实现代码的分隔;
(2)如果请求过多,那么需要大量的if判定
(a) 根据方法名调用对应的方法【反射机制】
(b) 传递过来的请求参数必须和方法名一致。
2.4、每个Servlet都需要用到通过反射机制调用对应的方法这套业务,可以提取公共的父类【ModelServlet】,让ModelServlet继承HttpServle,再把doGet和doPost、根据方法名,通过反射机制调用对应的方法这套业务。之后创建Servlet,就不要继承HttpServlet,继承ModelServlet
三、代码实现
welcome.html
html
<!DOCTYPE html>
<!--导入thymeleaf包-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>用户模块</h3>
<a th:href="@{/userServlet(method='login')}">登录</a><p/>
<a th:href="@{/userServlet(method='register')}">注册</a><p/>
<a th:href="@{/userServlet(method='logout')}">注销</a><p/>
<a th:href="@{/userServlet(method='update')}">更新</a><p/>
<a th:href="@{/userServlet(method='delete')}">删除</a><p/>
</body>
</html>
WelcomeServlet.java
java
package org.hlx.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.hlx.thymeleaf.CustomTemplateEngine;
import java.io.IOException;
import java.math.BigDecimal;
@WebServlet("/welcome.html")
public class WelcomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取模板引擎对象
CustomTemplateEngine templateEngine = CustomTemplateEngine.getInstance(req);
// 处理请求并响应结果
templateEngine.processTemplate("welcome", req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
ModelServlet.java
java
package org.hlx.servlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
/**
* @author : HLX
* @ClassName :ModelServlet
* @date : 2026/1/13 15:02
* @Version :1.0
* @Description: TODO
* @modyified By :
*/
public class ModelServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws jakarta.servlet.ServletException, java.io.IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws jakarta.servlet.ServletException, java.io.IOException {
//获取请求的参数
String method = req.getParameter("method");
try {
//获取Method对象
Method methodObject = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
//暴力破解
methodObject.setAccessible(true);
//执行方法
methodObject.invoke(this, req, resp);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
UserServlet.java
java
package org.hlx.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author : HLX
* @ClassName :UserSevlet
* @date : 2026/1/13 14:55
* @Version :1.0
* @Description: TODO
* @modyified By :
*/
@WebServlet("/userServlet")
public class UserServlet extends ModelServlet {
public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行login方法...");
}
public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行register方法...");
}
public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行logout方法...");
}
public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行update方法...");
}
public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行delete方法...");
}
}
四、运行效果
http://localhost:8080/ThymeleafServlet_01/welcome.html

