Powered By Blogger

Tuesday, May 11, 2010

Interfaces

Friday, March 12, 2010

Servlet 3.0 in Java EE6

Java Servlet 3.0 API – JSR 315

Sun Microsystems released Java EE 6 specification and GlassFish v3 , THE Reference Implementation of Java EE6 on December 10th 2009

The list of supported technologies includes, but is not limited, by

  • Servlet 3.0
  • JSF 2.0
  • WebBeans
  • CDI (Contexts and Dependency Injection)
  • Bean Validation
  • EJB 3.1
  • JPA 2.0
  • JAX-RS

We will go through about one of the feature of javaee6, which is Servlet 3.0 . Again in Servlet 3.0(Java ee6) lots of features have been implemented but we will discuss only few which are quite interesting.

1)web.xml optional now

One of the very interesting feature/improvements is using Annotations instead of web.xml . No more complicated web.xml files and writing Servlet is comparatively less complicated now. Given below example Illustrates the same

Servlet 2.5 Example

In Servlet 2.5 , along with Servlet class ,even web.xml is required which is mandatory.

MyServlet.java

----------------------

package com.foo;

public class MyServlet extends

HttpServlet {

public void

doGet(HttpServletRequest

req,HttpServletResponse res)

{

...

}

...

}


web.xml

---------

MyServlet

com.foo.MyServlet

MyServlet

Servlet 3.0 Example

Now Let us see the same servlet

MyServlet.java

---------------------

package com.foo;

@WebServlet(name=”MyServlet”, urlPattern=”/myApp/*”)

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest req,

HttpServletResponse res)

{

...

}


Now comparing examples of 2.5 and 3.0 , we could see that web.xml is optional and it is very

easy to code now.


Some more on Annotations

-------------------------------

Annotations to declare Servlets, Filters, Listeners

and Security constraints

@WebServlet – Defines a Servlet

@WebFilter – Defines a Filter

@WebListener – Defines a listener

@WebInitParam – Defines an init param

@ServletSecurity – security constraints

@MultipartConfig – file upload


2)Sharing JSP's and Static pages


From Servlet 3.0 onwards , static pages and JSP's are no longer confined to document root of web application


These can be placed under

WEB-INF/lib/[*.jar]/META-INF/resources

and later add this *.jar in classpath.


But one thing to note here is that , resources in document root take precedence over those bundled in JAR files


3)Asynchronous support

One of the other interesting/significant feature in Servlet3.0 is the asynchronous support.

With this enhancement the servlet no longer has to wait for the response from resource such

as any database operation or webservice operation, its thread can continue its processing . In other words servlet thread is not blocked .

Prior to 3.0 this was achieved in a proprietary way by using API on top of Servlet 2.5 API .

But now standard asynchronous support has been added to the Servlet API.