Java Reference
In-Depth Information
L ISTING 5.2
Continued
String responseString = null;
if ( cookieList != null ) {
// Cookies found, let's get the session id
for ( int x = 0; x < cookieList.length; x++ ) {
String name = cookieList[x].getName();
if ( name.equals(“session_id”) ) {
// Get the user based on the session id
user = getCurrentUser(cookieList[x].getValue());
break;
}
}
}
if ( user == null ) {
// Let's create a cookie that represents a unique
// session id.
response.addCookie(new Cookie(“session_id”, “564XX892”));
responseString = new String(“Welcome to our site, “ +
“we have created a session for you.”);
}
else {
responseString = new String(“Hello : “ + user);
}
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<html>”);
out.println(“<head><title>CookieServlet</title></head>”);
out.println(“<body>”);
out.println(responseString);
out.println(“</body></html>”);
out.close();
}
Search WWH ::




Custom Search