Servlet comments and deployment descriptors

Source: Internet
Author: User

It is worth noting that the deployment descriptor takes precedence over the annotation. In other words, the deployment descriptor overwrites the configuration information specified by the annotation mechanism. The Web deployment descriptor version 3.0 contains a new attribute named metadata-complete on the web-app element. This attribute defines whether the web descriptor is complete, or whether the class files of the web application are annotated with the specified deployment information. If this attribute is set to true, the deployment tool must ignore any servlet comments in the class file and only use the configuration details mentioned in the descriptor. Otherwise, if this value is not specified or set to false, the container must scan all class files of the Application for comments. This attribute allows you to enable or disable annotation scanning and process annotations during application startup.

All comments introduced in Servlet 3.0 can be found in javax. servlet. http. annotation and javax. servlet. http. annotation. jaxrs software packages. The following sections describe the complete set of Servlet Annotations:

@ Servlet: javax. servlet. http. annotation. Servlet is a Class-level annotation. confirm that the annotated class is a servlet and save metadata about the declared servlet. The urlMappings attribute is the mandatory attribute of @ servlet that specifies the URL mode (call this Servlet. When a request is received, the container matches the URL in the request with the servlet's urlMappings. If the URL pattern matches, the corresponding servlet is called to respond to the request. All other attributes of the comment are optional and have reasonable default values. The Servlet class must have an HttpMethod annotation such as GET, PUT, POST, HEAD, or DELETE for annotation. These methods should take HttpServletRequest and HttpServletResponse as method parameters. Contrary to previous versions, the version of servlets 3.0 can be implemented as a simple traditional Java object (Plain Old Java Objects, POJOs; that is, servlets does not need to extend basic servlet implementation classes such as HTTPServlet or GenericServlet.

For comparison, Java Servlet code snippets written using the traditional servlet 2.5 API are provided as follows. In Servlet 2.5, as long as the detailed servlet Information is configured in the deployment descriptor, the web Container will initialize the servlet.

 
  1. public class MyServlet extends HttpServlet {  
  2. public void doGet (HttpServletRequest req,  
  3. HttpServletResponse res) {  
  4. ....  
  5. }  
  6. } 

Deployment descriptor (web. xml)

 
  1. <web-app> 
  2. <servlet> 
  3. <servlet-name>MyServlet</servlet-name> 
  4. <servlet-class>samples.MyServlet</servlet-class> 
  5. </servlet> 
  6.  
  7. <servlet-mapping> 
  8. <servlet-name>MyServlet</servlet-name> 
  9. <url-pattern>/MyApp</url-pattern> 
  10. </servlet-mapping> 
  11. ...  
  12.  
  13. </web-app> 

Here we provide a simplified version written using the Servlet 3.0 API. When MyServlet is annotated as a Servlet using @ servlet annotations, It is initialized during the startup of the web container. Note that the deployment descriptor is optional in this case.

 
  1. @Servlet(urlMappings={"/MyApp"})  
  2. public class MyServlet {  
  3. @GET  
  4. public void handleGet(HttpServletRequest req,  
  5. HttpServletResponse res) {  
  6. ....  
  7. }  
  8. }  
  9. Deployment descriptor (web.xml)  
  10.  
  11. optional 

@ ServletFilter and @ FilterMapping: You can use javax. servlet. http. annotation. ServletFilter annotations to comment out the filter class, so that you can easily create a servlet filter. This comment encapsulates the metadata of the filters being declared. @ FilterMapping annotation is mandatory in the filter class. @ FilterMapping annotation defines the URL pattern used for the filter. @ ServletFilter all other attributes are optional with reasonable default values. The V3.0 Filter class is now similar to the POJO class and does not have the Filter interface or non-parameter public constructor required for these classes. The following code snippet of the filter class using Servlet v2.5 API is provided:

 
  1. public class MyFilter implements Filter {  
  2. public void doFilter(ServletRequest req,  
  3. ServletResponse res,  
  4. FilterChain chain)  
  5. throws IOException, ServletException {  
  6. ......  
  7. }  
  8. }  
  9.  
  10. Deployment descriptor (web.xml)  
  11.  
  12. <web-app> 
  13. <filter> 
  14. <filter-name>My Filter</filter-name> 
  15. <filter-class>samples.MyFilter</filter-class> 
  16. </filter> 
  17.  
  18. <filter-mapping> 
  19. <filter-name>My Filter</filter-name> 
  20. <url-pattern>/foo</url-pattern> 
  21. </filter-mapping> 
  22. ...  
  23. </web-app> 

The following is an example of a filter class written in Servlet 3.0. Because this class uses the ServletFilter annotation, the container marks MyFilter as a filter class. MyFilter intercepts all received requests. The request URL matches/foo. Servlet 3.0 provides an optional deployment descriptor for the filter configuration.

 
  1. @ServletFilter  
  2. @FilterMapping("/foo")  
  3. public class MyFilter {  
  4. public void doFilter(HttpServletRequest req,  
  5. HttpServletResponse res) {  
  6. .....  
  7. }  
  8. }  
  9.  
  10. Deployment descriptor (web.xml)  
  11.  
  12. optional 

@ InitParam: This annotation can be used to define any initialization parameters that must be passed to the servlet or filter class. It is an attribute annotated by @ Servlet and @ ServletFilter. The following code example explains how to pass an initialization parameter with an english value called lang to a servlet class.

 
  1. @Servlet(urlMappings={"/MyApp"}, initParams ={@InitParam
    (
    name="lang", value="english")})  
  2. public class MyServlet {  
  3. @GET  
  4. public void handleGet(HttpServletRequest req,  
  5. HttpServletResponse res) {  
  6. ....  
  7. }  
  8. } 

@ ServletContextListener: javax. servlet. http. annotation. ServletContextListener comments declare this class as a servlet context listener. When a web Container creates or destroys ServletContext, the context listener receives comments. The context listener is a POJO class and does not need to implement the ServletContextListener interface. The listener class written using the Servlet 2.5 API is as follows. The container recognizes the listener class only when it is configured in the deployment descriptor.

 
  1. public class MyListener implements ServletContextListener {  
  2. public void contextInitialized(ServletContextEvent sce) {  
  3.  
  4. }  
  5. .....  
  6. }  
  7.  
  8. Deployment Descriptor (web.xml)  
  9.  
  10. <web-app> 
  11. <listener> 
  12. <listener-class>samples.MyListener</listener-class> 
  13. </listener> 
  14. ....  
  15. </web-app> 

Use the Servlet 3.0 API to write a listener class that is greatly simplified, as shown below.

 
  1. @ServletContextListener  
  2. public class MyListener {  
  3. public void contextInitialized (ServletContextEvent sce) {  
  4.  
  5. }  
  6. .....  
  7. }  
  8.  
  9. Deployment Descriptor (web.xml)  
  10.  
  11. optional 
  1. Servlet sharing links in sessions
  2. Configure the Servlet Development Environment
  3. Future Response Servlet features
  4. Tomcat Java Servlet Engine Security Settings
  5. Jetty-Additional Servlet container Functions

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.