Java Reference
In-Depth Information
Sessions can also be terminated explicitly, as shown in Example 18-8, a listing of
Logout.java . This is the Logout servlet that is invoked from portal.jsp when the
user clicks on the Logout button. The servlet ends the session by calling the
invalidate() method of the HttpSession object. Then it reads the page request
parameter and redirects the user's browser to that page. In the portal.jsp example,
this page parameter was set to point the browser back to portal.jsp . Since the ses-
sion was invalidated, however, the new request for the portal page is the first
request of a new session, and the portal page forwards it to the login.jsp page.
Example 18−8: Logout.java
package com.davidflanagan.examples.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This simple servlet ends the current session, and redirects the user's
* browser to a URL specified by the "page" request parameter. It should be
* suitable for use by any web application that requires the user to log in.
**/
public class Logout extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException
{
// Destroy the user's session
request.getSession().invalidate();
// Figure out what to display next
String nextpage = request.getParameter("page");
// And redirect the user's browser to that page
response.sendRedirect(nextpage);
}
// doPost just invokes doGet
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws java.io.IOException
{
doGet(request, response);
}
}
Custom Tags
The login.jsp and portal.jsp files used the custom <decor:box> tag for drawing col-
ored boxes with a border and optional title. The implementation of this tag con-
sists of two parts. The first part is DecorBox.java , shown in Example 18-9, which
contains the Java code that implements the tag. The second is WEB-INF/tlds/
decor_0_1.tld , shown in Example 18-10, which is a tag library descriptor (TLD) file
that describes the Decor tag library, the tags it contains, and the attributes sup-
ported by each tag. There is also one additional piece of the tag library puzzle: a
mapping from the URI that uniquely names the tag library to the local copy of the
TLD file. This mapping is part of the WEB-INF/web.xml deployment file for the
web application; we'll see it in Example 18-11.
Search WWH ::




Custom Search