|
Introduction | Servlets & HTTP | Life-Cycle | Sessions | Summary | Examples | |||||||||||||
OverviewServlets take an HTTP request at the server, do some processing and send back an HTTP response to the client. Because servlets are a abstraction of HTTP, a good servlet developer cannot escape knowing HTTP. To break you in, we will look at the two most widely used HTTP directives: GET and POST. A GET request is made by a client to retrieve a resource from the server. This resource could be a document, an image or even the result set from a database query. The GET can also pass additional information to the server encoded as name/value pairs on the HTTP request. This additional information could be authentication data required to get a connection to perform a database query. You should view GET as a means to access resources at the web server with limited capabilities of passing data from the client to the server. POST, on the other hand, is geared specifically towards sending large amounts of information, possibly megabytes, to the server, once and only once. Hence, a POST request is literally a post, you send it and your done. When surfing the web in your favourite browser, your performing GET requests to read resources, that is HTML documents and images, currently residing at the web server. Then you arrive at a site that wants you to fill in a web form. On submission of the form, you send your form details to the web server with a POST request. In the next section we introduce the Servlet API and illustrate how HTTP requests and responses are accessible from within servlets. Servlet APIThere have been three key releases of the servlet specification: 2.0, 2.1 and 2.2. Release 2.0 is considered old hat with the majority of production systems running 2.1 compliant servlet engines. Version 2.2 of the servlet specification was published in September 1999. Tomcat from Apache is providing the reference implementation for the Servlet 2.2 specification that will become a part of the Apache Web Server. In this seminar we focus upon version 2.1 of the Servlet specification, highlighting differences between 2.1 and 2.2 where appropriate. For a complete overview of the differences between servlet specs read Jason Hunter's articles on moving from 2.0 to 2.1 and then from 2.1 to 2.2. The Servlet 2.1 API is comprised of two packages: javax.servlet and javax.servlet.http. These packages are comprised of 3 concrete classes, 2 exception classess, 4 abstract classes and 12 interfaces. The javax.servlet package provides support for generic, protocol-indepdendent servlets. The javax.servlet.http package contains classes and interfaces that build upon these generic servlets to provide support for HTTP. Remember that servlets are generic server extensions. It's just that the only servlet specifications published to date only target web servers. In the future we could be seeing packages javax.servlet.ftp and java.servlet.smtp that provide support for FTP and SMTP respectively.
We will gradually introduce the interfaces and classes that comprise the javax.servlet and javax.servlet.http package throughout the seminar. We begin with the interface:
javax.servlet.Servlet All servlets must implement this interface. For protocol independent servlets, extend the abstract class javax.servlet.GenericServlet, which implements Servlet. Servlets are not applications in the true sense. They are small, self-contained server extensions that implement a specific service. A servlet handles services at the server and sends back responses to the client. Hence, servlets do not have a main() but instead must implement javax.servlet.Servlet.service() to handle requests passed to it by the server. Servlets handling a server request For developing web servlets, your going to be dealing exclusively with javax.servlet.http.HttpServlet that extends javax.servlet.GenericServlet. However, there is only one service() method but HTTP has many different types of requests that need to be serviced by the HTTP servlet. So within HTTPServlet's service() method, there is dispatch logic to handle the different types of HTTP request. Hence, developers extending HTTPServlet should not override its service() method but only those methods it delegates to based on the request types outlined in the table below:
Welcome 'Hello World'The most common use-case scenario for a servlet is that a
Take a look at the the HelloWorld servlet that carries out the above scenario:
package com.ack.servlets; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServlet; import java.io.IOException; import java.io.PrintWriter; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Hello World</title></head>"); out.println("<body> Hey there people!"); out.println("</body></html>"); out.close(); } } The first thing to notice is that the HelloWorld servlet extends HttpServlet. The HttpServlet shields HelloWorld from the tedious parts of HTTP processing, providing it with all the methods and objects it needs to do its job. In this example, we want HelloWorld to respond to a GET request made by a browser, so we override the doGet() method that is declared within HTTPServlet. The doGet() method takes two arguments and can throw two exceptions. ServletException is thrown to report any problems encountered in the servlet engine trying to service the HTTP request. Where an IOException can be thrown when using the PrintWriter object to stream web resources back to the client browser. This leaves probably the two most common objects used throughout servlet development; HttpServletRequest and HttpServletResponse. HttpServletRequest encapsulates a client's HTTP request, where HttpServletResponse models it's response. HttpServletRequest gives the serlvet developer access to all the information passed by a client's HTTP request such as header details, request parameters, URI paths, the URL, the user, remote addr, and so on. The HTTP response is used by the servlet to communicate its results back to the client. For the HelloWorld servlet we don't touch the HttpServletRequest object because whatever the client says, we're going to reply, "Hey there people!" The first thing HelloWorld servlet does is set the MIME content type for the HTML page it is about to generate. As a rule, you should always do this before to do anything else in your servlet. HelloWorld then gets hold of a PrintWriter object from its HttpServletResponse object and uses this to stream HTML back to the browser. Finally, we close() the 'out' stream when we have finished sending our HTML page. And that's it, HelloWorld is ready to go! | ||||||||||||||
(c) Zameer's Online Education 2006 |