Computer Science 396S
Assignment 1
Due:
First get the files for the
WebServer from my web site, put them into a folder and compile them. When you do, compile them in the following
order: WebRequestProcessor, Request, Response, and WebServer. You may want to download and compile the Page
file as well. If Page.class is in the
subfolder, client_server, you will not have to include it in every program.
Now download or type in the
files for EmailProcessor.java and EmailForm.html. They are in the same folder on the web
site. Compile the java program. Next run the WebServer program. When it shows the screen with Port: just type the enter key. If some time you wish to use a different
port, you can type it in at this point in the program.
Load either Internet Explorer
or Netscape and then click on the File menu.
In Internet Explorer, chose Open, in Netscape Open File. Go to the folder that holds the programs and
load EmailForm.html. Type in your name
and email address and click the Send button.
If you do not see a response from the server, check both the code for
the java program and the html form.
Once you have this example
working, try the one below. Then create
your own program and html form that will perform some computation. There are many possibilities, such as temperature
conversion, a person’s age given the birth date, interest on a loan, currency
conversion, etc. Try to come up with
some computation different from that of other class members.
Hand in printouts of your
program and the form. Also either send
me copies of your program and form or hand in a disk containing both.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<html>
<head> <title>Area of Circle
Form</title>
</head>
<body>
<h3>Enter the radius of
the circle.
<br />Then click the
Send button to send the data to the server.</h3>
<form method =
"get"
action="http://127.0.0.1:8080/client_server.AreaProcessor">
<p><input type =
"text" name = "radius" value = "" size = 30>
Radius </p>
<p><input type="submit"
value="Send"></p>
</form>
</body>
</html>
package client_server;
/* AreaProcessor gets the
radius of a circle from the request. It
then calculates the area of the circle and sends it back in a second web page. */
import java.io.*;
import java.text.*;
public class AreaProcessor extends WebRequestProcessor
{
public void process (Request request,
Response response)
{
// Get the request parameter with the radius.
String radiusString = request.getParameter
("radius");
double radius = Double.parseDouble
(radiusString);
// Calculate the area of the
circle.
double area = Math.pow
(radius, 2) * Math.PI;
// Get a PrintWriter object and create a web page to
return to the browser.
PrintWriter out = response.getWriter ();
Page.createHeader (out, "Area of Circle");
out.print ("<h3>The
area of the circle with radius " + radius);
out.println (" is "
+ decimals (area) + ".</h3>");
Page.createFooter (out);
} // process
// Formats a double for string output with two decimal places.
private String decimals (double num)
{
DecimalFormat decFor = new DecimalFormat ();
decFor.setMaximumFractionDigits (2);
decFor.setMinimumFractionDigits (2);
return decFor.format (num);
} // method decimals
}
// Class with static methods
that add standard lines to the html output page.
class Page
{
public static void createHeader
(PrintWriter out, String title)
{
out.println ("<!DOCTYPE
HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println ("<html>");
out.println ("<head>");
out.println ("<title>" + title +
"</title>");
out.println ("</head>");
out.println ("<body>");
} // createHeader
public static void createFooter
(PrintWriter out){out.println ("</body></html>");}
} // class Page