Java Reference
In-Depth Information
The Cookie class encapsulates persistent cookies as defined by RFC 2109. The prototype for
the Cookie 's constructor takes a String representing the unique name of the cookie and a
String representing the value of the cookie, and it is listed as follows:
public Cookie(String name, String value)
The Cookie class also provides accessors used to get and set the values of the cookie. Listing
5.2 contains an example of using cookies to perform session handling.
L ISTING 5.2
CookieServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CookieServlet extends HttpServlet {
//Initialize global variables
public void init(ServletConfig config)
throws ServletException {
super.init(config);
}
private String getCurrentUser(String value) {
String userName = new String(“”);
// This would normally be a Select from a database or
// other storage area.
if ( value.equals(“564XX892”) ) {
userName = new String(“Bob”);
}
return userName;
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
5
// Get the list of Cookies stored in the request
Cookie[] cookieList = request.getCookies();
String user = null;
 
Search WWH ::




Custom Search