1.@WebListener注解
表示的就是我們之前的在xml中配置的
<listener>
<listener-class>ListenerClass</listener-class>
</listener>
下面我們只需要在我們寫好的Listener類上面加上這個@WebListener注解就OK啦
使用Listener的類必須使用下列的幾個接口
ServletContextListener ServletContextAttributeListener ServletRequestListener ServletRequestAttributeListener HttpSessionListener HttpSessionAttributeListener
例子:
package com.linuxidc.web.listener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class MyHttpSessionListener implements HttpSessionListener{ @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("創建session "); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("銷毀session "); } }
這樣,在我們啟動的時候 就會自動的幫助我們創建一個Listener了,這個注解有一個屬性,是這個listener的描述信息
2.@WebFilter 這個注解就是表示的攔截器 同樣的也是直接注解在寫好的Filter上面就ok了。
package com.linuxidc.web.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @WebFilter(filterName = "filter1", urlPatterns="/*", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}, initParams={@WebInitParam(name="account",value="1234"),@WebInitParam(name="linuxidc",value="1234")} ) public class MyFilter implements Filter { @Override public void init(final FilterConfig filterConfig) throws ServletException { String account = filterConfig.getInitParameter("account"); String linuxidc = filterConfig.getInitParameter("linuxidc"); System.out.println("account:"+account+" linuxidc:"+hotusm); } @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { chain.doFilter(request, response); } @Override public void destroy() { } }
注解的主要參數及其含義
3.@WebServlet 這個注解表示的就是一般的Servlet
package com.linuxidc.web.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet(name = "servlet1", urlPatterns = {"/url1", "/url2","url3/*"}, loadOnStartup = 1, initParams = { @WebInitParam(name = "name", value="linuxidc") }) public class MyServlet extends HttpServlet { private String name; public MyServlet() { System.out.println("load on startup"); } @Override public void init() throws ServletException { super.init(); name= this.getInitParameter("name"); } @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); System.out.println(session); System.out.println("name:"+name); } }
注解主要參數
參數和Filter差不多
4.其中在上面的注解Filter和Servlet用到的@WebInitParam 注解 表示的就是參數啦 相當於<init-param>
參數以及含義
5.@MultipartConfig 該注解主要是為了輔助 Servlet 3.0 中 HttpServletRequest 提供的對上傳文件的支持。該注解標注在 Servlet 上面,以表示該 Servlet 希望處理的請求的 MIME 類型是 multipart/form-data。另外,它還提供了若干屬性用於簡化對上傳文件的處理。具體如下:
在Servlet Filter中使用Spring容器 http://www.linuxidc.com/Linux/2015-06/118881.htm
Java Servlet關鍵點詳解 http://www.linuxidc.com/Linux/2015-06/118468.htm
Tomcat手工搭建Jsp和Servlet程序 http://www.linuxidc.com/Linux/2014-07/104282.htm
Servlet 3.0的AsyncListener接口 http://www.linuxidc.com/Linux/2015-07/120496.htm
Servlet 3.1 規范(最終版)中文版+英文版PDF http://www.linuxidc.com/Linux/2016-05/131152.htm
Servlet生命周期實例代碼 http://www.linuxidc.com/Linux/2015-12/126300.htm