<< Chapter < Page Chapter >> Page >

Without getting into the details as to why, I will tell you that the last three lines of code in Listing 1 , (in conjunction with the import directive for java.net.* ) cause the name of the computer on which the server is running to be saved in the variable named hostName and to be displayed on the Tomcat server console. You should have learned enoughabout the class named InetAddress earlier in this course to understand what is going on here without further explanation.

This is very important information for students enrolled in this course because this or something like it will be required for many of the programmingassignments.

Two incoming parameters

Recall from previous modules that the doGet method receives two parameters. One parameter named req is a reference to an object of the interface type HttpServletRequest that contains all of the information sent by the browser when the request was made. The other parameter named res is a reference to an object of the interface type HttpServletResponse that collects output information and sends it back to the browser.

Both of those objects contain numerous methods that can be used to access incoming information ( req ) or to send information back to the browser ( res ) . We will see several of those methods being used shortly.

A bit of technical trivia:

HttpServletRequest and HttpServletResponse are interfaces, not classes. In general, we don't know the names of the classesfrom which the objects referred to by the incoming parameters were instantiated. And, we don't really care because we onlyplan to call methods on those objects that are declared in the interfaces that the objects implement.

However, just as a matter of technical trivia, it appears that those classes have the following names on my system:

org.apache.catalina.connector.RequestFacade org.apache.catalina.connector.ResponseFacade

Multiple fields with the same name

An HTML form can have multiple fields with the same name. As you saw in Figure 3 , the hidden fields are all named item . The code in Listing 2 calls the getParameterValues method on the request object to get the values stored in all of the hidden fields named item .

Listing 2 - Get and save the hidden values from the browser page.
String[] items = req.getParameterValues("item");

The getParameterValues method receives the field name item as a String parameter and returns an array of String objects, containing all of the values with matching names contained in the request object, or null if the field name does not exist.

Those values are saved in the String array named items shown in Listing 2 . As you will see later, this is data that was saved from previous requests.

Get and save user input data

Listing 3 calls the getParameter method on the request object to get and save the value submitted by the browser in the field named firstName . (See the input element on the sixth line in Figure 3 .) This is also the text field shown in Figure 2 .

Listing 3 - Get and save user input data.
String name = req.getParameter("firstName");

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