<< Chapter < Page Chapter >> Page >

Also, if the user exits and then restarts the browser and calls the servlet, the cookies that have not yet expired will continue to exist andwill be displayed when the server responds to the request. Thus, a session being tracked by cookies with a long maximum age can persist over long periods oftime, even when the computer has been shut down and restarted. (Many web sites will remember you on the same computer using the same browser over days,months, and even years.)

Interesting code fragments

Beginning of the doGet method

Listing 1 shows the beginning of the controlling class and the beginning of the doGet method. You have seen code like this before, so I won't discuss it further in this module.

Listing 1 - Beginning of the doGetmethod.
public class Java4570a extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{

Construct a unique session ID

Listing 2 constructs a unique session ID that is later written into a cookie on the browser.

Listing 2 - Construct a unique session ID.
String uid = new java.rmi.server.UID().toString(); String sessionID = java.net.URLEncoder.encode(uid);

A UID object

The first step is to get a String representation of a UID object. According to the documentation, the UID class is an "Abstraction for creating identifiers that are unique with respect to the host on which it is generated."

Some cleanup is required

The second step is to call the encode method of the URLEncoder class to convert the string into a MIME format called the "x-www-form-urlencoded" format. This ensures that the identifier can be reliably transmitted between the server and the client andthat it will contain only those characters that are acceptable for saving in a cookie (see documentation on the setValue method of the Cookie class for more information) .

To convert the String , each character is examined and modified as follows:

  • The ASCII characters 'a' through 'z', 'A' through 'Z', and '0' through '9' remain the same.
  • The space character is converted into a plus sign '+'.
  • All other characters are converted into the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the lower 8-bits ofthe character.

A typical session identifier created by this process might appear as follows:

1cce64%3Ad69878ccf0%3A-7ff9

Get all of the cookies into an array of type Cookie

Listing 3 calls the getCookies method of the incoming HttpServletRequest object to get and save the cookies submitted by the browser. The values of the cookies will be displayed later.

Listing 3 - Get all of the cookies into an array of type Cookie.
Cookie[] cookies = request.getCookies();

Get and save submitted value

As in the sample programs in earlier modules, Listing 4 gets and saves the field value submitted by the client.

Listing 4 - Get and save submitted value.
String name = request.getParameter("firstName"); response.setContentType("text/html");

Listing 4 also establishes the type of output.

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