After you've written the servlet, let's tell the Web container some information about the servlet. In servlet 3.0, you can use annotations (Annotation) to tell the container which servlets provide services and additional information. For example in Helloservlet.java: @WebServlet ("/hello.view") public class helloservlet extends Httpservlet {as long as the @webservlet annotation is set on the servlet, the container will automatically read the information in it. The above @webservlet tells the container that if the requested URL is "/hello.view", the service is provided by the instance of HelloServlet. You can use @webservlet to provide more information. @WebServlet ( name= "Hello", urlpatterns={"/hello.view"}, loadonstartup=1) public class helloservlet extends httpservlet { The above @webservlet informs the container that the name of the servlet helloservlet is Hello, which is specified by the Name property, and if the URL requested by the client is/ Hello.view, it is handled by a servlet with the Hello name, which is specified by the Urlpatterns property. When using annotations in Java ee related applications, you can remember that properties that are not set usually have default values. For example, if you do not set the Name property of @webservlet, the default value is the full name of the servlet's class. When the application starts, it does not actually create all the servlet instances. The container instantiates the corresponding servlet class, initializes it, and then processes the request when it first requests a servlet service. This means that the client requesting the servlet for the first time must wait for the servlet class to instantiate, and the initial action must take time to actually get the requested processing. If you want the application to start by loading, instantiating, and initializing the servlet class first, you can use the Loadonstartup setting. SetA value greater than 0 (the default is-1), which means that the servlet is initialized when the application is started (instead of instantiating several servlets). The number represents the initial order of the servlet, and the container must ensure that a servlet with a smaller number is initialized first, and that if more than one servlet uses the same number when setting loadonstartup, using annotations The container implementation vendor can decide for itself which servlet to load.
Supplemental Add: Callout (Annotation) declaration, you do not need to re-declare the servlet information in Web. xml: <?xml version= "1.0" encoding= "UTF-8"? ><web-app Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" Metadata-complete= "false" version= "2.5" > <display-name>mavenWeb</display-name> <servlet> < Servlet-name>hservlet</servlet-name>
<servlet-class>com.kim.demo.HelloServlet</servlet-class> <load-on-startup>1</ Load-on-startup> </servlet><servlet-mapping><servlet-name>HServlet</servlet-name> <url-pattern>/HServlet</url-pattern></servlet-mapping></web-app>
Attention:
metadata-complete= "false" default is
metadata-complete= "true" required modification of
servlet 3.0 Deployment Profile web.xml top-level label <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 scanning, which disables pluggable support), if the property is not configured, or if it is set to false, annotation support (and pluggable support) is enabled. It's a lot easier to apply annotations, but now you don't have to write more
with basic, primitive servlets such as integrated spring.
@webservlet ("/helloservlet") principle in the Java WEB Engineering servlet