Dealing with Threads Created by Forms with Multiple Users
in a safe way

 

Introduction.  The last page developed a form and processing script that actually wouldn't perform as desired under pressure.  One of the main ways to make things thread safe is to make use of the

synchronized

keyword.  In general, this is not a bad strategy at all, but it can introduce too many inefficiencies in your processing.  Thus the processing page might have a scriptlet that looks like the following.

 

<%!
synchronized (this)
{

String first_name;
String last_name;
String address1;
String address2;
String city;
String state;
String zip;
int number_threads;

}
%>

 

While this will work it is at the cost of performance.  It actually inhibits the server so that certain code segments can only be done one segment at a time!  Thus you are defeating the purpose of threading.  While this is improved as you create smaller synchronized segments, the following coding approach gets around this completely without synchronizing.

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

 

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

<body bgcolor="003355" text="cccccc">
<form method="post" action="thread_process_improved.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>

 

The only real change is the name of the processing script that the form posts to.

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

 

 

What we'll do in this processing script is develop a class that can treat each of our posted streams of data as an instance of a class.

This will be done with the following script which you should call thread_process_improved.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">
<%
// instantiate a holder object for the data
NameAndAddress holder = new NameAndAddress();
// obtain the posted form information
holder.first_name = request.getParameter("txt_first_name");
holder.last_name = request.getParameter("txt_last_name");
holder.address1 = request.getParameter("txt_address1");
holder.address2 = request.getParameter("txt_address2");
holder.city = request.getParameter("txt_city");
holder.state = request.getParameter("txt_state");
holder.zip = request.getParameter("txt_zip_code");

// call the formatting routine
formatNameAndAddress(holder, out);
%>
</body>
</html>

<%!
// create a class to act as the data holder
public class NameAndAddress
{

public String first_name;
public String last_name;
public String address1;
public String address2;
public String city;
public String state;
public String zip;

}

void formatNameAndAddress(NameAndAddress data, JspWriter out) throws java.io.IOException
{

out.println("<font size = 4>");
out.println(data.first_name);
out.println(" " + data.last_name + "<br>");
out.println(data.address1 + "<br>");
// print out the address2 line only if it has contents
if ((data.address2 != null) && (data.address2.length() > 0))
{

out.println(data.address2 + "<br>");

}
out.println(data.city +", " + data.state + " " + data.zip + "<br>");
out.println("</font>");

}
%>

 

Notice how the only real thing we have done differently is make the input variables instances of a class which will be placed in different memory locations for each thread.

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