<< 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.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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