You are on page 1of 3

ToC

Chapter 2: First Servlets


2.1 Basic Servlet Structure
2.2 A Simple Servlet Generating Plain Text
- Compiling and Installing The Servlet
- Invoking The Servlet
2.3 A Servlet That Generates HTML
2.4 Packaging Servlets
- Creating Servlets In Packages
- Compiling Servlets In Packages
- Invoking Servlets In Packages
2.5 Simple HTML Building Utilities
2.6 The Servlet Life Cycle
- The init bethod
- The servlet bethod
- the doGet, doPost, doXXX methods
- the SingleThreadModel Interface
- the destroy method
2.7 An Example Using Initialization Parameters
2.8 An Example Using Servlet Initialization And Page Modification Dates
2.9 Debugging Servlets
2.10 WebClient: Talking To WebServers Interactively
- Webclient
- HttpClient
- NetworkClient
- SocketUtil
- CloseableFrame
- LabeledTextField
- Interruptible

Chapter 2: First Servlets

Topics in this chapter


- basic structure of servlets
- a simple servlet that generates plain text
- the process of compiling, installing, invoking servlets
- a servlet that generates html
- some utilities to help build servlets
- the life cycle of servlets
- an example of reading initialization parameters
- an example that uses initialization and page modification dates
- servlet debugging techniques
- a tool for interactively talking to servlets (??)

2.1 Basic Servlet Structure

Listing 2.1

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletTemplate extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException{
PrintWriter out = response.getWriter(); // use 'out' to send content to
the browser.
}

the HttpServletRequest (object) has methods to extract incoming information like


form data, HttpRequest Headers, client host name etc
the HttpServletRespones (object) has methods to specify outgoing information such
as Http Status Codes, response headers (Content type, Set-Cookie etc) and most
importantly gives you a PrintWriter to send content back to the client.

For simple servlets, most of the effort is spent on the (writer.)println statements
that generate the desired page.

doGet ad doPost throw two exceptions so you have to include them in the
declaration.
you import java.io (for PrintWriter etc), javax.Servlet (for HttpServlet etc) and
javax.servlet.http for HttpServletRequest, HttpServletResponse etc

Strictly speaking HttpServlet is not the only starting point for servlets.
Servlets could, in principle, extend mail, FTP (etc) servers, Servlets for these
environments would extend GenericServlet, the parent class of HttpServlet. In
practice though, servlets are used exclusively for servers that communicate with
Http (web and app servers) and in this book, we focus on these.

2.2 A Simple Servlet Generating Plain Text

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

public class HelloWorld extends HttpServlet{


public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException{
PrintWriter out = response.getWriter();
out.println("Hello World");
}

- Compiling and Installing The Servlet

Step 1: CLASSPATH has to point to servlet classes.


- Invoking The Servlet
2.3 A Servlet That Generates HTML
2.4 Packaging Servlets
- Creating Servlets In Packages
- Compiling Servlets In Packages
- Invoking Servlets In Packages
2.5 Simple HTML Building Utilities
2.6 The Servlet Life Cycle
- The init bethod
- The servlet bethod
- the doGet, doPost, doXXX methods
- the SingleThreadModel Interface
- the destroy method
2.7 An Example Using Initialization Parameters
2.8 An Example Using Servlet Initialization And Page Modification Dates
2.9 Debugging Servlets
2.10 WebClient: Talking To WebServers Interactively
- Webclient
- HttpClient
- NetworkClient
- SocketUtil
- CloseableFrame
- LabeledTextField
- Interruptible

You might also like