Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
34Activity
0 of .
Results for:
No results containing your search query
P. 1
Servlet 3

Servlet 3

Ratings:
(0)
|Views: 1,383|Likes:
Published by tlad

More info:

Published by: tlad on Apr 06, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

01/03/2013

pdf

text

original

 
Servlet 3.0 
Introduction
This article covers most of the important features available as part of 
Servlet 3.0 
 specification. Note that the
Servlet 3.0 
specification constantly keeps changing frequently with the reviews coming in and the features andthe API's mentioned in thisarticleis based on the specification that is available in JCP for public review as of December 2008. This article focuses on the
new set of annotations
introduced that can be used by developersrather than put the data in theconfiguration file, followed by the enhanced
Pluggability and the extensionsupport 
for adding third-party frameworks. The article is finally concluded by detailing about the
asynchronousexecution of processing 
and the usage for the same.
JSR 315 
talks about the Servlet 3.0 features. For attitional information please read the reference section of this article.
Ease of Development through Annotations
The configurationinformationabout a component in a typicalweb applicationis expressed in an external meta file. The information about web components such as Servlets, Servlet Filters are mentioned in the deploymentdescriptor, which is web.xml. Starting from
Servlet 3.0 
, it is also possible to specify the meta information about acomponent inthe definitionof a component itself, throughAnnotations. It doesn't mean the deployment descriptor is now gone, deployment descriptor in the form of web.xml is still there. In fact information specified inthe deployment descriptor takes precedence over the information specified through Annotations.The
Servlet 3.0 specification
also provides an option for instructing the Web Container, whether the container should process the annotations defined on the web components. The name of the element is
metadata-complete
and it is a child element of 
web-app
element. The
metadata-complete
element indicates whether themeta-data information available in the deployment descriptor is complete. So, if the value for the metadata-complete element is set to a value of true, then it means that the meta information found in the deploymentdescriptor is complete and eventually the annotations defined on the web components will be ignored by the
Servlet Container 
. If the value for 
metadata-complete
is set to false, then it means that the information in thedeployment descriptor is not complete and web components decorated with annotations, if any, should bescanned and processed by the Web Container.The following annotations are applicable starting from Servlet 3.0 specification,
@WebServlet
@WebServletContextListener 
@ServletFilter 
@InitParam
Web Servlet and InitParam Annotation
In this section, we will see the usage of 
@WebServlet 
and
@InitParam
using an example. Look at the followingcode,
SimpleServlet.java
 
 
package net.javabeat.servlet30.newfeatures;import javax.servlet.annotation.InitParam;import javax.servlet.annotation.WebServlet;@WebServlet(name = "SimpleServlet",urlPatterns = {"/simple"},initParams = {@InitParam(name = "param1", value = "value1"),@InitParam(name = "param2", value = "value2")})
 
publicclass SimpleServlet {}In the example, we have declared a class by name SimpleServlet and this class is not made to extend or implement any of the Servlet/HttpServlet types. Instead, to qualify this class as a Servlet class we haveannotated using
@WebServlet 
annotation. Note that the name of the servlet is SimpleServlet as specifiedthrough the
name
attribute. The attribute
urlPatterns
defines a set of url-patterns that can be used to invoke theServlet. The Servlet Container after scanning this class will generate the deployment descriptor which may looklike the following,
web.xml
 ...<servlet><servlet-name>SimpleServlet</servlet-name><servlet-class>net.javabeat.servlet30.newfeatures.SimpleServlet</servlet-class><init-param><param-name>param1</param-name><param-value>value1</param-value></init-param><init-param><param-name>param2</param-name><param-value>value2</param-value></init-param></servlet><servlet-mapping><url-pattern>/simple</url-pattern><servlet-name>SimpleServlet</servlet-name></servlet-mapping>...
Filter annotation
The
@Filter 
annotation defines a
Servlet Filter component 
for a web application. A filter is typically used tointercept a web request for performing any of the pre-processing operations well before reaching the actualservlet component. Let us see the definition of a filter component using
Servlet 3.0 
approach,
SimpleFilter.java
 package net.javabeat.servlet30.newfeatures;import javax.servlet.annotation.InitParam;import javax.servlet.annotation.ServletFilter;@ServletFilter(filterName = "SimpleFilter",urlPatterns = "/simple",initParams ={@InitParam(name = "param1", value = "value1"),@InitParam(name = "param2", value = "value2")}
 
)public class SimpleFilter {}Again, to support backward compatibility, the annotation information in the above class will be transformed asinformation in the deployment descriptor by the Servlet Container and the deployment descriptor might look likethis,
web.xml
 ...<filter><filter-name>SimpleFilter</filter-name><filter-class>net.javabeat.servlet30.newfeatures.SimpleFilter</filter-class><init-param><param-name>param1</param-name><param-value>value1</param-value></init-param><init-param><param-name>param2</param-name><param-value>value2</param-value></init-param></filter><filter-mapping><filter-name>SimpleFilter</filter-name><url-pattern>/simple</url-pattern><filter-mapping>...
Servlet Context Listener annotation
The
Servlet Context Listener 
is used to receive events whenever the servlet context is created and destroyed bythe Web Container. Let us see an usage of this annotation,
SimpleServletContextListener.java
 package net.javabeat.servlet30.newfeatures;import javax.servlet.ServletContextEvent;import javax.servlet.annotation.WebServletContextListener;@WebServletContextListener()public class SimpleServletContextListener {public void contextInitialized(ServletContextEvent event){}public void contextDestroyed(ServletContextEvent event){}

Activity (34)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Ankit Jain liked this
ngtanhoang liked this
lsrirag liked this
ALi liked this
bharat_mania liked this
akkireddy8406 liked this
athirai liked this
tarunkusum liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->