The second annotation of servlet3.0 's new feature replaces the Web. XML configuration file

Source: Internet
Author: User

New features of servlet3.0:
    1. The annotations replace the Web. xml file
    2. Support for asynchronous processing
    3. Support for uploading files
1. Annotations instead of configuration files

1. The Web. xml file was deleted

2. Add @webservlet (urlpatterns={"/aservlet"}) on the Servlet class

3. Add @webfilter (urlpatterns= "/afilter") on the filter class

4. Add @weblistener on the Listener class

?

Summarize:

* Annotation Benefits: Less configuration information, easy to use!

* Note disadvantages: not easy to modify! (Change the source code if you need to modify it)

?

2. Usage examples:

?

Aservlet.java file

/*

originally servlet in the Web. XML a large amount of information needs to be configured in the file:

<servlet>

<servlet-name>AServlet</servlet-name>

<servlet-class>cn.itcast.web.servlet.AServlet</servlet-class>

<init-param>

<param-name>p1</param-name>

<param-value>v1</param-value>

</init-param>

<init-param>

<param-name>p2</param-name>

<param-value>v2</param-value>

</init-param>

<load-on-startup>1<load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>AServlet</servlet-name>

<url-pattern>/AServlet</url-pattern>

<url-pattern>/AAServlet</url-pattern>

</servlet-mapping>

?

*/

?

/*

* but now use Note, it's much easier.

*/

@WebServlet(urlpatterns="/aservlet",

???? initparams={

???????? @WebInitParam(name="P1", value="v1"),

???????? @WebInitParam(name="P2", value="v2")

????},

???? Loadonstartup=1

)

Public classaservletextends httpservlet {

???? Public void doget (httpservletrequest req, HttpServletResponse resp)

???????????? throws servletexception, IOException {

???????? System. out. println ("Hello servlet3.0!" );

???????? resp. getwriter (). Print ("Hello servlet3.0!!" );

????}

}

?

Afilter.java file: Annotated use of demo filters

//(urlpatterns= "/*") represents : Filter all the paths

@WebFilter(urlpatterns="/*")

Public class afilter implements Filter {

?

???? @Override

???? Public void Destroy () {

???????? // TODO auto-generated method stub

????????

????}

?

???? @Override

???? Public void doFilter (servletrequest request, Servletresponse repsonse ,

???????????? Filterchain chain) throws IOException, servletexception {

???????? System. out. println (" haha ~, you see me no! ");

???????? chain. DoFilter(request, repsonse); release

????}

?

???? @Override

???? Public void init (filterconfig arg0) throws servletexception {

???????? // TODO auto-generated method stub

????????

????}

?

}

?

Alistener.java file: Annotated use of demo listener

Import javax.servlet.ServletContextEvent;

Import Javax.servlet.ServletContextListener;

Import Javax.servlet.annotation.WebListener;

// Notes for Listeners

@WebListener

Public class Alistener implements Servletcontextlistener {

?

???? @Override

???? Public void contextdestroyed (servletcontextevent arg0) {

???????? System. out. println (" dead. ");

?

????}

?

???? @Override

???? Public void contextinitialized (servletcontextevent arg0) {

???????? System. out. println (" been born ");

?

????}

?

}

The second annotation of servlet3.0 's new feature replaces the Web. XML configuration file

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.