This page is optimized for mobile devices, if you would prefer the desktop version just click here

21.6 Java4560: session tracking using url rewriting  (Page 4/6)

Listing 1 - Beginning of the doGet method.
import java.io.*; import java.util.*;import javax.servlet.*; import javax.servlet.http.*;public class Java4560a extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{

Get and save URL parameters named item

An HTTP URL can have multiple parameters with the same name. In this case, all of the parameters are named item . Listing 2 gets the values stored in all of the parameters named item and saves them in a String array named items .

(Each time the servlet is called, the size of the list of parameters increases by one.)

The fragment also instantiates and initializes a String object used later to construct a new parameter string for the URL.

Listing 2 - Get and save URL parameters named item.
String parameters = "?"; String[]items = request.getParameterValues("item");

Construct the new parameter string

Listing 3 uses the parameter values from the old parameter string toconstruct a new parameter string. The new parameter string will later be concatenated to the URL for the servlet.

Listing 3 - Construct the new parameter string.

Concatenate current date and time to new parameter string

Listing 4 gets the current date and time in milliseconds and saves it in a long variable named theDate . Then it concatenates the current date and time in milliseconds onto the newparameter string that was constructed in Listing 3 .

Listing 4 - Concatenate current date and time to new parameter string.
long theDate = new Date().getTime(); parameters = parameters + "item=" + theDate;

Begin creation of the HTML page

Listing 5 begins the creation of the output HTML page. You have seen code like this before, so I won't discuss it further at this point.

Listing 5 - Begin creation of the HTML page.
//Establish the type of output response.setContentType("text/html");//Get an output streamPrintWriter out = response.getWriter();//Construct an HTML page and send it back to the client out.println("<html>"); out.println("<head><title>Java4560a</title></head>"); out.println("<body>");

Construct the new hyperlink

Listing 6 constructs the hyperlink containing the URL for the servlet. Note that the hyperlink references a URL that includes the parameter stringconstructed above.

If you compile and run this servlet, you may need to substitute the name of your server in place of my server.

Listing 6 - Construct the hyperlink.
out.println("<a href='http://localhost:8080/Java4560a" + parameters + "'>Click Here</a>")

Introductory text

Listing 7 provides a line of introductory text for the list of dates and times to be displayed on the screen.

Listing 7 - Introductory text.
out.println("<br/><br/>Your list of times is:<br/>");

Display date and time history

Listing 2 retrieved all of the data in the incoming URL parameters and saved that data in a String array named items .

If that array contains data, Listing 8 displays the date and time for each element in the array.

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.