<< Chapter < Page Chapter >> Page >

The parameter values in milliseconds are first converted from String to long . Each of the long values is then used to instantiate a new Date object, which is displayed in the format shown in Figure 2 .

Listing 8 - Display date and time history.
if(items != null){ for(int i = 0; i<items.length; i++){ long millis = Long.parseLong(items[i]); out.println("" + new Date(millis) + "<br/>"); }//end for loop}//end if
(Hopefully you already understand the relationship between the overridden toString method of the Date class and the format resulting from passing a Date object's reference to the println method. If not, see Ap0005: Preface to OOP Self-Assessment and the modules following that one.)

Display the current date and time

Listing 4 obtained the current date and time in milliseconds and saved it in a variable named theDate . Listing 9 uses that value to instantiate and display a new Date object reflecting the current date and time.

Listing 9 - Display the current date and time.
out.println("" + new Date(theDate) + "<br/>"); out.println("</body></html>");//finish HTML page

Listing 9 also creates the HTML code necessary to finish the HTML page.

The JSP program

I will also discuss the JSP program named Java4560b.jsp in fragments. A complete listing of the program is provided in Listing 17 .

Interesting code fragments

Import necessary packages

Listing 10 shows the necessary import directives plus the beginning of the HTML page.

Listing 10 - Import necessary packages.
<%@ page import='java.util.*,java.io.*' %><html><head><title>Java4560b</title></head><body>

There's nothing new in Listing 10 with the possible exception of the formatof the import directives. The format is different from the format used in regular Java code. (See Listing 1 .)

Some packages are imported automatically

Recall from Java4307: Servlets and JSP that it is not necessary to import javax.servlet and javax.servlet.http . Those two packages are automatically imported by the JSP container on theserver. No page directive import settings are required for using the classes defined in these packages.

Get data from the old parameter string

Listing 11 shows the beginning of a scriptlet containing pure Java code.

Listing 11 - Get data from the old parameter string.
<% String parameters = "?";String[] items = request.getParameterValues("item");

The code in Listing 11 declares and initializes a String that will be used later to construct a new parameter string.

Listing 11 also gets and saves the values of all of the parameters from the old parameter string named "item" .

Construct a new parameter string

Listing 12 constructs a new parameter string containing the names and values of all the parameters named "item" in the old parameter string. (A parameter for the current date and time will be concatenated later.)

Listing 12 - Construct a new parameter string.

Missing Figure

Concatenate current date and time to the parameter string

Listing 13 gets the current date and time in milliseconds andconcatenates it to the new parameter string with the name "item" .

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