How Java Servlet is Created and Runs?

Servlet is the java class that reads request from a client and responds by sending information to the client. Sounds interesting??? Well, here in this article, I am going to explain about java servlet and will also provide knowledge on how servlet cooperates with the client machine. Here we go…

What is java servlet?

Java Servlet is a server-side program, called by the user interface or other j2ee components and contain the business logic to process a request. Originally a server-side program handles client request by using the common gateway interface(CGI).

Java servlet is the product of Sun Microsystems, replaces the common gateway interface(CGI) programming. Java servlet technology is more efficient and uses less environment overhead then Common gateway interface.

You can use Common gateway interface starts a new process begins each time, a request is made to a common gateway interface program. Starting a new process is time-consuming but acceptable, as long as the execution time of the CGI program is longer than the time that is necessary to begin the new process, on the other hand executing a CGI program becomes unacceptable.

Servlet is the server-side programming technology. It is used to create a web-based application. We use the Servlet technology typically to create dynamic web applications. This also provides many interfaces and classes.

Advantages of Servlet

There are a number of advantages of servlet, some of them are:

  • Servlet technology is more secure and Robustness, the servlet is handled by the JVM(java virtual machine).
  • Java servlet technology avoids inefficiencies. Only one copy of java servlet is loaded into the memory or JVM, no matter the number of simultaneous requests made to the java servlet.
  • It provides better performance.  In this technology, each request begins threads to the java servlet rather than a new process, which saves memory uses on the server and increase response time, only one copy of the java servlet needs to be loaded into the memory.
  • It is persistence. In Servlet technology data is recovered after process termination.
  • This is more secure technology.

Common Gateway Interface (CGI)

CGI stands for common gateway interface. A CGI is a server-side program that receives the request from the client machine and process on the server.  In common gateway interface for every request, a new process starts in the memory.

For Example 

If 100 instance of an application requires a CGI program to process their request simultaneously.

As you know that this technology is old and CGI program must be loaded into memory hundreds(100) times, ie there is hundreds copy of the CGI program in the memory. Each instance must begin a new process, meaning hundreds new process started which is inefficient.

Disadvantages of CGI

  • A CGI program is not persistence, once a CGI program terminates, all data used by the process is lost and can not be used by other CGI programs
  • It becomes more difficult when the number of clients increases.
  •  In  CGI technology for each request, a new process is generated.

Life Cycle of A Java Servlet

Java Servlet technology provides four service method, each of which calls during the life cycle of the java servlet.

These methods are-

  • Init()
  • Service appropriate request method (do get, do post).
  • Destroy () method.

Init() method

The first method is the Init () method.The Init ()method calls automatically when the java servlet creates. Init() method execute only one time during the life cycle of the java servlet. You will not be able to get execute every new request.

public void inits (ServletConfig config) throws ServletException.

Service() method

Service() method calls whenever a request for the Java servlet makes to the web server. In this, each request creates a new thread.

The service() method examines the HttpRequest type and then calls the appropriate request method, such as doGet and doPost.

Example

public void service(ServleRequest request,ServletResponese response) throws ServletException, IOException.

Destroy() method

The destroy() method is the last method. Destroy() method calls before the java servlet terminates, this method is the last method of the servlet life cycle.

Example

public void destroy().

doGet() and doPost() method

Servlet technology uses two service method do Get() and doPost() method. A browser interacts with a web server a most common is GET() method. In which the browser simply request to the content of a particular “URL”(Uniform Resource Locator). When the browser uses this method the web server ends up calling the Servlet doGet() method.

Note

Both the doGet() and doPost() method require two arguments, first is an HttpRequest object and other arguments are HttpResponse object. The first object contains incoming information from the browser and HttpResponse object is uses to send outgoing information to the client. Both methods throw a ServletException and IOException.

The java class must extend HttpServlet and override HttpServlet doGet() and doPst().

method=”GET”.

It gets an attribute from an HTML form.

Servlet uses more secure and cryptography method is “POST ” method.

method=”POST”.

How to Create Servlet?

There are following steps to create Servlet some of them are –

  • First implements the Servlet Interface.
  • Then extends the HttpServlet class.
  • Using GenericServlet class.

Servlet Small Program Example

java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class simple extends HttpServlet

{

public void service(HttpServletRequest,rq,HttpServletResponse rs) throws IOException

{

PrintWriter pw=rs.getWriter();

rs.setContentType("text/plain");

String s=rq.getParameter("text name");

pw.println(s);

}

}

In Notepad(Html File)

<html><head><title>simple program of servlet</title></head>

<body>

<form method="POST" action="http"//localhost"8080/servlet/simple">

<input type="text" name="text name" value="">

<input type="submit">

</form>

</body>

</html>

Note: Run this program using ServletRunner. It is an interpreter.

Set Path

To run this program first save the file and then set the path.

SETCLASSPATH=c:\jsdk2.0\lib\jsdk.jar;%PATH%;.

SETPATH=c:\jsdk2.0\bin;%PATH%;.

Save this file  As

c:\jsdk2.0\example\simple.java.

Here in this article, I have tried to explain about server-side technology “Servlet”. It’s about Servlet, How servlet replaces the CGI (common gateway interface) server-side programming, What are the advantages and more important is the Lifecycle of the Servlet! Also, I have stated the use of different methods used in the servlet along with how servlet life cycle works. The most important method is service method which defines doGET() and doPost() methods in detail.

Hope you enjoyed reading it! For further queries, please feel free to make use of the comments section below!

Leave a Comment