web.xml配置还是比较麻烦,这次使用注解方式
编写servlet
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/test2")
public class ServletTest2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
System.out.println("雷霆嘎巴无情哈拉少");
response.getWriter().write("雷霆嘎巴无情哈拉少");
}
}
运行效果
刚才的@WebServlet注解是3.0提出的,那么 3.0还有哪些规范
文档: https://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/
- 一个容器在启动的时候会去找 ServletContainerInitializer 实例对象,该 实例对象 必须放在
META-INF/services 目录下中一个文件名称叫 javax.servlet.ServletContainerInitializer 里面
- javax.servlet.ServletContainerInitializer
com.min.demo2.MyServletContainerInitializerTest
- 编写一个servlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyServletTest3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
System.out.println("MyServletTest3--------------");
response.getWriter().write("大威天龙");
}
}
- 编写 ServletContainerInitializer 的实现类
import com.min.demo3.MyServletTest3;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.util.Set;
public class MyServletContainerInitializerTest implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException { //启动时执行
//System.out.println("MyServletContainerInitializerTest-------------");
//servletContext.addServlet(名称,new出来的Servlet对象);
//servletContext.addFilter();
//servletContext.addListener();
//添加上自己定义的servlet
ServletRegistration.Dynamic dynamic = servletContext.addServlet("aaa", new MyServletTest3());
//设置启动顺序
dynamic.setLoadOnStartup(1);
//设置访问路径
dynamic.addMapping("/demo3");
}
}
- 运行效果
@HandlesTypes
@HandlesTypes(value={aaa.class}) 可以传入一个接口的字节码对象,在执行 onStartup 的时候,会把这个字节码接口对应的所有抽象类 、子接口、 实现类都获取到, 并且放在 set集合里面 保存起来
package com.min.demo2;
import com.min.demo3.MyServletTest3;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.HandlesTypes;
import java.util.Set;
@HandlesTypes(value = {IHello.class}) // 可以传入一个接口的字节码对象
public class MyServletContainerInitializerTest implements ServletContainerInitializer {
// 在执行 onStartup 的时候,会把这个字节码接口对应的所有抽象类 、子接口、 实现类都获取到, 并且放在 set集合里面 保存起来
@Override
public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
//System.out.println("MyServletContainerInitializerTest-------------");
//servletContext.addServlet(名称,new出来的Servlet对象); 添加一个servlet
//servletContext.addFilter(); 添加一个 filter
//servletContext.addListener(); 添加一个listener
//添加上自己定义的servlet
ServletRegistration.Dynamic dynamic = servletContext.addServlet("aaa", new MyServletTest3());
//设置启动顺序
dynamic.setLoadOnStartup(1);
//设置访问路径
dynamic.addMapping("/demo3");
for (Class<?> aClass : set) {
System.out.println(aClass);
}
}
}
- 编写一个IHello 接口
package com.min.demo2;
public interface IHello {
}
- 编写一个子接口
package com.min.demo2;
public interface IHello_SON extends IHello {
}
- 编写一个实现类
package com.min.demo2;
public class IHello_Impl implements IHello{
}
- 编写一个抽象类
package com.min.demo2;
public abstract class IHello_Abstract implements IHello {
}
- 重启tomcat,观察打印结果