[ Team LiB ] 11.5 Using Scopes from

[ Team LiB ] 11.5 Using Scopes from Servlets Chapter 3 discussed the various scopes in which beans can live. Although these scopes are usually accessed by setting the scope field in a jsp:useBean tag, they can also be accessed through scriptlets or through code in a servlet. Listing 11.6 shows a servlet with a counter, which works much like the JSP in Listing 3.8 did. Listing 11.6 A servlet with a page counter package com.awl.jspblog.ch11; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Counter1 extends HttpServlet { private int count; public void init(ServletConfig sc) throws ServletException { super.init(sc); count = 0; } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException { handle(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException { handle(req,res); } public void handle(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException { res.setStatus(res.SC_OK); res.setContentType(“text/html”); PrintWriter out = res.getWriter(); out.println(““); out.println(“A Counter“); out.println(““); out.println(“This page has been accessed “); out.println(count); out.println(” times”); out.println(““); out.println(““); Page 213
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

[ Team LiB ] 11.4 Forwarding and Including

[ Team LiB ] 11.4 Forwarding and Including Requests Many examples throughout this blog have sent a user to another page, using a technique very different from the redirects. The jsp:forward tag is sort of a “server-side” redirect, as it instructs the server to generate a different page rather than tells the browser to load a new URL. This is related to the jsp:include tag, which includes the body of one JSP, HTML page, or servlet in another. There is no corresponding way to do this on the client side, at least not one that works on all browsers. Both of these tags are handled internally by the RequestDispatcher class, which, as the name implies, can dispatch a request to another resource in the system. It does this through two methods called, appropriately enough, forward() and include(). Both of these methods take as arguments the request and response objects that the calling servlet was passed. As might be expected, these objects will end up getting passed to the target servlet’s service() method. Listing 4.14 showed a JSP that used a jsp:forward tag and input from the user to send the user one of three pages. Listing 11.5 shows how a servlet would accomplish the same thing. Listing 11.5 A servlet that forwards requests package com.awl.jspblog.ch11; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class DispatchServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException { ServletContext sc = getServletContext(); RequestDispatcher rd; String which = req.getParameter(“which”); if(which != null) { if(which.equals(“red”)) { rd = sc.getRequestDispatcher(“/chapter11/red.jsp”); rd.forward(req,res); } else if(which.equals(“green”)) { rd = sc.getRequestDispatcher(“/chapter11/green.jsp”); rd.forward(req,res); } else if(which.equals(“blue”)) { rd = sc.getRequestDispatcher(“/chapter11/blue.jsp”); rd.forward(req,res); } else { res.sendError(res.SC_INTERNAL_SERVER_ERROR, “A page was requested that does exist!”); } } else { res.sendError(res.SC_INTERNAL_SERVER_ERROR, “No destination page was specified!”); } } } Page 211
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services

[ Team LiB ] 11.3 Servlet Events Often,

[ Team LiB ] 11.3 Servlet Events Often, developers may wish to take certain special actions at various points during the life cycle of either a servlet or the application as a whole. Some of these actions can be handled by simply adding code to the appropriate life-cycle method, but other methods are not available to the general programmer. One common case is session handling. If an application could determine when a session was being expired, it would be possible to move data from the session into a database before it disappeared forever. Likewise, knowledge of when a session was being created would make it possible to move data from the database into the session. The result would be sessions that are effectively immortal, providing a seamless experience for the user. This could be achieved in many ways, but a natural mechanism is already provided by the JavaBean specification by way of events and listeners, as discussed in the preceding chapter. The idea is that the servlet engine will fire off an event representing various activities, and programmers can build listeners to capture and act on these events. Conceptually, this works just like any other bean event; it is simply a matter of implementing the right listener interfaces and working with the corresponding events. An outline of the session backup listener is shown in Listing 11.4. Listing 11.4 The outline of a session listener package com.awl.jspblog.ch11; import javax.servlet.http.*; public class BackupListenerimplements HttpSessionAttributeListener { public voidsessionCreated(HttpSessionEvent event) { HttpSession session = event.getSession(); // … load the session from the database … } public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); // … back up the session to the database … } public void attributeAdded(HttpSessionBindingEvent event) { // … It may not be necessary to do anything for this // event, but the interface requires it be provided } public void attributeRemoved(HttpSessionBindingEvent event) { // … It may not be necessary to do anything Page 209
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services