New servlet Features: annotations

Source: Internet
Author: User

Reference:

http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/

Deployment profile for Servlet 3.0 The top-level label for Web. XML <web-app> has a Metadata-complete property that specifies whether the current deployment profile is complete. If set to true, the container will only rely on the deployment profile at deployment time, ignoring all annotations (also skipping web-fragment.xml scans, i.e. disabling pluggable support, see the following for pluggable support); If you do not configure this property, or set it to False, annotation support (and pluggable support) is enabled.

@WebServlet

@WebServlet is used to declare a class as a servlet, which will be handled by the container at deployment time, and the container will deploy the appropriate class as a servlet based on the specific property configuration. The note has some of the common properties given in the following table (all of which are optional), but Vlaue or urlpatterns are usually required and cannot coexist, and if specified at the same time, the value of value is usually ignored. Name is used to map to the servlet by name when using filters:

Table 1. @WebServlet List of main attributes
Property name type Description
Name String Specifies the Name property of the Servlet, equivalent to <servlet-name>. If not explicitly specified, the value of the Servlet is the fully qualified name of the class.
Value String[] This property is equivalent to the Urlpatterns property. Two attributes cannot be used at the same time.
Urlpatterns String[] Specifies the URL-matching pattern for a set of Servlets. Equivalent to <url-pattern> tag.
Loadonstartup Int Specifies the order in which the Servlet is loaded, equivalent to the <load-on-startup> tag.
InitParams Webinitparam[] Specifies a set of Servlet initialization parameters, equivalent to the <init-param> tag.
asyncsupported Boolean Declares whether the Servlet supports asynchronous operation patterns, equivalent to <async-supported> tags.
Description String The Servlet's descriptive information is equivalent to the <description> tag.
DisplayName String The display name of the Servlet, usually used in conjunction with the tool, is equivalent to the <display-name> tag.

The following is a simple example:

True=-1, name = "Simpleservlet", DisplayName = "ss"= {@WebInitParam (name = "Username", value = "to M "publicclassextends httpservlet{...}

With this configuration, you do not have to configure the appropriate <servlet> and <servlet-mapping> elements in Web. XML, and the container publishes the class as a servlet at deployment time, based on the specified properties. Its equivalent web. XML configuration form is as follows:

<servlet>    <Display-name>Ss</Display-name>    <Servlet-name>Simpleservlet</Servlet-name>    <Servlet-class>Footmark.servlet.SimpleServlet</Servlet-class>    <Load-on-startup>-1</Load-on-startup>    <async-supported>True</async-supported>    <Init-param>        <Param-name>Username</Param-name>        <Param-value>Tom</Param-value>    </Init-param></servlet><servlet-mapping>    <Servlet-name>Simpleservlet</Servlet-name>    <Url-pattern>/simple</Url-pattern></servlet-mapping>
@WebInitParam

This annotation is usually not used alone, but is used in conjunction with @WebServlet or @WebFilter. Its purpose is to specify initialization parameters for the Servlet or filter, which is equivalent to the <init-param> sub-tags of <servlet> and <filter> in Web. Xml. @WebInitParam have some common properties that are given in the following table:

Table 2. Common Properties for @WebInitParam

Property name type is optional Description
Name String Whether Specifies the name of the parameter, equivalent to <param-name>.
Value String Whether Specifies the value of the parameter, equivalent to <param-value>.
Description String Is A description of the parameter is equivalent to <description>.

The following is a simple example:

@WebFilter (Servletnames = {"Simpleservlet"},filtername= "Simplefilter"Publicclass  implements filter{...}

With this configuration, you do not have to configure the appropriate <filter> and <filter-mapping> elements in Web. XML, and the container publishes the class as a filter at deployment time, based on the specified properties. Its equivalent in Web. XML is configured in the following form:

<Filter>     <Filter-name>Simplefilter</Filter-name>     <Filter-class>Xxx</Filter-class> </Filter> <filter-mapping>     <Filter-name>Simplefilter</Filter-name>     <Servlet-name>Simpleservlet</Servlet-name> </filter-mapping>

@WebFilter

@WebFilter is used to declare a class as a filter that will be handled by the container at deployment time, and the container will deploy the appropriate class as a filter based on the specific property configuration. The note has some of the common properties given in the following table (all the properties below are optional, but value, Urlpatterns, Servletnames must contain at least one, and value and urlpatterns cannot coexist, and if specified at the same time, it is generally ignored Value):

Table 3. Common Properties for @WebFilter
Property name type Description
FilterName String Specify the Name property of the filter, equivalent to <filter-name>
Value String[] This property is equivalent to the Urlpatterns property. But both should not be used at the same time.
Urlpatterns String[] Specifies the URL-matching pattern for a set of filters. Equivalent to <url-pattern> tag.
Servletnames String[] Specifies which servlets the filter will be applied to. The value is the value of the name attribute in the @WebServlet, or the value of <servlet-name> in Web. Xml.
Dispatchertypes Dispatchertype Specifies the forwarding mode of the filter. Specific values include:
ASYNC, ERROR, FORWARD, INCLUDE, REQUEST.
InitParams Webinitparam[] Specifies a set of filter initialization parameters, equivalent to the <init-param> tag.
asyncsupported Boolean Declares whether the filter supports asynchronous operation mode, equivalent to the <async-supported> tag.
Description String The description information of the filter is equivalent to the <description> tag.
DisplayName String The filter's display name, usually used with the tool, is equivalent to the <display-name> tag.

The following is a simple example:

@WebFilter (Servletnames = {"Simpleservlet"},filtername= "Simplefilter"publicclass  Implements filter{...}

@WebListener

This annotation is used to declare a class as a listener, and the class that is @WebListener labeled must implement at least one of the following interfaces:

    • Servletcontextlistener
    • Servletcontextattributelistener
    • Servletrequestlistener
    • Servletrequestattributelistener
    • Httpsessionlistener
    • Httpsessionattributelistener

This annotation is very simple to use and its properties are as follows:

Table 4. Common Properties for @WebListener
Property name type is optional Description
Value String Is The description information for the listener.

A simple example is as follows:

@WebListener ("This was only a demo listener") public class SimpleListener implements servletcontextlistener{...}

In this case, you do not need to configure the <listener> tag in Web. Xml. It is equivalent to the configuration form in Web. Xml as follows:

<listener>     <listener-class>footmark.servlet.SimpleListener</listener-class> </ Listener>
@MultipartConfig

This note is primarily intended to assist in the support provided by HttpServletRequest in Servlet 3.0 for uploading files. The note is labeled above the servlet to indicate that the MIME type of the request that the servlet wishes to process is multipart/form-data. In addition, it provides several properties for simplifying the processing of uploaded files. Specific as follows:

Table 5. Common Properties for @MultipartConfig
Property name type is optional Description
Filesizethreshold Int Is When the amount of data is greater than this value, the content is written to the file.
Location String Is Store the generated file address.
MaxFileSize Long Is Maximum number of files allowed to upload. The default value is-1, which means there is no limit.
Maxrequestsize Long Is The maximum number of requests for the Multipart/form-data, the default value is-1, which means there is no limit.


New servlet Features: annotations

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.