Servlet3.0的注解

Posted 默默爬坑中

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet3.0的注解相关的知识,希望对你有一定的参考价值。

1.@WebListener注解

表示的就是我们之前的在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的描述信息

2.@WebFilter  这个注解就是表示的拦截器  同样的也是直接注解在写好的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() {
        
    }
}

注解的主要参数及其含义

3.@WebServlet 这个注解表示的就是一般的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>

参数以及含义

 

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

 例子:

<form action="upload" enctype="multipart/form-data" method="POST">
    <input name="file" type="file" />
    <input type="submit" value="Upload" />
</form>
package com.hotusm.web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@MultipartConfig(location="C://upload")
@WebServlet(name="upload",urlPatterns={"/upload"})
public class UploadServlet extends HttpServlet{
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");  
        resp.setContentType("text/html; charset=UTF-8");  
        PrintWriter out = resp.getWriter();  
        Part part = req.getPart("file");  
        String fileName = part.getHeader("content-disposition");
        System.out.println(fileName);
        System.out.println(fileName.substring(fileName.lastIndexOf(File.separator)+1, fileName.length()-1));
        part.write( fileName.substring(fileName.lastIndexOf(File.separator)+1, fileName.length()-1));
        out.write("{\'type\':\'1\',\'msg\':\'success\'}");
    }
}

 

以上是关于Servlet3.0的注解的主要内容,如果未能解决你的问题,请参考以下文章

Servlet3.0学习总结——使用注解标注Servlet

Spring注解驱动开发-AOPTx和Servlet3.0

重新学习Spring注解——servlet3.0

Servlet3.0提供的@WebServlet注解引用参数详情介绍

Servlet3.0实现注解开发

Servlet3.0的注解