The specification focuses on following feature:
• Ease of development
• Plug-ability and extensibility
• Asynchronous support
• Security enhancement
In this article(Servlet 3.0 part-1), I would keep the focus on defining Servlet, Filters and Listeners using annotations and will try to put my thougts on programmatically adding Servlet, Filters and Listeners to ServletContext object in Servlet 3.0 (part-2).
According to specs 3.0, deployment descriptor is optional. Servlets and other components can be configured using annotations.
All annotations used in Servlet 3.0 can be found under package ‘javax.servlet.annotation’.
For comparison, i have put code snippets for writing servlet and its components in old Servlet 2.5 API and same components in new Servlet 3.0. In 2.5 Web container will initialize the components only if you configure the details in deployment descriptor.
• Servlet:
public class ServletA extends HttpServlet { //A GET method public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ //code to process request } Deployment Descriptor (web.xml) EntryServletA com.jb.ServletA ServletA /ServletA
Here is a much simplified code for servlet written in Servlet 3.0
@WebServlet(name="ServletA", urlPatterns={"/ServletA"}) public class ServletA extends HttpServlet { @ Override public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ //code to process request. } }
Deployment Descriptor (web.xml) Entry
Optional
• Filters
public class FilterA implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //code snippets … } Deployment Descriptor (web.xml) EntryFilterA com.jb.FilterA FilterA /ServletA
Configuring Filter in Servlet 3.0
@WebFilter(filterName="FilterA", urlPatterns={"/ServletA"}) public class FilterA implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //filter code here… } }
Deployment Descriptor (web.xml) Entry
Optional
• Listeners
public class ListenerA implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { throw new UnsupportedOperationException("Not supported yet."); } } Deployment Descriptor (web.xml) Entrycom.jb.ListenerA
Configuring Listeners in Servlet 3.0
@WebListener() public class ListenerA implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { throw new UnsupportedOperationException("Not supported yet."); } }
Deployment Descriptor (web.xml) Entry
Optional
• Passing Init Params in Servlet 3.0 has a simple syntax too.
@WebServlet(name="ServletA", urlPatterns={"/ServletA"}, initParams={@WebInitParam(name="param1", value="paramvalue")} // passing init params to servlet ServletA ) public class ServletA extends HttpServlet { //Servlet code … }
Continued in Servlet 3.0 (part-2)...
No comments:
Post a Comment