This week's book giveaway is in the Raspberry Pi forum.
We're giving away four copies of Getting started with Java on the Raspberry Pi and have Frank DelPorte on-line!
See this thread for details.
Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

SESSION TRACKING.

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PLEASE HELP ME IN UNDERSTANDING COOKIES.ALSO HTTPSESSION INTERFACE FUNDAS.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heck - there's a good example provided with Tomcat:
Just remember, cookies are nothing more than small files stored on the user's hard drive that have a certain expiration date. You can store up to 4k of string data in any cookie. If you encode your data properly, you can remember all sorts of things....userid, password, preferences, etc.
Always remember, the user can delete their cookies at any time or turn them off at the browser....therefore it is always good practice to check for this condition first.....
Tomcat and other apps use a single cookie to track the sessionid of the user and the cookie name is the same as the session id.

/* $Id: CookieExample.java,v 1.1.1.1 1999/10/09 00:19:59 duncan Exp $
*
*/
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Example servlet showing request headers
*
* @author James Duncan Davidson <duncan@eng.sun.com>
*/
public class CookieExample extends HttpServlet {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
String title = rb.getString("cookies.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body>");
// relative links
// XXX
// making these absolute till we work out the
// addition of a PathInfo issue

out.println("<a href=\"/examples/servlets/cookies.html\">");
out.println("<img src=\"/examples/images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"/examples/servlets/index.html\">");
out.println("<img src=\"/examples/images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");
out.println("<h3>" + title + "</h3>");
Cookie[] cookies = request.getCookies();
if (cookies.length > 0) {
out.println(rb.getString("cookies.cookies") + "<br>");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
out.print("Cookie Name: " + cookie.getName() + "<br>");
out.println(" Cookie Value: " + cookie.getValue() +
"<br><br>");
}
} else {
out.println(rb.getString("cookies.no-cookies"));
}
String cookieName = request.getParameter("cookiename");
String cookieValue = request.getParameter("cookievalue");
if (cookieName != null && cookieValue != null) {
Cookie cookie = new Cookie(cookieName, cookieValue);
response.addCookie(cookie);
out.println("<P>");
out.println(rb.getString("cookies.set") + "<br>");
out.print(rb.getString("cookies.name") + " " + cookieName +
"<br>");
out.print(rb.getString("cookies.value") + " " + cookieValue);
}

out.println("<P>");
out.println(rb.getString("cookies.make-cookie") + "<br>");
out.print("<form action=\"");
out.println("CookieExample\" method=POST>");
out.print(rb.getString("cookies.name") + " ");
out.println("<input type=text length=20 name=cookiename><br>");
out.print(rb.getString("cookies.value") + " ");
out.println("<input type=text length=20 name=cookievalue><br>");
out.println("<input type=submit></form>");


out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
 
Get me the mayor's office! I need to tell him about this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic