Servlet 动态注册

This commit is contained in:
2026-10-21 15:33:11 +08:00
parent c2f73296c6
commit ad6bfee31e
6 changed files with 90 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.linlangjz</groupId>
<artifactId>SpringStudy</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Spring-ch09</artifactId>
<packaging>war</packaging>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,25 @@
package com.ycjy;
import com.ycjy.servlet.FirstServlet;
import com.ycjy.servlet.SecondServlet;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRegistration;
import java.util.Set;
public class MyInit implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> set, ServletContext ctx) throws ServletException {
// new Servlet, 一般情况下老师都教不能new, 但是特殊情况下可以
FirstServlet fs = new FirstServlet();
SecondServlet ss = new SecondServlet();
// 注册并映射动态代理
ServletRegistration.Dynamic a1 = ctx.addServlet("a1", fs);
a1.addMapping("/a");
ServletRegistration.Dynamic a2 = ctx.addServlet("a2", ss);
a2.addMapping("/b");
}
}
@@ -0,0 +1,16 @@
package com.ycjy.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
// 第一个Servlet
public class FirstServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("--firstServlet");
}
}
@@ -0,0 +1,16 @@
package com.ycjy.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
// 第二个Servlet
public class SecondServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("--SecondServlet");
}
}
+1
View File
@@ -16,6 +16,7 @@
<module>SpringFramework-Aop</module>
<module>SpringFramework-Affairs</module>
<module>SpringFramework-MVC</module>
<module>Spring-ch09</module>
</modules>
<properties>