Java Servlets Introduction

The software you need for the tutorials or the examples.

  1. Install the J2SE Development Kit.
  2. Install Eclipse Helios IDE tool.
  3. Install Oracle Weblogic 10.3 server.
  4. At last you must add the Oracle Weblogic 10.3 server to the Eclipse Helios IDE tool.
  • All source code examples in this session is part of a complete example, ServletSimpleApp, that you can download.

What is Servlets?

  • Servlets are Java classes that are loaded and executed by a servlet container, and can run as a component in a Web-server or a Java Enterprise Edition Application server.
  • Servlets are designed to accept a response from a client (usually a web browser), process that request, and return a response to the client.
  • The base class for all servlets is javax.servlet.GenericServlet, which defines a generic, protocol-independent servlet.
  • The most common used type of servlet is an HTTP servlet which extends the javax.servlet.http.HttpServlet class which is a subclass of javax.servlet.GenericServlet.
  • When you create a servlet, that extends the javax.servlet.http.HttpServlet class, you must override one or more methods to respond to specific HTTP requests.
  1. When a client request is received by a web server, it will deliver the request to the servlet container that will start and deliver the request to the servlet.
  2. The Servlet receives the request and returns a response to its container which will deliver the response to the web server that returns the response to the client.

Alternative requests:

  • A client (usually a web browser) will send a request of a kind:
    HTTP request Description HttpServlet Method
    GET Retrieves information identified by a request Uniform Resource Identifier (URI). doGet(HttpServletRequest request, HttpServletResponse response)
    POST Requests that the server pass the body of the request to the resource identified by the request URI for processing. doPost(HttpServletRequest request, HttpServletResponse response)
    HEAD Returns only the header of the response that would be returned by a GET request. doHead(HttpServletRequest request, HttpServletResponse response)
    PUT Uploads data to the server to be stored at the given request URI. The main difference between this and POST is that the server should not further process a PUT request, but simply store it at the request URI. doPut(HttpServletRequest request, HttpServletResponse response)
    DELETE Deletes the resource identified by the request URI. doDelete(HttpServletRequest request, HttpServletResponse response)
  • The most common requests are GET and POST.
  • When you implement a new servlet class, extending the javax.servlet.http.HttpServlet , you need to override the right Http Servlet methods depending on which HTTP request you gets from the client.
    A simple Servlet example:
    public class SimpleServlet extends HttpServlet
    // doGet receives the GET request.
      protected void doGet(HttpServletRequest request,HttpServletResponse response)
          PrintWriter printWriter = response.getWriter();
          printWriter.println("If you are reading this, " +
                  "your Servlet works fine!");
        catch (IOException ioException)
    How to try examples.
  • In this servlet we expect a GET Http request and we do not use any information from the request object.
  • We set the Content type to "text/html" for the response to the client (browser) and use a PrintWriter on the response object to return html codes and text back to the client.
  • To run this Servlet you have to deploy it to a web-server or a Application server (I am using Weblogic 10.3 application server and Eclipse Helios IDE tool ) and you will find how to install this under the Tutorials).
  • To deploy means to install the Servlet with some instruction to a such server.
  • These instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.
  • The contents of the web.xml file regarding servlet, SimpleServlet is usually automatically generated by the IDE tool when you create the Servlet and should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns=""
    How to try examples.
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a Servlet.
  • With a servlet tag we give the Servlet class a servlet name, which is used in the servlet-mapping tag to specify a url for the Servlet.
  • In this way we can have many urls for the same servlet.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the url of the servlet.
  • The context-root (in example /ServletSimpleApp) for the application will in most cases be specified by a server vendor deployment descriptor.
  • Here is the Weblogic 10.2 deployment descriptor weblogic.xml for deployment in this example:
    <?xml version="1.0" encoding="UTF-8"?>
    <wls:weblogic-web-app xmlns:wls=""
    How to try examples.
  • Everything must at last be build into a war file which is a zipped file and can be build with the java jar tool or with a zip generator application.
  • The files containing in the war file should be organized like this:

  • You can (in most cases) create and deploy the war file to the server with your selected IDE tool.
  • When we have deployed this servlet and run it in a browser with http://localhost:7001/ServletSimpleApp the result back to the browser from the Servlet should be:

    If you are reading this, your Servlet works fine!

  • All source code examples in this session is part of a complete example, ServletSimpleApp, that you can download.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.