This article is continued from Servlet 3.0 (part-1). Here i will try to brief on programmatic adding of Servlet, Filters and Listeners to ServletContext object.
As per Servlet 3.0 Specs, any of these components (servlet, filter or listener) should be added during initialization of servletContext object, contextInitialized(ServletContextEvent sce) method of ServletContextListener API is a better place to add servlet and other components to context object.
A simple example to add a Servlet to ServletContext:
UserHomeServlet is a servlet which has to be registered to context programmatically.
Implement ServletContextListener to provide realization for contextInitialized() method
Similarly a Filter and Listener can be registered to context as below:
* Filter
* Listener
As per Servlet 3.0 Specs, any of these components (servlet, filter or listener) should be added during initialization of servletContext object, contextInitialized(ServletContextEvent sce) method of ServletContextListener API is a better place to add servlet and other components to context object.
A simple example to add a Servlet to ServletContext:
UserHomeServlet is a servlet which has to be registered to context programmatically.
public class UserHomeServlet extends HttpServlet { /** * Handles the HTTPGET
method. */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { RequestDispatcher rd = request.getRequestDispatcher("userHome.jsp"); rd.forward(request, response); } finally { out.close(); } } /** * Handles the HTTPPOST
method. */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Implement ServletContextListener to provide realization for contextInitialized() method
@WebListener public class CustServletContextListener implements ServletContextListener{ public void contextInitialized(ServletContextEvent sce) { System.out.println("init context method"); try{ //add UserHomeServlet to context. ServletRegistration userHome = sce.getServletContext().addServlet("userHome", UserHomeServlet.class); //provide url pattern for UserHomeServlet. userHome.addMapping("/userHome"); }catch(Exception e){ e.printStackTrace(); } } public void contextDestroyed(ServletContextEvent sce) { throw new UnsupportedOperationException("Not supported yet."); } }
Similarly a Filter and Listener can be registered to context as below:
* Filter
public void contextInitialized(ServletContextEvent sce) { try{ //add UserFilter to Context. FilterRegistration userFilter = sce.getServletContext().addFilter("userFilter", UserrequestFilter.class); //provide servlet name to filter. userFilter.addMappingForServletNames(null, true, "com.jb.UserHomeServlet"); }catch(Exception e){ e.printStackTrace(); } }
* Listener
public void contextInitialized(ServletContextEvent sce) { try{ //register UserRequestListener to context. sce.getServletContext().addListener(UserRequestListener.class); }catch(Exception e){ e.printStackTrace(); } }
No comments:
Post a Comment