You are on page 1of 5

Unit 3: Servlet

Servlet is basically a JAVA program that runs particularly inside JVM or {JAVA Virtual
Machine} on the Web server. It is generally used to make/create Dynamic web applications
and web pages.
It is a technology that is used to develop the Dynamic web applications. It uses the JAVA
language for its work of developing web pages. This technology is secured, scalable, and
robust because it uses JAVA technology which provides the same features.

This technology is very similar to CGI {Common Gateway Interface} but due to the
drawbacks of CGI Servlet overcame it. If we talk about the difference the main difference
between them is efficient and portable and data sharing is possible where it can handle the
cookies but on the other hand, CGI is not portable and sharing data is impossible then
resultant it cannot handle the cookies also.
Servlets API’s builds from two packages:
Javax.servlet {basic}
Javax.servlet http{advance}
Servlets Components
Servlet, ServletRequest, Filter, ServletConfig, HttpServlet, ServletResponse, GenericServlet,
HttpServletRequest

Life Cycle and Working of Servlet


The life cycle of a servlet can be summed up in below mentioned five points:
1) The Servlet class is loaded.
2) The Servlet instance is created.
3) The init() method is invoked in order to initialize the servlet.
4) The service() method is invoked repeatedly for each client request placed.
5) The servlet is destroyed using destroy() method.

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 1


Step 1: Loading.
Whenever the server starts the container of it deploys and loads all the servlet. During this
step it creates ServletContext Object which is an interface to communicate easily with the
container.
Step 2: Creating Instance of Servlet.
When all the Servlet classes are loaded, the container creates the instance for each class. The
container creates only one instance for a single class and requests to the servlet executed on
the same instance.
Step 3: Invoke init() method.
When all instances created then its init() method is invoked. This method is used for the
initialization. There are many init parameter which we can specify in the web.xml file.
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.XYZ.MyServletDemo</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Step 4: Invoke Service() method.
Every time the server receives requests for the servlet, it gets a new thread that calls service()
method. When the Servlet is Generic then request is served by service()method, HttpServlet
then service() method receives the request
Step 5: Invoke destroy() method.
When Container shutdown, then it unloads all the servlet classes and interface and calls
destroy() method for each initialized It.

Types of Servlets
There are two main types of Servlet. They are Generic and HTTP servlets. We can use the
constructor method to initialize the Servlets with the help of init() and the destructor method
to remove the servlet from the resources using destroy(). There is a separate method called
service() to handle the servlet requests but they are handled in a different manner in both the
servlets.
Generic servlets extend javax.servlet.GenericServlet –
It is protocol independent servlet. Generic Servlet is a base class servlet from which all other
Servlets are derived. Generic Servlet supports for HTTP, FTP and SMTP protocols. It
implements the Servlet and ServletConfig interface. It has only init() and destroy() method of
ServletConfig interface in its life cycle. It also implements the log method of ServletContext
interface.

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 2


HTTP servlets extend javax.servlet.HttpServlet
HTTPServlet is HTTP dependent servlet. The HTTP protocol is a set of rules that allows
Web browsers and servers to communicate. When Web browsers and servers support the
HTTP protocol, Java-based web applications are dependent on HTTP Servlets.HttpServlet is
Extended by Generic Servlet. It provides an abstract class for the developers for extend to
create there own HTTP specific servlets.

Session in Servlet
Session Tracking is remembering and recording of client conversion in span of time. It is also
called as session management.
If web application is capable of remembering and recording of client conversion in span of
time then that web application is called as stateful web application.
We need session tracking because
 Http protocol is stateless, to make stateful between client and server we need Session
Tracking.
 Session Tracking is useful for online shopping, mailing application, E-Commerce
application to track the conversion.
 Http protocol is stateless, that means each request is considered as the new request.
You can see in below image.
Servlet technology allows four technique to track conversion, they are;
Cookies (Partial Session Tracking)
URL Rewriting (Complete Session Tracking)
Hidden Form Field (Partial Session Tracking)
HttpSession (Complete Session Tracking)

Cookie Class in Servlet


Cookies enable you to store the session information on the client side. We can classify the
cookie based on their expiry time:
 Session
 Persistent
1) SessionCookies:
Session cookies do not have expiration time. It lives in the browser memory. As soon as the
web browser is closed this cookie gets destroyed.

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 3


2) Persistent Cookies:
Unlike Session cookies they have expiration time, they are stored in the user hard drive and
gets destroyed based on the expiry time.
Advantage
• Persistence: One of the most powerful aspects of cookies is their persistence. When a
cookie is set on the client’s browser, it can persist for days, months or even years. This makes
it easy to save user preferences and visit information and to keep this information available
every time the user returns to your site. Moreover, as cookies are stored on the client’s hard
disk so if the server crashes they are still available.
• Transparent: Cookies work transparently without the user being aware that information
needs to be stored. They lighten the load on the server’s memory.
How to send Cookies to the Client
Here are steps for sending cookie to the client:
Create a Cookie object.
Set the maximum Age.
Place the Cookie in HTTP response header.
1) Create a Cookie object:
Cookie c = new Cookie("userName","Chaitanya");
2) Set the maximum Age:
By using setMaxAge () method we can set the maximum age for the particular cookie in
seconds.
c.setMaxAge(1800);
3) Place the Cookie in HTTP response header:
We can send the cookie to the client browser through response.addCookie() method.
response.addCookie(c);

Servlet with JDBC


Servlet with JDBC, JDBC is an application programming interface (JDBC API) that
characterizes an arrangement of standard operations for connecting with DBMS. The DBMSs
might be situated on a remote machine associated with the Internet. Keeping in mind the end
goal to get to a database under a particular DBMS, for instance, PostgreSQL, one must have a
driver for that DBMS and the driver must actualize JDBC API.
The term JDBC stands for Java Database Connectivity. JDBC is a standard Java API for
database and used for connecting the wide range database and the Java programming
language. In servlet connecting to the database is an important task why because while

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 4


dealing with big projects more databases will be used. All the databases will support, but the
class should be mentioned in the code and username and password of the database are
needed. The following task can be done using the JDBC library class.
 By using JDBC, one can connect to a database.
 One can create SQL or MYSQL statements using JDBC.
 The records can be viewed and modified using JDBC.
 The SQL or MYSQL queries can be executed in the database using JDBC.

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 5

You might also like