Vous êtes sur la page 1sur 10

4.

Java servlet technology


This section serves as additional material for the Software Project. It contains an introduction to Java servlet technology. By reading this section in parallel with the source code of the example AdresBoekje you will learn to recognize the methods and techniques, which are specific to the servlet technology.

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.

4.2. The HTTP protocol


Before trying to understand what a Java servlet is, you have to know some things about the most popular web-based communication protocol, the HTTP (Hypertext Transfer Protocol). HTTP is commonly associated with browser-to-server communication, but it has many other possibilities. Like many protocols, HTTP is constantly evolving. At this moment the latest version in use is HTTP 1.1. A HTTP communication involves two parties (see Figure 10): There is a client, which is responsible for starting the communication with the server, by sending a client request. There is a server, which waits for a client request, processes the client request and returns a server response to the client. HTTP is the network protocol used to deliver almost all files and other data (called resources) on the World Wide Web. In Internet terms, the client is seen as a web browser, called user-agent, but it can also be another server. If you enter an URL (Uniform Resource Locator) in your browser (a HTTP client), this actually sends a HTTP command to the Web server (the HTTP server) telling it to fetch and transmit the requested document. A resource is not always a file. It can be a dynamically generated query result, the output of a script, etc.

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

4.3. What is a servlet?


As we already said, the Java servlet runs on a Web server and communicates with a client. The client of the servlet can be a Web browser or another Java application. The servlet dynamically processes requests and constructs responses. Any client that correctly prepares a request will receive an appropriate response, and this is due to the servlet. The most common type of format for the servlet response is the HTML page. To be useful, servlets can also communicate with external resources, often databases. In this way, the servlets act as a middle layer between a request from a web browser and a database or other application (see Figure 12).

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.

4.4. A few simple servlets


In this paragraph we discuss first the basis structure for a servlet. After that, a few examples follow to teach you how to write simple servlets. 4.4.1. The servlet basic structure The general structure of a Java servlet is shown in Figure 13. We try to describe the few steps necessary to build the main blocks in such a servlet. Block 1 In order to make servlet development possible, a special Java API (Application Programming Interface) is available, the Servlet API. All the Java functions necessary for servlet programming are contained in this API, consisting of two packages, named javax.servlet and javax.servlet.http. The first package deals with generic servlets and the second is used to implement HTTP servlets in particular. Any servlet has to start with a statement to import at least these two packages. These packages contain servlet specific classes and interfaces. You dont have to know how are they implemented by the container, you just have to know how to access them. More details about these classes and interfaces can be found find on the web page http://raderboot.cs.vu.nl/doc.html. For example, in javax.servlet there is a superclass of all servlets, named GenericServlet, where no communication protocol is specified. Almost all servlets implement the HTTP protocol; so another package is available, javax.servlet.http, containing among others the superclass HttpServlet. This class serves as basis for all other servlets you might want to write. The HttpServlet class has a few basic methods, like init(), service(), doPut(), doGet(), and destroy(). Of course many classes and interfaces in javax.servlet.http continue to use the classes and interfaces from javax.servlet. For example, the methods init() and destroy() in HttpServlet class are inherited from GenericServlet class. This is shown by the statements super.init() and super.destroy(). In addition since servlet output uses a PrintWriter stream, package java.io is also required.

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

Fig. 13. The general structure of a servlet.

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.

The result of running the HelloServlet.

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

Fig. 17. The result of invoking the HelloWWW servlet.

28

4.5. Accessing a database with a servlet


Accessing a database over the Internet is a very common requirement. Servlets can connect to a relational database by using the JDBC standard library. With the same Java syntax a wide variety of different SQL databases can be accessed. During this project we will use the MySQL database management system. Officially JDBC is not an acronym and it stands for nothing. Unofficially, Java Database Connectivity is commonly used as the long form of the name. Detailed information about the methods and classes from the JDBC API can be found at http://java.sun.com/j2se/1.4.1/docs/guide/jdbc/index.html. There are 7 standard steps in querying databases: 1. Load the JDBC driver 2. Define the connection URL 3. Establish the connection 4. Create a statement object 5. Execute a query or update 6. Process the result 7. Close the connection We will clarify each of these steps. Load the driver The JDBC driver is a piece of software that knows how to talk to the database server. To load the driver all we need to do is load the appropriate class, using the method Class.forName(): class.forName(JDBCdriver).newInstance Define the connection URL Once the JDBC driver is loaded, you need to specify the location of database serve, in our case a MySQL server. URLs referring to databases contain the string jdbc:protocol followed by the server host, port and database name. The exact format is defined in the documentation that comes with a particular driver. If we consider the following situation: String host = raderboot.cs.vu.nl; String mySqlPort = 3306; String dbName = address_book; The server URL in this case is: String mySqlUrl = jdbc:mysql: + host + : + mySqlPort + / + dbName; Establish the connection To make the actual network connection you have to pass the server URL, the username and the password to the getConnection() method of the DriverManager class. Suppose the user is frank and its password is secret. Then we have: String username = frank; String password = secret; The new JDBC URL is formed and given as parameter to the getConnection() method: String jdbcUrl = mySqlUrl + ?user= + username + &password= + password; Connection connection = DriverManager.getConnection(jdbcUrl); Create a Statement Now it is possible to send queries to the database. A Statement object is used to send queries and commands to the database and is created from the connection as follows: Statement statement = connection.createStatement();

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

Vous aimerez peut-être aussi