Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,484 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,750 people online right now. Registration is fast and FREE... Join Now!




Servlets Bits and pieces

 
Reply to this topicStart new topic

> Servlets Bits and pieces, init(),service() explained

bhandari
Group Icon



post 21 Feb, 2008 - 06:54 AM
Post #1


Attached File  tutorial_2.zip ( 69.65k ) Number of downloads: 73


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 :

java
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.
java
public class CheckServlet extends HttpServlet {
}


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

java
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:

java
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:

java
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:

java
<?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.



Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 07:09PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month