Internet Programming I - Assignment 8: Html Forms and Java Programs, Assignments of Information Technology

Instructions for completing assignment 8 in the internet programming i course, which involves creating an html form and a java program to collect data from the form and send it to a server. The server processes the data and sends the result back to the user. The document also includes instructions for downloading and running a simple server, webserver, and explains how the server processes the form data.

Typology: Assignments

Pre 2010

Uploaded on 08/09/2009

koofers-user-ciu
koofers-user-ciu 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Internet Programming I
Assignment 8
Html forms and Java programs
Due: November 7, 2006
One of the principle uses of programming on a web page is to collect data from an Html form and send it
to the server. Once the data is there, it can be used in several ways. The server can compute something
and send the result back to the user, or it can use the data to query a database. We will see examples of
computations first. The ninth assignment will be used to query a database.
1. To get an idea how this works, we will need a server. There are a number of commercial servers
including ones available from Microsoft. There is also an open source server, Apache Tomcat, from
the Apache open source project. It is widely used, including on the Seidenberg School web site.
Since these servers are fairly complicated, we will use a simple stripped down version that I wrote
several years ago. It is called WebServer and can be found in the documents file on my website.
2. In the folder called IT 200, click on Course Documents. One of the files listed there is
Webserver.zip. Unzip it into a folder on your computer or USB drive. You will find a number of
files, including WebServer.java. There should also be a subfolder called client_server. This contains
the .class files. These are the ones that contain the bytecode that can be interpreted and run by your
computer.
3. Open JCreator and load the file, WebServer.java. It is all compiled and ready to run. So just
click on the two blue dots that run the file. This will open a black console screen with nothing on it
except Port: You can either type in a number for the port or press the Enter key. Doing the latter
will select the default port, which is 8080. (The standard Internet port is 80. We will avoid using it,
since we don’t want to interfere with usual Internet traffic.)
4. To see how the server works, open Internet Explorer and browse to the folder containing the
Webserver. Next open the file called EmailForm.html. You should see the following screen.
5. Fill in your name and email address and click on the Send button. You should see something like
the following, where the name entered was Alice Lee and the email address was [email protected].
pf3
pf4
pf5

Partial preview of the text

Download Internet Programming I - Assignment 8: Html Forms and Java Programs and more Assignments Information Technology in PDF only on Docsity!

Internet Programming I Assignment 8 Html forms and Java programs Due: November 7, 2006 One of the principle uses of programming on a web page is to collect data from an Html form and send it to the server. Once the data is there, it can be used in several ways. The server can compute something and send the result back to the user, or it can use the data to query a database. We will see examples of computations first. The ninth assignment will be used to query a database.

  1. To get an idea how this works, we will need a server. There are a number of commercial servers including ones available from Microsoft. There is also an open source server, Apache Tomcat, from the Apache open source project. It is widely used, including on the Seidenberg School web site. Since these servers are fairly complicated, we will use a simple stripped down version that I wrote several years ago. It is called WebServer and can be found in the documents file on my website.
  2. In the folder called IT 200, click on Course Documents. One of the files listed there is Webserver.zip. Unzip it into a folder on your computer or USB drive. You will find a number of files, including WebServer.java. There should also be a subfolder called client_server. This contains the .class files. These are the ones that contain the bytecode that can be interpreted and run by your computer.
  3. Open JCreator and load the file, WebServer.java. It is all compiled and ready to run. So just click on the two blue dots that run the file. This will open a black console screen with nothing on it except Port: You can either type in a number for the port or press the Enter key. Doing the latter will select the default port, which is 8080. (The standard Internet port is 80. We will avoid using it, since we don’t want to interfere with usual Internet traffic.)
  4. To see how the server works, open Internet Explorer and browse to the folder containing the Webserver. Next open the file called EmailForm.html. You should see the following screen.
  5. Fill in your name and email address and click on the Send button. You should see something like the following, where the name entered was Alice Lee and the email address was [email protected].
  1. Next go to View/Source on the Internet Explorer menu. There you will find the Html source for the page. It should look like the following. This is a standard Html page with the data included.

Test Data

Hello. Your name is Alice Lee Your email address is [email protected]

  1. Now return to JCreator and look at the black console screen. It will now contain information about the process that you just ran. This information was written on the screen by the server when the Html form was processed. It shows that the server received the data, which is shown after the question mark (?). In this string, spaces are replaced by plus signs (+) and data items are separated by ampersands (&). This string was generated by Internet Explorer, and not by the WebServer program. The WebServer program only echoes it. The rest of the data on the screen comes from the WebServer program itself.
  2. Now open the program EmailProcessor.java in JCreator. The program is shown below. package client_server; /**
    • EmailProcessor processes a request from a web page. It responds to the
    • request by sending back a web page listing the name and email address. **/

String name = request.getParameter ("name"); String email = request.getParameter ("email"); are used to bring this information into the program.

  1. Next open the Html form page in JCreator. When you click on the open file icon, you will only see Java files. Click on the Files of type: down arrow and select Html Files. You will then see the file, EmailForm.html. It should look like the file below.

E-Mail Form

Enter your name and e-mail address.
Then click the Send button to send the data to the server.
See the results.

Name

E-Mail Address

  1. The Java program uses a request to get the data from the form. The line, String name = request.getParameter ("email "); receives the data entered into the box called email in the form. It comes from the line

    E-Mail Address

    It is very important that the parameter in name="email" be exactly the same as that in request.getParameter ("email "); Since Java is case sensitive, a serious error is to use different cases in the two places.

  2. Now change both the program and the form to add a box for a telephone number. Insert another input statement into the form to produce the third box.

    Telephone

    Then save the changed file.

  3. Next insert two new lines into the program, one to get the new data item and the other to display it. The line that gets the data will read String phone = request.getParameter ("phone"); The line that displays it will be something like out.println ("Your telephone number is " + phone + ""); Note that all three places use the word, phone, and not some modification of it. You should be able to tell where these lines go in the program by looking at the other lines there.

  4. Now start over with a new form and a new program. This time the form should have boxes for the width and height of a rectangle. You can copy the EmailForm into the new Html file and change several lines. The first line to change is the one with the form tag. It must have the name of the Java program that will process this request. If you call that program AreaProcessor, the form tag becomes

    You should also change the lines that tell the user what to do. Enter your width and height of a rectangle. Also both input lines that define the boxes have to be changed. They become

Width

Height

Save this form with a name such as AreaForm.html.
  1. Next create the Java file called AreaProcessor. Again copy the code from EmailProcessor and change some of the lines. First change the name of the program to AreaProcessor. The file name and the program name must be the same. You should also change the information in the comment. Now change the request lines to int width = Integer.parseInt (request.getParameter ("width")); int height = Integer.parseInt (request.getParameter ("height")); Just as in JavaScript, the strings must be changed to numbers. In Java this is done with Integer.parseInt. This is awkward, but it works. (To change a string into a double, use Double.parseDouble.) The computation part is easy. We need a variable called area that will multiply the width and the height. The line needed is int area = width * height; Last we change the output lines. They now become out.println ("Area of Rectangle."); out.println ("The area of the rectangle is " + area + ""); Save and compile the program. Test it out by running it with the WebServer program.
  2. Now do some other computation. Create a new Html form and Java program. Be very careful that the parameters are the same case in both. It is easiest just to keep the names simple and use all lower case. Again you only have to change a few lines in the files. When done, test the program to make sure it works, zip up the folder and send it to me.