Java servlets

Servlet template


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SomeServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
      
    // Use "request" to read incoming HTTP headers (e.g. cookies)
    // and HTML form data (e.g. data the user entered and submitted)
    
    // Use "response" to specify the HTTP response line and headers
    // (e.g. specifying the content type, setting cookies).
    
    PrintWriter out = response.getWriter();
    // Use "out" to send content to browser
  }
}

Servlet engines


HelloWorld servlet (plain text)

Servlet return

If a request is made to the server, it returns


telnet localhost 8088
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /jan/servlet/HelloWorld HTTP/1.1

HTTP/1.0 200 OK
Servlet-Engine: Tomcat Web Server/3.2.1 (JSP 1.1; Servlet 2.2; Java 1.4.2; Linux 2.6.10-1.12_FC2custom i386; java.vendor=Sun Microsystems Inc.)

Hello World
Connection closed by foreign host.

HelloWorld servlet (HTML)

Servlet return


telnet localhost 8088
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /jan/servlet/HelloWWW HTTP/1.1

HTTP/1.0 200 OK
Content-Type: text/html
Servlet-Engine: Tomcat Web Server/3.2.1 (JSP 1.1; Servlet 2.2; Java 1.4.2; Linux 2.6.10-1.12_FC2custom i386; java.vendor=Sun Microsystems Inc.)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Hello WWW</TITLE></HEAD>
<BODY>
<H1>Hello WWW</H1>
</BODY></HTML>
Connection closed by foreign host.


Deploying servlets


HTTP headers

HTTP headers can be found by


Servlet return

The output from a request by Firefox with language set to French, English US and then English is


Cache-Control max-age=0
Host localhost:8088
Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1
Keep-Alive 300
Accept-Language fr,en-us;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive


Getting user locales

The following program uses the Accept-Language field to find possible locales:


Getting properties

The following program checks if there is a properties file matching any of the desired locales

Form parameters


package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ThreeParams extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Reading Three Request Parameters";
    out.println(ServletUtilities.headWithTitle(title) +
                "<BODY>\n" +
                "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
                "<UL>\n" +
                "  <LI>param1: "
                + request.getParameter("param1") + "\n" +
                "  <LI>param2: "
                + request.getParameter("param2") + "\n" +
                "  <LI>param3: "
                + request.getParameter("param3") + "\n" +
                "</UL>\n" +
                "</BODY></HTML>");
  }

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}

Cookies

Session tracking

Setting the charset

Form parameter encoding


Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Fri May 13 10:17:44 EST 2005
Copyright ©Jan Newmarch