Show TOC Start of Content Area

Procedure documentation Configuring Methods Using Annotations in Web Applications  Locate the document in its SAP Library structure

Use

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

public class PostConstructAnnot_servlet extends HttpServlet {

 

//…

 

   @PostConstruct

   public void postConstruct() {

      //Invocation of init methods, preparative operations

      System.out.println("PostConstructAnnot_servlet: "

            + " postConstruct method invoked! ");

   }

 

//…

 

}

Using the @PreDestroy Annotation

Use this annotation at method level without any attributes.

Example

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 Content Area