Page 1 of 1

Servlets Bits and pieces init(),service() explained Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Post icon  Posted 21 February 2008 - 07:54 AM

Attached File  tutorial_2.zip (69.65K)
Number of downloads: 223

This tutorial is in sequence to my previous tutorial on servlets (here). This goes one step further to explain the things.
Lets get started.

The javax.servlet package contains the servlets related classes. The more specific are one level further down this package like all the classes that help in dealing with http requests are present in javax.servlet.http package. Here we will be creating a http based servlet and will understand.

First of all you need to import the related classes which are :

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




Then declare the class as usual but extend HttpServlet class to make this class a servlet.
public class CheckServlet extends HttpServlet {
}



We will be checking the multithreaded behaviour of servlets so lets declare a static variable as:

public class CheckServlet extends HttpServlet {
    static int count=0;
}



The HttpServlet class extends GenericServlet class (a abstract class) which in turn implements servlet interface.
There is a init() method declared in GenericServlet class (fully implemented) which saves a reference to ServletConfig (just remember it). Our servlet will be overriding this method to do some initializations as:

public class CheckServlet extends HttpServlet {
    static int count=0;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
}



The above method first calls the GenericServlet’s init to allow it what it does (saving a reference to ServletConfig object to implicit config reference) and then does the initialization of a local variable.
One more point to notice about init() method is that init() is called once per initialization of servlet and not for every request (i.e. first time the url for servlet is hit).

The method which is called every time a browser (though not same) hits the servlet url is the service(HttpServletRequest request, HttpServletResponse response). Our servlet declares this method as:

public class CheckServlet extends HttpServlet {
    static int count=0;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    public void service (HttpServletRequest request,HttpServletResponse response) throws                        IOException {
        PrintWriter writer = response.getWriter();
        writer.println(++count);
        writer.println("The name as specified in web.xml :" + getServletName() );
    }
}



Here we are sending the value of our count variable to the servlet. Since the variable is static, each servlet will receive the incremented value as updated by the previous invocations of the servlet.

One more thing that we are doing here is that we are getting the name of servlet using the getServletName() method which is again implemented in GenericServlet class. This method gets us the name of servlet as declared in the web.xml file.

As in the previous tutorial, the web.xml is as:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>CheckServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
                <servlet-name>MyServlet</servlet-name>
                <url-pattern>/newServlet</url-pattern>
    </servlet-mapping>
     
</web-app>



Just to recap what we learnt from this tutorial:

1)Servlets are multithreaded (our static variable uses the value as updated by previous servlet hit)
2)init() method is called for the first servlet hit after starting the web server and it saves a reference to ServletConfig object in implicit config reference.
3)service() method is called for each servlet hit.

The source code for this tutorial is attached alongwith.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1