<< Chapter < Page Chapter >> Page >

The getParameter method receives the name of a request parameter and returns the value corresponding to that request parameter as a String , or null if the parameter does not exist. (Recall that the data is transmitted to the server as name::value pairs.)

Begin constructing the HTML output data

At this point in the process, all of the incoming data has been saved in a variable named name and an array named items . The time has come to construct the raw HTML code and send it back to the client. Listing 4 begins that process by

  • Telling the browser how to interpret the data ("text/html").
  • Getting on output stream on the response object on which to print the output data.
  • Constructing the first few lines of HTML text.
Listing 4 - Begin constructing the HTML output data.
//Establish the type of output res.setContentType("text/html");//Get an output streamPrintWriter out = res.getWriter();//Construct an html form and send it back to the client out.println("<html>"); out.println("<head><title>Java4550a</title></head>"); out.println("<body>");

Create raw HTML code for the form

Listing 5 begins the construction of the HTML form that is to be returned to the browser. This code creates the first in a series of HTML statements necessary tosupport the input field and the submit button. (You may need to refer to earlier modules that discussed the format of an HTML form in order tounderstand the next few fragments.)

Listing 5 - Begin the HTML code for an HTML form.
out.println("<form method='get' action=" + "\"http://" + hostName + ":8080/Java4550a\">");

The code in Listing 5 uses the name of the computer on which the server is running ( hostName ) that was obtained and saved earlier to construct the action element of the HTML form. This causes the servlet to be suitable for running in a server on an computerwith any name. It also makes it possible to access the servlet from other computers on the local area network provided those computers know the name ofthe computer on which the server is running.

Once again, this is very important information for students enrolled in this course.

This code produced the term dell8700 in Figure 3 because the server was running on a computer named dell8700 when the screen shot was taken.

This fragment results in HTML code that causes a GET request to be sent to the HTTP server containing field data as a parameter whenever the user presses the submit button

The input field and the submit button

Listing 7 places a line of instructional text on the browser screen.

It also creates the HTML code necessary to place an input fieldand a submit button on the form that will be displayed on the screen as shown in Figure 1 .

Listing 6 - An input field and a submit button.
out.println("<p>Enter a name and press the button</p>"); out.println("<p>Name:<input type=\"text\" name=" + "\"firstName\"/></p>"); out.println("<input type=\"submit\" value=" + "\"Submit Name\"/>")

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask