Servlet Essentials 1.3.5

2.  Servlets Step by Step

This chapter acts as a Servlet tutorial. You will learn how to use important techniques for Servlet development by writing some typical Servlets, ranging from very simple to rather complex. All examples in this chapter are fully functional and complete Servlets which have been successfully compiled and run.

2.1  Hello World!

This section shows how to

We start our venture into Servlet programming with the well-known "Hello World" example, this time named more suitably "Hello Client":

HelloClientServlet.java

 1:  import java.io.*;
 2:  import javax.servlet.*;
 3:  import javax.servlet.http.*;
 4:
 5:  public class HelloClientServlet extends HttpServlet
 6:  {
 7:    protected void doGet(HttpServletRequest req,
 8:                         HttpServletResponse res)
 9:              throws ServletException, IOException
10:    {
11:      res.setContentType("text/html");
12:      PrintWriter out = res.getWriter();
13:      out.println("<HTML><HEAD><TITLE>Hello Client!</TITLE>"+
14:                  "</HEAD><BODY>Hello Client!</BODY></HTML>");
15:      out.close();
16:    }
17:
18:    public String getServletInfo()
19:    {
20:      return "HelloClientServlet 1.0 by Stefan Zeiger";
21:    }
22:  }

When you compile this Servlet and run it by requesting a URL which is assigned to it in a Web Browser it produces the following output:

A browser window showing the text "Hello Client!"

Let's have a look at how the Servlet works.


                Servlet Essentials 1.3.5