Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

New Features in Java EE 6 : Page 3

Get a sneak preview of the new modularity and lightweight features in Java EE 6.


advertisement

Servlet 3.0

Servlet is one of the robust technologies in the Java EE APIs. Since its introduction, apart from the inclusion of filters and web application events, the Servlet specification has not undergone any major changes—until Servlet 3.0, which will introduce significant changes to the API.

Some of the major changes Servlet 3.0 will introduce are:

  • Support for Annotations: Instead of making an entry in a deployment descriptor (web.xml), developers can use annotations to mark a servlet. The annotation @WebServlet is used to mark a class that extends HttpServlet as a servlet in a web application. The @WebFilter annotation is used to mark a class that implements Filter as a filter in the web application. Similarly, developers can use annotations like @WebInitParam and @WebListener to specify an init parameter and a listener in a web application, respectively.
    With the introduction of annotations, the deployment descriptor web.xml becomes optional. Apart from these annotations, developers can continue to use other annotations related to security and dependency injection from the servlet or Filter.


    @WebServlet("/stockquote") public class StockQuoteServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { … } public void doPost(HttpServletRequest request, HttpServletResponse response) { ... } ... } @WebServlet(name="StockQuoteServlet", urlPatterns={"/stockquote","/getQuote"}) public class StockQuoteServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { ... } public void doPost(HttpServletRequest request, HttpServletResponse response) { ... } ... } @WebFilter("/login") public class LoginFilter implements Filter { public void doFilter(HttpServletRequest request, HttpServlerResponse response, FilterChain filterChain) { ... } ... }

  • Web fragments: Web fragments are meant to provide modularity. They provide logical partitioning of the deployment descriptor web.xml so that frameworks such as Struts and JavaServer Faces (JSF) can have their own piece of information added in the JAR file, and the developer doesn't have to edit web.xml. Web fragments are identified by using web-fragment as the root element.
    Developers can use all the elements in the deployment descriptor. The only condition is that the root element must be <web-fragment> and hence have the name web-fragment.xml. This element is typically placed in the WEB-INF\lib folder. Any JAR file placed in the WEB-INF\lib folder can include the web fragment, and it should have the structure META-INF\web-fragment.xml:

    <web-fragment> <servlet> <servlet-name>StockQuoteServlet </servlet-name> <servlet-class> com.stock.StockQuoteServlet</servlet-class> </servlet> <listener> <listener-class> com.stock.listener.StockAddedListener </listener-class> </listener> </web-fragment>

    When an application has multiple web fragments, the order of execution can be resolved using absolute ordering or relative ordering. In both cases, XML tags are used to identify the order. Absolute ordering is specified using <absolute-ordering> in web.xml and relative ordering is specified using <ordering> in web-fragment.xml.
    When the element <metadata-complete> is not mentioned or set to false in the deployment descriptor, the container skips processing web.xml and processes annotations and web fragments. If <metadata-element> is set to true, then the container skips processing annotations and web fragments and processes based on the information available in web.xml, because this takes precedence.
  • Asynchronous processing: Servlets allow asynchronous processing in Java EE 6 to support AJAX. A servlet often has to wait for a response from a resource such as a database or a message connection. Asynchronous processing avoids the blocking request so that the thread can return and perform some other operation. Developers can mark servlets as asynchronous by setting the value true to the asyncSupported attribute of the @WebServlet or @WebFilter annotation. In addition to the annotation, some new APIs such as AsyncContext have been introduced, and methods such as startAsync have been added to the ServletRequest and ServletResponse classes.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap