You are on page 1of 3

http://download.oracle.

com
http://www.roseindia.net/
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html
***********************************
•The objective of server side programs is to centrally manage all programs relating to a particular
application (e.g. Banking, Insurance, e-shopping, etc).
•Clients with bare minimum requirement (e.g. Pentium II, Windows XP Professional, MS Internet
Explorer and an internet connection) can experience the power and performance of a
Server (e.g. IBM Mainframe, Unix Server, etc) from a remote location without having to
compromise on security or speed.
•More importantly, server programs are not only portable but also possess the capability to
generate dynamic responses based on user’s request.

***********************************
•A servlet container is nothing but a compiled, executable program. The main function of the
container is to load, initialize and execute servlets
***********************************
•It is interesting to note here that the container and the objects in a container are multithreaded.
***********************************
•Servlets are server as well as platform-independent.
***********************************
•Servlets run entirely inside the Java Virtual Machine. Since the Servlet runs at server side so it
does not checks the browser for compatibility
***********************************
•Servlets uses the classes in the java packages javax.servlet and javax.servlet.http
***********************************
HTTP Servlet typically used to:

•Priovide dynamic content like getting the results of a database query and returning to the client.
•Process and/or store the data submitted by the HTML.
•Manage information about the state of a stateless HTTP. e.g. an online shopping car manages
request for multiple concurrent customers.
***********************************
Now lets see what a servlet container does for each HTTP request for a servlet, in general :

•The servlet container loads the servlet class and calls the init method of the servlet as soon as
the servlet is called for the first time.
•Then this container makes an instance of javax.servlet.ServletRequest and
javax.servlet.ServletResponse for each request.
•Then it passes the ServletRequest and ServletResponse objects by invoking the servlet's
service method.
•Finally, it calls the destroy method and unload the servlet class when the servlet class is to be
shut down.
***********************************
A Generic servlet contains the following five methods:

•public void init(ServletConfig config) throws ServletException


The servlet cannot be put into the service if:
• The init() method does not return within a fix time set by the web server.
• It throws a ServletException

•public void service(ServletRequest req, ServletResponse res) throws ServletException,


IOException
• Once the servlet starts getting the requests, the service() method is called by the
servlet container to respond. The servlet services the client's request with the help
of two objects- javax.servlet.ServletRequest & javax.servlet.ServletResponse are passed
by the servlet container.
•The status code of the response always should be set for a servlet that throws or sends
an error.

•public ServletConfig getServletConfig()


•public String getServletInfo()
•public void destroy()
Once the servlet container calls the destroy() method, no service methods will be then
called . That is after the exit of all the threads running in the servlet, the destroy() method is
called. Hence, the servlet gets a chance to clean up all the resources like memory, threads etc
which are being held.
***********************************

***********************************
Project--->WebContent--->WebINF--->web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>test.HelloServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/rupa</url-pattern>
</servlet-mapping>
</web-app>
***********************************
Project--->src---> HelloServ.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServ extends HttpServlet


{ protected void doGet(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException
{ resp.setContentType("text/html");
PrintWriter out= resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello World</h1>");
out.println("</body>");
out.println("</html>");
}
public void destroy(){}
}
***********************************
Project--->WebContent--->Hello.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Displaying Hello World Servlet</title>
</head>
<body>
<form action="HelloServ" >
</form>
</body>
</html>

You might also like