Winter 2017 / CSC 415 Web Applications
In lecture, we discussed a program that receives a HTTP request and echoes it back to the browser. The code worked, but it experienced occasional crashes. The crashes are due to the web browser sending an empty HTTP request without being instructed to do so by the user. This results in the program immediately running into the end of the input and throwing a null pointer exception when it tries to read the first line of of the request. In addition to that, if you send a HTML response to the browser, the browser will automatically send a HTTP request for an icon to use in the title of the browser tab. This will also cause our program to crash. I have modified the program to check for these two extra requests and close the socket. The modified code is here .
To test the program, send the program any GET or POST request from a browser. For example, to test the program with a POST request, you might use this Http Echo Test HTML file. You can obtain the text for the HTML file by using the View Page Source facility of the browser, by right-clicking and selecting the option to same the file to your local disk. You can then load the file into NodePad ++ and run it with the browser of your choice. Your should get a response that looks like this:
POST / HTTP/1.1 Host: localhost:50001 Connection: keep-alive Content-Length: 34 Cache-Control: max-age=0 Origin: null Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 Content-Type: application/x-www-form-urlencoded Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8 firstname=Godfrey&lastname=Muganda
As a preliminary to the assignment, go on the W3Schools website and learn about the following HTML elements: radio, fieldset, legend. Next, compose a HTML file that has a form element and various other elements, as to make it a suitable interface for a web calculator. so as to render as in the following image. The action attribute of the form should target the HTTP Echo program.
Each of the radio button inputs should have a value of "operation" for the name attribute. When you submit the form, you should get a browser response that looks like this:
POST / HTTP/1.1 Host: localhost:50001 Connection: keep-alive Content-Length: 22 Cache-Control: max-age=0 Origin: null Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 Content-Type: application/x-www-form-urlencoded Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8 X=23&Y=5&operation=sum
Use your HTML file to post to a modified version of the HTTP echo program. The modified Echo Program sends back a HTML response that identifies the calculator operation and the numbers X and Y that were sent in the request, and also gives the result of the calculation. This should be in addition to the echo of the received request. An example is shown below.
Here are some hints. Once you are past the blank line that comes after the request headers, read the body one character at a time, storing the sequence of characters read into a StringBuilder object. Next, use the String.split() method to split on the pattern "&". This will give you three strings of the form name=value. Use the String.split() method again to splic the name value pairs on the pattern "=".
Turn in a Netbeans project folder that contains your Java code, and a text file with an .html extension that contains the HTML form that is supposed to post to your Java program. Submit by email using your College email address.
This assignment is due Friday at the end of week 3, but you would be well-advised to have it completed by Wednesday (At least, you should understand the concepts used in the assignment by Wednesday, so you are better prepared for the quiz.)