Servlet 动态注册
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user