[email protected]注解
表示的就是我们之前的在xml中配置的
<listener>
<listener-class>ListenerClass</listener-class>
</listener>
下面我们只需要在我们写好的Listener类上面加上这个@WebListener注解就OK啦
使用Listener的类必须使用下列的几个接口
ServletContextListener ServletContextAttributeListener ServletRequestListener ServletRequestAttributeListener HttpSessionListener HttpSessionAttributeListener
例子:
package com.hotusm.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的描述信息
[email protected] 这个注解就是表示的拦截器 同样的也是直接注解在写好的Filter上面就ok了。
package com.hotusm.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="hotusm",value="1234")} ) public class MyFilter implements Filter { @Override public void init(final FilterConfig filterConfig) throws ServletException { String account = filterConfig.getInitParameter("account"); String hotusm = filterConfig.getInitParameter("hotusm"); System.out.println("account:"+account+" hotusm:"+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() { } }
注解的主要参数及其含义
[email protected] 这个注解表示的就是一般的Servlet
package com.hotusm.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="hotusm") }) 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>
参数以及含义
[email protected] 该注解主要是为了辅助 Servlet 3.0 中 HttpServletRequest 提供的对上传文件的支持。该注解标注在 Servlet 上面,以表示该 Servlet 希望处理的请求的 MIME 类型是 multipart/form-data。另外,它还提供了若干属性用于简化对上传文件的处理。具体如下:
时间: 2024-10-12 16:56:11