Servlet3.0 annotation and Servlet3.0 Annotation

Source: Internet
Author: User

Servlet3.0 annotation and Servlet3.0 Annotation

1. @ WebListener Annotation

It indicates

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

Next we just need to add this @ WebListener Annotation on the Listener class we have written.

The Listener class must use the following interfaces:

ServletContextListenerServletContextAttributeListenerServletRequestListenerServletRequestAttributeListenerHttpSessionListenerHttpSessionAttributeListener

Example:

Package com. hotusm. web. listener; import javax. servlet. annotation. webListener; import javax. servlet. http. httpSessionEvent; import javax. servlet. http. httpSessionListener; @ WebListenerpublic class MyHttpSessionListener implements HttpSessionListener {@ Override public void sessionCreated (HttpSessionEvent se) {System. out. println ("create session") ;}@ Override public void sessionDestroyed (HttpSessionEvent se) {System. out. println ("Destroy session ");}}

In this way, we will automatically create a Listener when we start it. This annotation has an attribute, which is the description of this listener.

2. @ WebFilter the annotation indicates that the interceptor is also directly annotated on the written Filter.

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() {            }}

Main Parameters of the annotation and their meanings

3. @ WebServlet This annotation represents a general 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);    }}

Annotation Parameters

The parameter is similar to the Filter parameter.

4. In the above annotation, the @ WebInitParam annotation used by the Filter and Servlet represents the parameter, which is equivalent to <init-param>

Parameters and definitions

 

5. @ MultipartConfig This annotation is mainly used to assist HttpServletRequest in Servlet 3.0 to support file upload. The annotation is marked on the Servlet to indicate that the MIME type of the request to be processed by the Servlet is multipart/form-data. In addition, it provides several attributes to simplify the processing of uploaded files. The details are as follows:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.