Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Wednesday, December 12, 2007

How to Use cookie to save session data in java

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShoppingCartViewerCookie extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();

String sessionid = null;
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("sessionid")) {
sessionid = cookies[i].getValue();
break;
}
}
}

// If the session ID wasn't sent, generate one.
// Then be sure to send it to the client with the response.
if (sessionid == null) {
sessionid = generateSessionId();
Cookie c = new Cookie("sessionid", sessionid);
res.addCookie(c);
}

out.println("Current Shopping Cart Items");
out.println("");

// Cart items are associated with the session ID
String[] items = getItemsFromCart(sessionid);

// Print the current cart items.
out.println("You currently have the following items in your cart:
");
if (items == null) {
out.println("None");
} else {
out.println("
    ");
    for (int i = 0; i < items.length; i++) {
    out.println("
  • " + items[i]);
    }
    out.println("
");
}

// Ask if they want to add more items or check out.
out.println("
");
out.println("Would you like to
");
out.println("");
out.println("");
out.println("
");

// Offer a help page.
out.println("For help, click + "?topic=ShoppingCartViewerCookie\">here");

out.println("");
}

private static String generateSessionId() throws UnsupportedEncodingException {
String uid = new java.rmi.server.UID().toString(); // guaranteed unique
return URLEncoder.encode(uid,"UTF-8"); // encode any special chars
}

private static String[] getItemsFromCart(String sessionid) {
return new String[]{"a","b"};
}
}

No comments: