Starting with the Java EE5 specification, the servlet adds two annotations (Annotation) that affect the servlet life cycle: @PostConstruct and @preconstruct. These two annotations are used to decorate a non-static void () method. And this method cannot have a throw exception declaration.
How to use, for example:
1 @PostConstruct //mode of public void SomeMethod () {3 ... 4 }5 6 public @PostConstruct void SomeMethod () { //way ... 8 }
[Email protected] Description
The method that is modified by @postconstruct will run when the server loads the servlet and will only be called once by the server, similar to the Serclet Inti () method. Methods that are modified by @postconstruct will run before the Init () method after the constructor.
[Email protected] Description
The @preconstruct-modified method runs when the server unloads the servlet and is only called once by the server, similar to the Destroy () method of the servlet. Methods that are modified by @preconstruct are run after the Destroy () method, before the servlet is completely uninstalled. (See the program practice below)
3. Procedure Practice
Xml
1 <!--@PostConstruct and @predestroy annotations-->2 <servlet>3 <servlet-name>AnnotationServlet< /servlet-name>4 <servlet-class>com.servlet.annotationservlet</servlet-class>5 </ Servlet>6 <servlet-mapping>7 <servlet-name>annotationservlet</servlet-name>8 < Url-pattern>/servlet/annotationservlet</url-pattern>9 </servlet-mapping>
Annotationservlet
1 package com.servlet; 2 3 Import java.io.IOException; 4 Import Java.io.PrintWriter; 5 Import Java.sql.Time; 6 Import Java.text.SimpleDateFormat; 7 Import Java.util.Date; 8 9 Import javax.annotation.postconstruct;10 import javax.annotation.predestroy;11 Import JAVAX.SERVLET.SERVLETEXCEPTION;12 Import javax.servlet.http.httpservlet;13 Import JAVAX.SERVLET.HTTP.HTTPSERVLETREQUEST;14 Import javax.servlet.http.httpservletresponse;15 public class Annotationservlet extends HttpServlet {simpledateformat df = new SimpleDateFormat ("HH:mm:ss. SSS ");//Set date format, accurate to milliseconds, public annotationservlet () {System.out.println (" Time: "+df.format (new Date ()) +" Execute }22 destroy () {This.log ("Time:" +df.format (New Date ()) + "Execute Destroy () method ..."). ;//super.destroy (); Just puts "destroy" string in LOG26//Put Your code here27}28 @PostConstruct30 public void som Emethod () {//this.log ("Performing @postconstruct-ModifiedSomeMethod () method ... ");//Note: This error is System.out.println (" Time: "+df.format (new Date ()) +" SomeMethod () to perform @postconstruct adornments. Method ... "),}34 @PreDestroy36 public void Othermethod () {PNs System.out.println (" Time: "+df.format (NE W Date ()) + "Othermethod () method to perform @predestroy decoration ..."),}39-public void doget (HttpServletRequest request, Httpse Rvletresponse response) throws Servletexception, IOException {this.log ("Time:" +df.format (New Date () + "Execute doget () method ...");}44 public void Init () throws Servletexception {//Put your code HERE47 This.log ("Time:" +df.format (New Date ()) + "Execute init () method ..."),}49 protected void service (HttpServletRequest R Equest, httpservletresponse response) Wuyi throws Servletexception, ioexception{52 This.log ("Time:" +df.f Ormat (New Date ()) + "perform service () method ..."); Super.service (request, response); 54}55 56}
Operation Result:
4. Precautions
The number of annotations can affect the server's startup speed. When the server starts, it traverses all the class files under the Web app's web-inf/classes and all the jar files under Web-inf/lib to check which classes use annotations. If no annotations are used in the program, you can set the <web-app> Metadatacomplete property to True in Web. Xml to turn off routine checks at server startup.
Servers that support annotations need to support Servlet2.5 and above, so Tomcat requires 6.0.X and later.
@postconstruct and @preconstruct annotations for Java development