Servlet3.0的注解

摘要

1.@WebListener注解 表示的就是我们之前的在xml中配置的 <listener>&lt […]

1.@WebListener注解

表示的就是我们之前的在xml中配置的

<listener>
<listener-class>ListenerClass</listener-class>
</listener>

下面我们只需要在我们写好的Listener类上面加上这[email protected]

使用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[email protected](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() {
        
    }
}

注解的主要参数及其含义

Servlet3.0的注解

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);
    }
}

注解主要参数

Servlet3.0的注解

参数和Filter差不多

[email protected] 注解 表示的就是参数啦 相当于<init-param>

参数以及含义

 Servlet3.0的注解

 5.@MultipartConfig  该注解主要是为了辅助 Servlet 3.0 中 HttpServletRequest 提供的对上传文件的支持。该注解标注在 Servlet 上面,以表示该 Servlet 希望处理的请求的 MIME 类型是 multipart/form-data。另外,它还提供了若干属性用于简化对上传文件的处理。具体如下:

Servlet3.0的注解

在Servlet Filter中使用Spring容器  http://www.searu.org/2015-06/118881.htm

Java Servlet关键点详解  http://www.searu.org/2015-06/118468.htm

Tomcat手工搭建Jsp和Servlet程序  http://www.searu.org/2014-07/104282.htm

Servlet 3.0的AsyncListener接口 http://www.searu.org/2015-07/120496.htm

Servlet 3.1 规范(最终版)中文版+英文版PDF  http://www.searu.org/2016-05/131152.htm

Servlet生命周期实例代码 http://www.searu.org/2015-12/126300.htm

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: