J2EE Material
J2EE Material
J2EE Material
4.1. Introduction
Suppose the following situation. A client is wishing to buy a pet dog and for this purpose he wants to query a suitable database using a browser. Of course he first wants an HTML form where he can specify the details of his ideal dog (for example race, colour, age, skills, bad habits, etc). He submits this HTML form and the result he expects is a list of all petshops or asylums where this kind of dog is currently available. How can you solve this type of problem? You already learnt during PVI practicum how to use PHP scripts to access a database from a browser. The PHP scripting language was developed because static Web pages, which could not interact with the user, seemed not to be enough. Dynamically created web pages were required and the Common Gateway Interface (CGI) was developed to fill this void. Though widely used, CGI scripting technology has the shortcoming of platform dependency. For this project you will learn about another possible solution to the above-mentioned problem, besides PHP scripting: Java servlets. Servlets are the Java Technology answer to CGI programming. A servlet is a program written in Java that runs on a Web server and solves the problem basically in the same way the PHP script does. It takes the user information from the initial HTML form, specifying what kind of dog the client wants, connects to an appropriate database and answers with a HTML page. The advantage of the servlets approach is that you use the Java programming language, with which you are already familiar from the IP (Inleiding Programmeren) practicum. Another advantage is that the servlets offer a portable way to provide dynamic, useroriented context, so they will work on any kind of hardware. The range of potential business applications that can use servlets is immense (airline tickets reservation systems, on-line shops, on-line travel agencies, etc). In this chapter we will present specific information about Java servlets, which will help you understand the source code of the example AdresBoekje.
22
HTTP defines how messages are formatted and transmitted and what actions web servers and browsers should take in response to various commands. This section will look at the structure and context of HTTP transactions.
Fig. 10. The client - server model of the HTTP protocol (in this case the client is a browser) The HTTP requests and responses have a similar structure and contain text-based communication in the headers and eventually text in the message body. The message body is usually a HTML page. 4.2.1. The HTTP client request The client is responsible for initiating the communication with the server. He sends a formatted request and waits for a response from the server. The structure of the client request is shown in Figure 11. The order of the component parts is important.
Fig. 11. The structure of a HTTP client request. At a minimum the request will include only the request information line, consisting of three parts separated by spaces: Method URI HTTPversion where the first part is the method name (always in uppercase), the second is the local path of the requested resource and the last is the version of HTTP being used. For example this is the HTTP request generated when you ask the default index page of a website: GET /index.html HTTP/1.1 Note that the URI is the part of the URL that comes after the host and port. For example for the URL http://randomhost.com/servlet/search/Booksearch, the URI is /servlet/search/BookSearch. The most used request methods provided by HTTP 1.1 are: GET is the most common HTTP method. It is a retrieval method, which says simply get me this document. The method does not change the information or resources on the server. The information is retrieved as an entity, referenced by the request URL. The most common way 23
to get information from a web page is to attach the form data to the end of the URL after a question mark in a query string. A GET HTTP request URL contains the following parts: http://<host>:<port><request path>?<query string> POST is more sophisticated. It performs not only retrieval, but it also allows the client to send a block of data in the message body of the request, to the server or to a database.
Other methods such as HEAD, PUT and DELETE are not relevant for this project. 4.2.2. The HTTP server response The structure of the servers response is similar to the client request with two main differences the response information line and the response headers. A HTTP response consists of the response information line, one or more headers, a blank line, and the actual document body, in that order. The response information line includes the HTTP version, the status code indicating the result of the request and the message associated with the status code, according to this format: HTTPversion statuscode Message For example a quite often seen message generated by the server when it cannot find the webpage you requested is: HTTP/1.1 404 Not Found
Fig. 12. The servlet runs on the Web server and communicates with the client. The client is usually a browser. The servlet dynamically processes the client requests and constructs responses to the client. Eventually it can access and manipulate a database. The servlets response is usually an HTML page. The most widely used communication protocol between the client and the servlet is HTTP. This is the reason why we will talk only about HTTP servlets further on.
24
The servlets life cycle Any servlet class has a few basic methods, divided in lifecycle methods: init(), destroy(), a service method service() and some HTTP handling methods, like doGet(), doPost(), etc. Servlets do not communicate directly with the client. The servlets run in a so-called servlet-container, the web server (see Figure 12). [Note: A web server is the software that resides on a server computer]. This servlet-container works as an interface between the client and the servlet. The servlet-container controls the servlets life cycle. The servlet-container listens for requests from clients, which may occur irregularly and unpredictable. On this container stays an HTML page that contains a hyperlink to a servlet. When the user selects this link, the servletcontainer invokes the servlet by performing the following steps: 1. If the instance of a servlet does not exist, the container loads the servlet class, creates an instance of the servlet class and it initializes the servlet instance by calling the init() method. 2. Invokes the service() method passing a request and response object. The service() method then calls doGet(), doPost() or another doXXX() method, depending on the HTTP request it received. Now the servlet can handle client requests and pass the client a response via the servercontainer. Finally when the server decides to unload the servlet, it first calls the servlets destroy() method.
25
Block 2 To be a servlet, a class should extend the class HttpServlet and override its methods doGet() or doPost(), depending on whether the data is being sent by the GET or by POST method. Both methods take two parameters: an HttpResponseRequest and an HttpServletResponse. Block 2.1 HttpServletRequest has methods to find out about incoming information such as form data, HTTP request headers and clients hostname. Extracting the needed information from a form data is quite a tedious operation. A very useful method is getParameter() used to extract parameters from the request. Block 2.2 and 2.3 HttpServletResponse lets you specify outgoing information such as HTTP status code, response headers and more important, lets you obtain a PrintWriter object, used to send a document content back to the client. The package java.io has to be imported for PrintWriter. PrintWriter object is similar to the Output object from IP practicum. The correct procedure for building a response is to first fill in the response headers by indicating the type of the response contents, then retrieve the output stream PrintWriter and finally write any body context to output stream. It is important to respect this order. For simple servlets most of the effort goes in println() statements to generate the desired page for the client. In Figure 13 we show the general structure for a servlet implementing the GET method:
import necessary packages Block 1
public class TemplateServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Use req to read incoming HTTP headers and HTML form data entered and submitted by the user. Use res to specify the HTTP response status code and headers PrintWriter out = res.getWriter(); Use out to send content to client browser } } Block 2.3 Block 2.1 Block 2
Block 2.2
26
4.4.2. Example. A simple servlet generating plain text Lets try, using the template from 2.4.1 to write a very simple servlet, which will send as response to the client the well-known text Hello World. In Figure 14, we show the Java source code of this servlet. Use any text editor you are familiar with to edit this simple servlet.
import java.io.*; import javax.servlet.http.*; import javax.servlet.*; public class HelloServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Indicate the content type in the response res.setContentType ("text/html"); // Write content of the response PrintWriter out = res.getWriter(); out.println("Hello World"); } }
Fig. 14. Source code for a simple example servlet HelloServlet.java, generating text. Installing and running the example servlet After you edited the servlet HelloServlet.java, save it in the directory: /home/swp08xx/servlets/WEB-INF/classes where xx is the number of your group. You have to compile and run your servlet as specified in Chapter 2. If everything works fine, the message Hello World appears in the browser window (see Figure 15). This is the servlets response we expected, given as plain text. Try to make little changes in the source code and observe the effects.
Fig. 15.
27
4.4.3. Example. A servlet that generates HTML Most servlets generate HTML instead of plain text as in our previous example. To build HTML, you need two additional steps: 1. Tell the browser that you are sending back HTML 2. Modify println statements to build a legal Web page. The first step is accomplished by setting the HTTP header. HttpServletResponse has a special method setContentType() for this purpose. In order to say that the returned page is HTML, you use: res.setContentType(text/html); Other types of documents can be also given in this way, for example image/gif for pictures in gif format. Remember that all the headers have to be set before you can use PrintWriter(). The second step is to use println() statements to generate a HTML page.
import java.io.*; import javax.servlet.http.*; import javax.servlet.*; public class HelloWWW extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//WC3//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" + "<BODY>\n" + "<H1>Hello WWW</H1>\n" + "</BODY></HTML>"); } }
Fig. 16. The source code for a servlet HelloWWW.java, generating a HTML page
28
29
Execute a Query Once you have a Statement object, you can use it to send SQL queries by using the executeQuery() method, which returns an object of type ResultSet. Here is an example: String query = SELECT Name, Address FROM Mytable; ResultSet resultset = statement.executeQuery(query); Other useful methods are executeUpdate() for UPDATE, INSERT or DELETE and execute() for an arbitrary command. Prepared statements Other useful methods in Connection class include prepareStatement(). If you are going to execute similar SQL statements multiple times, using prepared statements can be more efficient than executing a raw query each time. The idea is to create a parameterized statement in a standard form that is sent to the database for compilation before actually being used. A question mark is used to indicate the places where a value will be substituted into the statement. Each time you use the prepared statement you simply replace some of the marked parameters, using a setXXX() call corresponding to the entry you want to use and the type of parameter. Then you use a normal execute Query or execute() method. For example: String query = UPDATE employees SET salary = ? WHERE id = ?; PreparedStatement statement = connection.prepareStratement(query); statement.setInt(1, 3000); statement.setInt(2, 23415); statement.execute(); Process the Results The simplest way to handle the results is to process them one row at a time using the next() method of ResultSet to move through the table a row at a time. Within a row, ResultSet provides various getXXX() methods that take a column index or column name as an argument and return the result as a variety of different Java types. For example, use getInt() if the value should be an integer, getString() for a string. Note that the columns in ResultSet begin with 1, not with 0. Here is an example that prints the values of the first two columns in all rows of a ResultSet: while (resultSet.next()) { System.out.println(resultSet.getString(1) + + resultSet.getString(2)); } The same result can be obtained with: while (resultSet.next()) { System.out.println(resultSet.getString(Name) + + resultSet.getString(Address)); } Close the Connection To close the connection with the database, just use: connection.close();
30
References
1. Marty Hall, Core Servlets and JavaServer Pages, Sun Microsystems Press, Prentice Hall, 2000. 2. Subrahmanyam Allamaraju, J, T. Bell et al., Professional Java Servlets 2.3, Birmingham Wrox Press, 2002. 3. J. Graba, An introduction to Network programming with Java, Addison Wesley, 2003 4. http://java.sun.com/products/Servlet/technical.html 5. http://java.sun.com/products/servlet/articles/tutorial/
31