Dealing with Threads Created by Forms with Multiple Users

 

Introduction.  We've done some form processing, which will continue to be a major issue in this course.  But, so far we haven't addressed issues of how a JSP works when it is accessed by several different users.

When a JSP is requested for the first time, one instance of the JSP is created and loaded into memory.  In addition, a thread is started that executes the Java code in the JSP.  For each subsequent request for the JSP another thread is started that can access the initial instance of the JSP.

When you code variables in scriptlets, each thread gets its own copy of each variable which is usually what you want and automatically keeps things thread safe.

But in some situations you may want to have variables that can be accessed by all threads that are accessing the JSP.  In other situations you may end up developing code that isn't thread safe using what you think are reasonable approaches.

These are the sorts of issues we start to address in this webpage and the next.

First you need to develop the HTML form page thread_form.html.

 

<html>
<head>
<title>Form for Testing Threads</title>
</head>

<body bgcolor="003355" text="cccccc">
<form method="post" action="thread_process.jsp">
<table width=500 align="center">
<tr>
<td>First Name:
</td>
<td><input type="text" name="txt_first_name" size=20>
</td>
</tr>
<tr>
<td>Last Name:
</td>
<td><input type="text" name="txt_last_name" size=20>
</td>
</tr>
<tr>
<td>Address:
</td>
<td><input type="text" name="txt_address1" size = 40>
</td>
</tr>
<tr>
<td>
</td>
<td><input type="text" name="txt_address2" size = 40>
</td>
</tr>
<tr>
<td>City:
</td>
<td><input type="text" name="txt_city" size=20>
</td>
</tr>
<tr>
<td>State:
</td>
<td><input type="text" name="txt_state" size=20>
</td>
</tr>
<tr>
<td>Zip Code:
</td>
<td><input type="text" name="txt_zip_code" size=12>
</td>
</tr>
<tr>
<td>
</td>
<td><input type="submit" name="cmd_submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

 

After uploading and running this in your account on the server you should see something like the following when you go to the page.

 

 

After entering information you want to process it.  This will be done with the following script which you should call thread_process.jsp.  Notice how this is the name of the action in the <form> tag.

 

<html>
<head>
<title>Processing Threads</title>
</head>

<body bgcolor="003355" text="cccccc">
<%!
// holders for the various portions of the form info
// these are actually instance variable rather than local variables
// that exist uniquely for each thread
String first_name;
String last_name;
String address1;
String address2;
String city;
String state;
String zip;
int number_threads;
%>

<%
// define a local variable for number of threads
int local_number_threads = 1;
// obtain the posted form information
first_name = request.getParameter("txt_first_name");
last_name = request.getParameter("txt_last_name");
address1 = request.getParameter("txt_address1");
address2 = request.getParameter("txt_address2");
city = request.getParameter("txt_city");
state = request.getParameter("txt_state");
zip = request.getParameter("txt_zip_code");
number_threads = number_threads + 1;
local_number_threads = local_number_threads + 1;

formatNameAndAddress(out);
out.println("<br><font size=4>Local thread count is " + local_number_threads + "</font>");
%>

 

This is almost the simplest possible processing script.  But it has its problems.  Even just two people accessing it at the same time could conflict and result in overwrites and other problems.

When processing this information you should see something like the following.

 

 

Notice how the number of threads is incremented by each user which emphasizes how all of our variables are actually instance variables rather than local variables.

The next webpage will discuss ways to do this better.