0% found this document useful (0 votes)
143 views11 pages

Introduction To Servlets

Servlets provide a component-based, platform-independent method for building dynamic web applications and allow Java code to run on a web or application server. Servlets act as a middle layer between HTTP requests from a web browser or client and databases or applications on the server. Using servlets, web pages and content can be dynamically generated by collecting input from users, accessing databases, and performing other tasks. Servlets offer better performance than CGI programs since they reuse threads rather than creating new processes for each request.

Uploaded by

prasanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views11 pages

Introduction To Servlets

Servlets provide a component-based, platform-independent method for building dynamic web applications and allow Java code to run on a web or application server. Servlets act as a middle layer between HTTP requests from a web browser or client and databases or applications on the server. Using servlets, web pages and content can be dynamically generated by collecting input from users, accessing databases, and performing other tasks. Servlets offer better performance than CGI programs since they reuse threads rather than creating new processes for each request.

Uploaded by

prasanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Servlet technology is used to create web application (resides at server side

and generates dynamic web page).


Servlets provide a component-based, platform-independent method for
building Web-based applications, without the performance limitations of
CGI programs.
Servlets have access to the entire family of Java APIs, including the JDBC
API to access enterprise databases.
Java Servlets are programs that run on a Web or Application server and act
as a middle layer between a requests coming from a Web browser or other
HTTP client and databases or applications on the HTTP server.
Using Servlets, you can collect input from users through web page forms,
present records from a database or another source, and create web pages
dynamically.

What is a Servlet?
Servlet can be described in many ways, depending on the context.

Servlet is a technology i.e. used to create web application.

Servlet is an API that provides many interfaces and classes including documentations.

Servlet is an interface that must be implemented for creating any servlet.

Servlet is a class that extend the capabilities of the servers and respond to the incoming
request. It can respond to any type of requests.

Servlet is a web component that is deployed on the server to create dynamic web page

What is web application?


A web application is an application accessible from the web. A web application is composed of
web components like Servlet, JSP, Filter etc. and other components such as HTML. The web
components typically execute in Web Server and respond to HTTP request.
CGI(Commmon Gateway Interface)
CGI technology enables the web server to call an external program and pass HTTP request
information to the external program to process the request. For each request, it starts a new
process.

Disadvantages of CGI
There are many problems in CGI technology:
1. If number of clients increases, it takes more time for sending response.
2. For each request, it starts a process and Web server is limited to start processes.
3. It uses platform dependent language e.g. C, C++, perl.

Advantage of Servlet

There are many advantages of servlet over CGI. The web container creates threads for handling
the multiple requests to the servlet. Threads have a lot of benefits over the processes such as they
share a common memory area, lightweight, cost of communication between the threads are low.
The basic benefits of servlet are as follows:
1. better performance: because it creates a thread for each request not process.
2. Portability: because it uses java language.
3. Robust: Servlets are managed by JVM so no need to worry about momory leak, garbage
collection etc.
4. Secure: because it uses java language..

What are the Characteristics of Servlets?


The characteristics of servlets that have gained them a wide spread acceptance are as follows:
1.Servlets are efficient:
The initialization code for a servlet is executed only when the servlet is executed for the first
time.Subsequently, the requests that are received by the servlet are processed by its service()
method.This helps to increase the efficiency of the server by avoiding creation of unnecessary
processes.
2.Servlets are robust:
As servlets are based on Java,they provide all the powerful features of Java.such as exception
handling and garbage collection,which make them robust.
3.Servlets are portable:
Servlets are also portable because they are developed in Java. This enables easy portability
across Web servers
.

4.Servlets are persistent:


Servlets helps to increase the performance of the system by preventing frequent disk access.
For ex.if a customer logs on to www.EarnestOnline.com, the customer can perform many
activities,such as checking for the balance. applying for a loan etc.
In every stage the customer needs to be authenticated by checking for the account
number against the database. Instead of checking for the account number against
the database every time. servlets retain the account number in the memory till the
user logs out of the Web site.

Servlet Architecture:

Servlets Tasks:

Servlets perform the following major tasks:

Read the explicit data sent by the clients (browsers). This includes an HTML form on a
Web page or it could also come from an applet or a custom HTTP client program.

Read the implicit HTTP request data sent by the clients (browsers). This includes
cookies, media types and compression schemes the browser understands

Process the data and generate the results. This process may require talking to a database,
executing an RMI or CORBA call, invoking a Web service, or computing the response
directly.

Send the explicit data (i.e., the document) to the clients (browsers). This document can be
sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel,
etc.

Send the implicit HTTP response to the clients (browsers). This includes telling the
browsers or other clients what type of document is being returned (e.g., HTML), setting
cookies and caching parameters, and other such tasks.

Servlet Life Cycle:


The web container maintains the life cycle of a servlet instance.
1. Servlet class is loaded.
2. Servlet instance is created.
3. init method is invoked.

4. service method is invoked.


5. destroy method is invoked.

The init() method :


The init method is designed to be called only once. It is called when the servlet is first created,
and not called again for each user request. So, it is used for one-time initializations, just as with
the init method of applets.
The servlet is normally created when a user first invokes a URL corresponding to the servlet, but
you can also specify that the servlet be loaded when the server is first started.
When a user invokes a servlet, a single instance of each servlet gets created, with each user
request resulting in a new thread that is handed off to doGet or doPost as appropriate. The init()
method simply creates or loads some data that will be used throughout the life of the servlet.
The init method definition looks like this:
public void init() throws ServletException {
// Initialization code...
}

The service() method :

The service() method is the main method to perform the actual task. The servlet container (i.e.
web server) calls the service() method to handle requests coming from the client( browsers) and
to write the formatted response back to the client.
Each time the server receives a request for a servlet, the server spawns a new thread and calls
service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.)
and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
Here is the signature of this method:
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}

The service () method is called by the container and service method invokes doGet, doPost,
doPut, doDelete, etc. methods as appropriate. So you have nothing to do with service() method
but you override either doGet() or doPost() depending on what type of request you receive from
the client.
The doGet() and doPost() are most frequently used methods with in each service request.

The doGet() Method


A GET request results from a normal request for a URL or from an HTML form that has no
METHOD specified and it should be handled by doGet() method.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}

The doPost() Method


A POST request results from an HTML form that specifically lists POST as the METHOD and it
should be handled by doPost() method.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}

The destroy() method :

The destroy() method is called only once at the end of the life cycle of a servlet. This method
gives your servlet a chance to close database connections, halt background threads, write cookie
lists or hit counts to disk, and perform other such cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage collection. The
destroy method definition looks like this:
public void destroy() {
// Finalization code...
}
doPUT: The PUT method is a complement of a GET request, and PUT stores the entity body
at the location specified by the URI. It is similar to the PUT function in FTP.

doPut
protected void doPut(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
Called by the server (via the service method) to allow a servlet to handle a PUT request.
The PUT operation allows a client to place a file on the server and is similar to sending a
file by FTP.
doDelete
protected void doDelete(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
Called by the server (via the service method) to allow a servlet to handle a DELETE
request. The DELETE operation allows a client to remove a document or Web page from
the server.
doDELETE: The DELETE method is used to delete a document from the server. The
document to be deleted is indicated in the URI section of the request.

Difference between doGet and doPost methods:


In doGet Method the parameters are appended to the URL and sent along with header
information In doPost, parameters are sent in separate line in the body.
Maximum size of data that can be sent using doget is 240 bytes There is no maximum size for
data
Parameters are not encrypted Parameters are encrypted
DoGet method generally is used to query or to get some information from the server Dopost is
generally used to update or post some information to the server
DoGet is faster if we set the response content length since the same connection is used. Thus
increasing the performance DoPost is slower compared to doGet since doPost does not write the
content length
DoGet should be idempotent. i.e. doget should be able to be repeated safely many times This
method does not need to be idempotent. Operations requested through POST can have side
effects for which the user can be held accountable, for example, updating stored data or buying
items online.
DoGet should be safe without any side effects for which user is held responsible

Servlet API
1. Servlet API
2. Interfaces in javax.servlet package
3. Classes in javax.servlet package
4. Interfaces in javax.servlet.http package
5. Classes in javax.servlet.http package

The javax.servlet and javax.servlet.http packages represent interfaces and classes for servlet api.
The javax.servlet package contains many interfaces and classes that are used by the servlet or
web container. These are not specific to any protocol.
The javax.servlet.http package contains interfaces and classes that are responsible for http
requests only.

Interfaces in javax.servlet package:


There are many interfaces in javax.servlet package. They are as follows:
1. Servlet
2. ServletRequest

3. ServletResponse
4. RequestDispatcher
5. ServletConfig
6. ServletContext
7. SingleThreadModel
8. Filter
9. FilterConfig
10.FilterChain
11.ServletRequestListener
12.ServletRequestAttributeListener
13.ServletContextListener
14.ServletContextAttributeListener

Classes in javax.servlet package:


There are many classes in javax.servlet package. They are as follows:
1. GenericServlet
2. ServletInputStream
3. ServletOutputStream
4. ServletRequestWrapper
5. ServletResponseWrapper
6. ServletRequestEvent
7. ServletContextEvent
8. ServletRequestAttributeEvent
9. ServletContextAttributeEvent
10.ServletException

11.UnavailableException

Interfaces in javax.servlet.http package


There are many interfaces in javax.servlet.http package. They are as follows:
1. HttpServletRequest
2. HttpServletResponse
3. HttpSession
4. HttpSessionListener
5. HttpSessionAttributeListener
6. HttpSessionBindingListener
7. HttpSessionActivationListener
8. HttpSessionContext (deprecated now)

You might also like