<< Chapter < Page | Chapter >> Page > |
Figure 2 - Browser display for Tom, Dick, and Harry.
The GET method
In this servlet, requests are sent from the browser to the server using the GET method. (The POST method could also have been used.) Each time the browser makes a GET request of the server, an HTML form is created by the servlet and sent back to the browser.
Hidden fields
Hidden fields are added to the form each time it is created by the servlet. One hidden field contains the name submitted for that GETrequest. Other hidden fields contain each of the names submitted by each previous GET request during the current session. In other words, the hidden fields on each successive formmatch those of the previous form plus a new hidden field for the new name submitted with that GET request.
Raw HTML code
Figure 3 shows the HTML code returned to the browser following the sending of Tom, Dick, and Harry to the server in three separate requests. The three hidden fields are shown at thebottom.
Figure 3 - Hidden fields in the servlet output.
The term dell8700 on the fourth line of Figure 3 is the name of the computer on which the server that serviced the request was running.
If the user enters another name into the text field and clicks the submit button
Save historical data in an HTML form
With this approach, the historical data is saved by embedding it in the new HTML form that is returned to the browser. There is no requirement to save thehistorical data in a database or in the server's file system. Note however, that the data will be lost if the user shuts down the browser or shuts down thecomputer. However, because of the browser cache, it is sometimes possible for the user to navigate to other web sites and then return to the same web pageusing the Back button and find that the historical data is still there.
Simple, reliable, and easy to implement
This approach has been used by web programmers for many years in those cases where it will get the job done. The approach is reliable and easy to implement. Itplaces no special requirements on the server software (other than the ability to support Java servlets) and it is compatible with all browsers that support HTML forms.
The doGet method
The code in Listing 1 shows the beginning of the controlling class and the beginning of the doGet method for the servlet.
Listing 1 - Beginning of the servlet class. |
---|
import java.io.*;
import javax.servlet.*;import javax.servlet.http.*;
import java.net.*;public class Java4550a extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException{//Get and display name of localhost
InetAddress inetAddress = InetAddress.getLocalHost();String hostName = inetAddress.getHostName();
System.out.println(hostName); |
The name of the server
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?