<< Chapter < Page Chapter >> Page >

A reference to the new output stream is also saved in the local variable named out so that it will be available during the remainder of the doGet method. This is what happens at the beginning of a new session with a specific browser.

Listing 3 - Get an output stream.
response.setContentType("text/html");PrintWriter out = (PrintWriter)session.getValue("out"); if(out == null){//First request from this client out = response.getWriter();session.putValue("out",out); }//end if//Create HTML page headerout.println("<html>"); out.println("<head><title>Java4580a</title></head>"); out.println("<body>");

An output stream already exists

If the call to the getValue method in Listing 3 returns an output stream, a reference to the output stream is saved in the local variable named out . This is what happens in the second and subsequent calls to the servlet during an ongoing session with a specific browser.

Create the HTML header text

Once the output stream is available, the code in Listing 3 uses it to create the normal header text on the outgoing HTML page.

A hit counter

Listing 4 instantiates a hit counter object and stores it in the session object under the name " counter ". If the session doesn't have a counter , one is created and its value is initialized to 1. If the session already has a counter , it is incremented by 1. Then the new or incremented counter is put in the session, replacing the one that was already there.

Listing 4 - A hit counter.
Integer cnt = (Integer)session.getValue("counter"); if(cnt == null) cnt = new Integer(1);else cnt = new Integer(cnt.intValue() + 1); session.putValue("counter",cnt);

Note that because an object of the Integer class is immutable, the only way to increment the counter is to create a new Integer object to replace the existing one.

The putValue and getValue methods

Listing 4 also illustrates the putValue and getValue methods. These methods are used to store and retrieve objects from the session object. The putValue() method requires two parameters:

  • an object to be stored
  • the String name under which the object is to be stored

The getValue method requires the String name of the object to retrieve.

Insert a Date object into the session

As shown in Listing 5 , each time the servlet is called, a new Date object containing the current date and time is instantiated and stored in thesession object.

Listing 5 - Insert a Date object into the session.
Date theDate = new Date(); long millis = theDate.getTime();String strMillis = "" + millis; session.putValue(strMillis,theDate);

Each Date object is stored under a name created by converting the current date and time in milliseconds to a String . Thus, the amount of data stored in the session object increases with each callto the servlet.

(Note that if the servlet is called twice by the same client within one millisecond, this naming scheme will fail due to duplicate names being created and put into thesession object.)

An object of type MyClass

When the value of the hit counter is 1, an object of type MyClass is instantiated and stored in the session object under the name MyClassObj . This is shown in Listing 6 . Note in particular that a reference to the output stream object is passed to theconstructor for the MyClass object. You will see why later.

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