Java Servlets

Home  »    Java   »    Advance Java  »  Java Servlets


Java servlets are the Java programs that are used to handle server-side operations in client-server architecture. CGI scripts were used prior to Java servlet for client-server computing. CGI scripts were written in C or Perl programming language. Java servlets are platform independent and therefore provide enhanced features for client-server operations. Java compiled Java servlets can be invoked multiple times by the Web servers without any need of recompilation. You need not compile sevlet each time they are executed because the init() method in Java servlet is invoked only once in a servlet life cycle. Once the servlet is initialised and compiled, it converts into the native code. This native code of Java servlet is now ready to handle client requests without any need of recompilation. This feature of Java servlet makes it more suitable to handle server-side requests on heterogeneous networks such as the Internet. The various steps involved in using Java servlet to handle server-side requests are:

  1. The client program running on the Web browser sends and Http request to the Web server.
  2. The Web server accepts the client's request, loads and executes the Java servlet in the Java Virtual Machine (JVM) and forwards the client's request to the servlet.
  3. The servlet performs the operation specified by the Http request and sends the result of the desired operation back to the Web server.
  4. The Web server sends to response back to the requesting client using the HttpResponse() method of the Java servlet.



Using a Java Servlet


Diagram Using a Java Servlet


A Java servlet is a Java class that implements the javax.servlet.Servlet interface of Java. There are two types of Java servlet, generic servlet that implements the javax.servlet.GenericServlet interace and the Http servlet, which implements the javax.servlet.http.HttpServlet interface.



Features of Servlets

JAVA Servlets Provides various Services which helps in accessing the Resources those are Located on the Server and There are Many Advantages over using the Servlets.

  1. Persistent : The Persistent Features of Servlet Makes it , as a Server which handles Request of Many Clients at a same Time without Recompiling a Servlet.

  2. Fast : Servlets Provides fast Services to the Client because they are easily Loaded and they Loaded only once in the Memory.

  3. Platform Independent :Servlets are Executed on any Machine once Compiled.

  4. Secure : Servlets are very Secure because they doesn't Provide an information that how the Request of a Client is Processed.

  5. Efficient : As I Mention Earlier that Servlet of java can handle Request of Many users. So this Makes it Efficient.

  6. Portables : Servlets are Portable Means they can run on various Platforms without Modifications.



Requirements of JAVA Servlets

If we wants to Execute Servlets o our Machine then first we have to fulfills all the Software's Requirements which a Servlet uses. Requirements of JAVA Servelets are as below:-

  1. JDK : This is the Main Component for Executing JAVA Servelets So first of all install JAVA on the Machine by using the JAVA development Kit Which provides us various Methods and interfaces for Running Programs of JAVA.

  2. Servlet API : For Executing you have to compile your Servlet by using the Servlet.Jar File by using the Classpath and we can also set the Classpath in which Servelet.jar File resides.

  3. Servlet-Enabled Web Server : Then after Compiling we have to start the Server which will Listen the Request of Client and there are Many Web Servers Available and with the help of those Web Servers handle the Request of Clients. The Various Types of Web Servers are as followings :-

    1. Apache Web Server : This is the stand alone Server which can be used for testing the JAVA servlet and this is very Reliable Server.
    2. Tomcat Server.
    3. Jigsaw Server.
    4. Atlanta Web Server : This is the very useful Software which can be installed on any System like Solaris , MacOS and also on Unix Operating system.
    5. AVA web Server: This Server has a capability to run the JAVA Servlets those are Written only in the JAVA Programming Language.


Servlets API

A Servlet is much like an applet it does not run any application or doesn't have a main() method rather it is loaded in the memory and instance is created and it also provides some methods like an applet but these are different from applet's methods. When a servlet instance is created its init() method is called. But a servlet does not need to become active or de-active like applet. So it does not have start(), stop() or paint() methods. But these are required to give the response to new connections. When a new connection is detected it automatically call the service() method. The service() method takes two arguments first ServletRequest and second is ServletResponse. These interface provides the access methods that allow how a servlet will be executed, what request it has received and what to do to provide a proper response. Basically we can use HTTP for communication. Se we can use HTTP servlet class, which is the subclass of Generic servlet. The HTTP defines two interfaces –

  1. HTTP ServletRequest interface
  2. HTTP ServeletResponse interface
The first interface is used for giving the details of the request, path and query string, and the second is used for sending the HTTP errors and for redirects.


Servlet Life Cycle

A Java servlet extends the HttpServlet class of Java. A Java servlet invokes the init(), service() and destroy() methods during its life cycle. The various steps in a servlet life cycle are:

  1. Create a servlet instance.

  2. The Web server invokes the init(ServletConfig config) method of Java servlet. The argument, config in the init(ServletConfig config) method is an instance of the ServletConfig class. It stores servlet parameters and a refence to the servlet.

  3. The init() method initialises the servlet and is invoked only once in the servlet life cycle.

  4. The service() method is invoked to handle the request made by the Web browser to the Java servlet. You can override the service() method of the Java servlet.

  5. The destroy() method is invoked to destroy a Java servlet and free all the resources that a Java servlet occupies. The destroy() method is also invoked only once in a servlet lifecyle.


Diagram shows a servlet lifecycle.


Servlet Lifecycle


There are three levels in a servlet lifecycle. The load and unload events of a Java servlet call the init() and destroy() methods, respectively. The service() method of the Java servlet is invoked to handle the client requests.



Java Servlet Programming







<< Previous Topic
Next Topic >>



3 comments: