欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Servlet3

程序员文章站 2022-04-18 10:58:00
...

一、注解一个Servlet3规范的Servlet

@WebServlet(name="hello",asyncSupported=true, 
		loadOnStartup=2,
		urlPatterns = { "/test/hello" },
		initParams={@WebInitParam(name="config",value="config.xml")}  
)
public class Hello extends HttpServlet 

 二、Listener注解

@WebListener("This is a servlet3 listener test on startUp")
public class ListenerOnStart implements ServletContextListener

 三、Filter注解

@WebFilter(
		 dispatcherTypes = {
		   DispatcherType.REQUEST, 
		   DispatcherType.FORWARD,
		   DispatcherType.INCLUDE
		 }, 
		 urlPatterns = { "/test/LogServlet" }, 
		 asyncSupported = true //支持异步Servlet
		)
public class LogFilter implements Filter 

 四、web.xml中的头

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0" metadata-complete="true">

 其中如web.xml中的Filter,Servlet有过滤到该URL时,如果该Servlet需要支持异步,请将这些Filter,Servlet添加

<async-supported>true</async-supported>