Servlet step-by-step

This is an example of the simple servlet, you will see in the next lines how to write and display html pages, and some more complex things. Also, you will get an explenation for every line of the code.

Hello World!

This servlet will provide you with information on how to build a static content:

All the examples are available with any of our tomcat web hosting solutions.

HelloServlet.java

1: import javax.servlet.http.* 2: import javax.servlet.*; 3: import java.io.*; 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 World!</TITLE>"+ 14: "</HEAD><BODY>Hello World!</BODY></HTML>"); 15: out.close(); 16: } 17: 18: public String getServletInfo() 19: { 20: return "Your first Hello Servlet!"; 21: } 22: }

When this servlet is compilied you will get the "Hello World!" page.

Explaination to the lines of code:

First 3 lines indicate the packages you need to load, so you can use method and procedures of such:

1: import javax.servlet.http.* 2: import javax.servlet.*; 3: import java.io.*;

We declared the servlet class at line 5. This servlet extends javax.servlet.http.HttpServlet, the standard base class for HTTP Servlets.

5: public class HelloClientServlet extends HttpServlet

In the lines from 7 - 16 HttpServlet's doGet method is getting overridden.

7: protected void doGet(HttpServletRequest req, 8: HttpServletResponse res) 9: throws ServletException, IOException 10: { ... 16: }

HttpServletResponse method is used at the line 11, object to set the content type of the response that we are going to send. You must set all response headers before you request a PrintWriter or ServletOutputStream to send body data to the response.

11: res.setContentType("text/html");

PrintWriter object is requested at line 11 to write a response content type

12: PrintWriter out = res.getWriter();

[API 2.0] ServletResponse.getWriter() is a new feature of JSDK version 2.0. If your Servlet engine does not support JSDK 2.0 you can replace the above line by "ServletOutputStream out = res.getOutputStream();". This change can be made in most of the example Servlets in this tutorial. The advantages of using ServletResponse.getWriter() are discussed in section 4.4.

In lines 13 and 14 we use the PrintWriter to write the text of type text/html (as specified through the content type).

13: out.println("<HTML><HEAD><TITLE>Hello World!</TITLE>"+ 14: "</HEAD><BODY>Hello World!</BODY></HTML>");

The PrintWriter is closed, when the writing to it is finished.

15: out.close();

This line is included for completeness. It is not strictly necessary. The Web Server closes the PrintWriter or ServletOutputStream automatically when a service call returns. An explicit call to close() is useful when you want to do some post processing after the response to the client has been fully written. Calling close() tells the Web Server that the response is finished and the connection to the client may be closed as well.

In lines 18 through 21 we override the getServletInfo() method which is supposed to return information about the Servlet, e.g. the Servlet name, version, author and copyright notice. This is not required for the function of the HelloClientServlet but can provide valuable information to the user of a Servlet who sees the returned text in the administration tool of the Web Server.

18: public String getServletInfo() 19: { 20: return "Your first Hello Servlet!"; 21: }
Contact sales!