Show TOC

Procedure documentationConfiguring Methods Using Annotations in Web Applications Locate this document in the navigation structure

 

You can define the moment when a method is called by using the corresponding annotations.

There are two annotations for configuring method invocation:

  • @PostConstruct

    This annotation specifies that the method will be called before all lifecycle methods are called.

  • @PreDestroy

    This annotation specifies that the method will be called before the servlet object is destroyed.

Procedure

Using the @PostConstruct Annotation

Use this annotation at method level without any attributes.

Example Example

  1. public class PostConstructAnnot_servlet extends HttpServlet {
    
    //…
    
    	@PostConstruct
    	public void postConstruct() {
    		//Invocation of init methods, preparative operations
    		System.out.println("PostConstructAnnot_servlet: "
    				+ " postConstruct method invoked! ");
    	}
    
    //…
    
    }
    
End of the code.
Using the @PreDestroy Annotation

Use this annotation at method level without any attributes.

Example Example

  1. public class PreDestroyAnnot_servlet extends HttpServlet {
    
    //…
    
    @PreDestroy
        public void preDestroy(){
             //Clean up any open resources
        	     System.out.println("PreDestroyAnnot_servlet: " +
     		                " preDestoy method invoked! "); 
        }
    
    //…
    
End of the code.