<< Chapter < Page Chapter >> Page >

Create a new session ID cookie

Because cookies are sent to the browser using HTTP headers, they should be added to the response before you send any content.

If no cookies were submitted by the client with the request, this is interpreted by this servlet to be the beginning of the session. A new Cookie object is instantiated containing the session ID value created above along with the name of the cookie: sessionID .

Then the cookie is sent to the client's browser by calling the addCookie method on the outgoing HttpServletResponse object as shown in Listing 5 .

Listing 5 - Create a new session ID cookie.
if(cookies == null){ Cookie newCookie = new Cookie("sessionID",sessionID);response.addCookie(newCookie); }//end ifPrintWriter out = response.getWriter();

Listing 5 also gets an output stream using code that you have seen in earliermodules.

Create, name, and populate a new Cookie object

Listing 6 instantiates a Cookie object containing the field value submitted by the client and sends that cookie back to the browser for storage.

Listing 6 - Create, name, and populate a new Cookie object.
if(name != null){ String cookieName = "" + new Date().getTime();Cookie newCookie = new Cookie(cookieName, name); newCookie.setMaxAge(60);response.addCookie(newCookie); }//end if

Unique names for cookies

Unless delineated by path information, each cookie needs a unique name in addition to its value. Assuming that successive calls to this servlet willbe separated in time by at least one millisecond, unique names can be created by using the current date and time in milliseconds. That mechanism was used in Listing 6 to create unique cookie names. The getTime method of the Date class returns the date and time in milliseconds represented by a Date object. The Date object in Listing 6 encapsulates the current date and time. You can see those times represented in milliseconds in the list of cookies in Figure 1 .

On the other hand, a servlet that creates two or more Cookie objects could easily create more than one cookie during each one-millisecond interval. In thatcase, you should probably use something like the code in Listing 2 to create unique cookie names.

Familiar code

Listing 7 contains code that is very similar to code discussed in earlier modules, so I won't discuss it further here. The fragment is being included herefor continuity.

Listing 7 - Familiar code.
//Construct an html form and send it back to the client out.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

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