A HISTORICAL REVIEW ABOUT JAVA WEB APPLICATION DEVELOPMENT – Develop Java Web Applications to Access Databases

8.1   A HISTORICAL REVIEW ABOUT JAVA WEB APPLICATION DEVELOPMENT

Java Web applications are based on the Servlet technique, and the Servlet works as a Web server that provides all support, such as receiving requests from the client and sending responses back to the client. That is, a Servlet is a server class built in the Java language with all the functionalities of a server engine. A Servlet performs its job in the following ways:

  • When a Servlet is created, the init() method is called to do the initialization for the Web server.
  • When a request is received by the Servlet, it creates two objects: request and response.
  • Then the Servlet sends these two objects to the service() method.
  • The request object encapsulates the information passed from the HTTP request coming from the client.
  • The service() method is the main responding body and will be used to process the request and send the response that has been embedded into the response object back to the client.

Conventional Web applications are built with a Servlet as a Web container and HTML pages as Web clients.

8.1.1   Using Servlet and HTML Web Pages for Java Web Applications

The main purpose of using the Servlet is to compensate for the shortcomings of using a common gateway interface (CGI). Unlike the CGI, the Servlet can be used to create dynamic Web pages during the server-client communication processes. Two methods, doGet() and doPost(), are the main channels to communicate between the server and clients.

FIGURE 8.1   The finished Login.html file.

General uses of Servlets include:

  • Processing requests received from clients and responses back to clients
  • Creating dynamic Web pages
  • Managing state information for applications
  • Storing data and information for clients

Generally, client pages can be divided into two categories: reading pages and posting pages. The former is used to read data from the user, and the latter is used for displaying feedback from the server. To interface to the client to get user data, most often, the server calls the getParameter() method that belongs to the request object. To send feedback to the client, the server usually uses the println() method that belongs to the out object. With this pair of methods, a server can eas-ily communicate with the client and transfer data between them.

By using an example that utilizes these methods to transfer login information between a Servlet and a client Web page, we can get a much clearer picture and deeper understanding of this topic.

Open Notepad and enter the code shown in Figure 8.1 to build the Login.html file. Save this file with the name of “Login.html” to make it an HTML file. You have to use double quotation marks to enclose this file name with the. html extension to let Notepad know that you want to save it as an HTML file.

Double-click on this file to run it. The run result is shown in Figure 8.2.

Two input text fields are used by users to enable them to enter the desired username and pass-word. The key is the identifier for both text fields, username and password, which is the name

FIGURE 8.2   The Login.html run result.

FIGURE 8.3   Using the getParameter() method to get data from the client.

property or attribute of these two text fields. When a server needs these two pieces of login informa-tion, it uses the getParameter() method defined in the request object with the names of two text fields as identifiers to get them. Figure 8.3 shows a piece of code developed on the server side to perform this login information pick-up operation.

Two variables, uname and pword, are used on the server side to hold the picked-up username and password entered by the user from the client Web page. The getParameter() method is used to do this pick-up operation. The identifiers for these two parameters are the names of two text fields on the HTML page.

With this simple example, you can see how easy it is for the server and client to communicate with each other. The server can send feedback or post any desired information in the client by using the out object that is obtained by creating a new PrintWriter instance in the first two codelines.

OK, now we have a clear picture of using a Servlet and a client to build and implement a Java Web application in the early days. To deploy this login Servlet, we need to locate the Servlet class file in the suiTable directory.

One of the shortcomings of this kind of application is that the server and client use two differ-ent languages, and a converter or renderer is necessary to perform a conversion between the two. This will reduce the run speed and efficiency of the Web application. A solution to this issue is the JavaServer Pages technique, which was developed by Sun. With the help of the JSP, server code can be extended and embedded on the client side to facilitate communications between a server and a client.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024