java2db

Servlet annotations example (Use annotation in servlet)



Servlet annotations are the java statements which are alternate for XML files which can  do resources configuration.

 

There are two types of annotations :

Servlet annotations example Documentation annotations: These are used to generate documentation of java code (jdk 1.0 and later)

Servlet annotations example Programming annotations: These annotations are given for the programming purpose.

 

Servlet annotations example syntax:

@<annotation_name>(element1,element2…)

 

create a servletPerformance wise annotations are good compared to XML configurations,but XML’s are good for modification.

Annotations can apply  in different levels.

Servlet annotations example Resource level : On the top of a class or interface declaration.
Servlet annotations example Method level :On the top of a method declaration.
Servlet annotations example Field level : on the top of instance variable declarations.

 

Servlet 3.0 comes with several annotations ,Few are given below.

@WebServlet : This annotation is used to declare the configuration of a servlet.

 Modifier and Type   Optional Element
 boolean  asyncSupported
 java.lang.String  displayName
 WebInitParam[]  initParams
 java.lang.String  largeIcon
 int  loadOnStartup
 java.lang.String  name
 java.lang.String  smallIcon
 java.lang.String[]  urlPatterns
 java.lang.String[]  value

 

@WebFilter : This annotation is used to declare a servlet filter

 Modifier and Type   Optional Element
 boolean  asyncSupported
 java.lang.String  description
 DispatcherType[]  dispatcherTypes
 java.lang.String  displayName
 java.lang.String  filterName
 WebInitParam[]  initParams
 java.lang.String  largeIcon
 java.lang.String[]  servletNames
 java.lang.String  smallIcon
 java.lang.String[]  urlPatterns
 java.lang.String[]  value

 

@WebInitParam :The annotation used to declare an initialization parameter on a Servlet or Filter, within a WebFilter or WebServlet annotation

 Modifier and Type Required Element
 java.lang.String  name
 java.lang.String  value

 

@WebListener :The annotation used to declare a listener for various types of event, in a given web application context.

 Modifier and Type Optional Element
 java.lang.String  value

 

Servlet annotations example Example for Servlet annotations :

In the below example , there is no web.xml file to integrate servlet.Because we used @WebServlet annotation for configuration.

Servlet annotations example index.jsp

Servlet annotations example welcomeServlet.java

Servlet annotations example Execution Result :

Enter the name and click send button.




Related Posts :
Kill or invalidate session using session id ( concurrent login )
Servlet Filter ( Request and Response filters in servlets ) example
URL rewriting in servlets ( Servlet session tracking )
Servlet Cookies ( Servlets session tracking with cookie )
Hidden form field in servlets ( Servlet session tracking )
Request attributes in servlet with an example ( Attribute scope )
Session attributes in servlet with example (Attribute scope)
ServletConext Attributes in servlet ( Attribute scope )
   [will not be published]



^ <