<< Chapter < Page Chapter >> Page >
Housekeeping material
  • Module name: Java4570: Session Tracking using Cookies
  • File: Java4570.htm
  • Revised: 02/07/16
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Complete program listing

Listing 10 - The servlet program named Java4570a.java.

/*File Java4570a.java, Copyright 1999, R.G.Baldwin Rev 12/24/13The purpose of this program is to illustrate session tracking through the use of cookies.Each time the servlet is called, it displays an html form on the client screen. The form contains:An input field for submitting a name A submit buttonA list of previously submitted namesThe first time the servlet is called, it creates a unique session ID and stores it in a cookie on the browser.Each time the servlet is called, it creates a cookie containing the name submitted by the user and stores iton the browser. *************************************************************/import java.io.*; import java.util.*;import javax.servlet.*; import javax.servlet.http.*;public class Java4570a extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{//Get a unique ID to be used to construct a session IDString uid = new java.rmi.server.UID().toString(); //Encode any special characters that may be in the uid// to construct the session ID String sessionID = java.net.URLEncoder.encode(uid);//Get and save the cookies submitted with the request Cookie[]cookies = request.getCookies();//Get the submitted name for the current get request String name = request.getParameter("firstName");//Establish the type of output response.setContentType("text/html");//If no cookies were submitted with the request,// create and add a cookie containing the session ID if(cookies == null){Cookie newCookie = new Cookie("sessionID",sessionID); response.addCookie(newCookie);}//end if//Get an output stream PrintWriter out = response.getWriter();if(name != null){ String cookieName = "" + new Date().getTime();Cookie newCookie = new Cookie(cookieName, name); newCookie.setMaxAge(60);response.addCookie(newCookie); }//end if//Construct an html form and send it back to the clientout.println("<html>"); out.println("<head><title>Java4570a</title></head>"); out.println("<body>");//Substitute the name of your server in // the following statement.out.println("<form method='get' action=" + "'http://localhost:8080/Java4570a'>"); out.println("<p>Enter a name and press the button</p>"); out.println("<p>Name:<input type='text' name=" + "'firstName'/></p>"); out.println("<input type='submit' value=" + "'Submit Name'/>"); out.println("<br/><br/>Your session ID and list of names is:<br/>"); if(name == null){out.println("Empty<br/>"); }//end if//Display the session ID and the values of the// cookies that have been saved. if(cookies != null){for(int i = 0; i<cookies.length; i++){ out.println(cookies[i].getName() + ":" + cookies[i].getValue() + "<br/>"); }//end for loop}//end if //Display name submitted with current get requestif(name != null){ out.println(name + "<br/>"); }//end ifout.println("</form></body></html>");}//end doGet() }//end class Java4570a

Listing 11 - The JSP program named Java4570b.jsp.

<%@ page import='java.util.*,java.io.*' %><html><head><title>Java4570b</title></head><body><% String uid = new java.rmi.server.UID().toString();String sessionID = java.net.URLEncoder.encode(uid); Cookie[]cookies = request.getCookies(); String name = request.getParameter("firstName");response.setContentType("text/html");if(cookies == null){Cookie newCookie = new Cookie("sessionID",sessionID); response.addCookie(newCookie);}//end ifif(name != null){ String cookieName = "" + new Date().getTime();Cookie newCookie = new Cookie(cookieName, name); newCookie.setMaxAge(60);response.addCookie(newCookie); }//end if%><form method='get' action='http://localhost:8080/Java4570b.jsp'><p>Enter a name and press the button</p><p>Name:<input type='text' name='firstName'/></p><input type='submit' value='Submit Name'/><br/><br/>Your session ID and list of names is:<br/><% if(name == null){out.println("Empty<br/>"); }//end ifif(cookies != null){ for(int i = 0; i<cookies.length; i++){ out.println(cookies[i].getName() + ":" + cookies[i].getValue() + "<br/>"); }//end for loop}//end if if(name != null){out.println(name + "<br/>"); }//end if%></form></body></html>

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask