0% found this document useful (0 votes)
102 views

Servlet

The document discusses the basics of web technologies including HTTP, HTML, web servers, clients and browsers. It then covers web applications and how servlets provide a more robust and scalable alternative to CGI for developing dynamic web applications. Key concepts around the servlet API, lifecycle and request handling are also summarized.

Uploaded by

Saurabh Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Servlet

The document discusses the basics of web technologies including HTTP, HTML, web servers, clients and browsers. It then covers web applications and how servlets provide a more robust and scalable alternative to CGI for developing dynamic web applications. Key concepts around the servlet API, lifecycle and request handling are also summarized.

Uploaded by

Saurabh Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to Web

Web consists of billions of clients and server connected through wires and wireless
networks. The web clients make requests to web server. The web server receives the
request, finds the resources and return the response to the client. When a server answers a
request, it usually sends some type of content to the client. The client uses web browser to
send request to the server. The server often sends response to the browser with a set of
instructions written in HTML(HyperText Markup Language). All browsers know how to
display HTML page to the client.

Web Application
A website is a collection of static files(webpages) such as HTML pages, images, graphics
etc. A Web application is a web site with dynamic functionality on the
server. Google, Facebook, Twitter are examples of web applications.

HTTP (Hypertext Transfer Protocol)

• HTTP is a protocol that clients and servers use on the web to communicate.
• It is similar to other internet protocols such as SMTP(Simple Mail Transfer Protocol) and
FTP(File Transfer Protocol) but there is one fundamental difference.
• HTTP is a stateless protocol i.e HTTP supports only one request per connection. This
means that with HTTP the clients connect to the server to send one request and then
disconnects. This mechanism allows more users to connect to a given server over a
period of time.
• The client sends an HTTP request and the server answers with an HTML page to the
client, using HTTP.

HTTP Methods
HTTP request can be made using a variety of methods, but the ones you will use most often
are Getand Post. The method name tells the server the kind of request that is being made,
and how the rest of the message will be formated.
Difference between GET and POST requests

GET Request POST Request

Data is sent in header to the server Data is sent in the request body

Get request can send only limited amount of Large amount of data can be sent.
data
GET Request POST Request

Get request is not secured because data is Post request is secured because data is not
exposed in URL exposed in URL.

Get request can be bookmarked and is more Post request cannot be bookmarked.
efficient.

Servlet
Servlet technology is used to create web application (resides at server side and generates
dynamic web page).

Servlet technology is robust and scalable because of java language. Before Servlet, CGI
(Common Gateway Interface) scripting language was popular as a server-side programming
language. But there was many disadvantages of this technology.

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 we don't need to worry about memory
leak, garbage collection etc.

4. Secure: because it uses java language

CGI vs Servlet
Java Servlet technology was introduced to overcome the shortcomings of CGI
technology.

• Servlets provide better performance that CGI in terms of processing time, memory
utilization because servlets uses benefits of multithreading and for each request a
new thread is created, that is faster than loading creating new Object for each
request with CGI.
• Servlets and platform and system independent, the web application developed
with Servlet can be run on any standard web container such as Tomcat, JBoss,
Glassfish servers and on operating systems such as Windows, Linux, Unix,
Solaris, Mac etc.
• Servlets are robust because container takes care of life cycle of servlet and we
don’t need to worry about memory leaks, security, garbage collection etc.
• Servlets are maintainable and learning curve is small because all we need to take
care is business logic for our application.

Servlet API
Servlet API consists of two important packages that encapsulates all the important classes
and interface, namely :

• javax.servlet
• javax.servlet.http

How a Servlet Application works


Web container is responsible for managing execution of servlets and JSP pages for Java
EE application.
When a request comes in for a servlet, the server hands the request to the Web
Container. Web Container is responsible for instantiating the servlet or creating a new
thread to handle the request. Its the job of Web Container to get the request and response
to the servlet. The container creates multiple threads to process multiple requests to a
single servlet.
Servlets don't have a main() method. Web Container manages the life cycle of a Servlet
instance.
1. User sends request for a servlet by clicking a link that has URL to a servlet.
2. The container finds the servlet using deployment descriptor and creates two
objects :

a. HttpServletRequest
b. HttpServletResponse

3. Then the container creates or allocates a thread for that request and calls the
Servlet's service()method and passes the request, response objects as
arguments.
4. The service() method, then decides which servlet method, doGet() or doPost() to
call, based on HTTP Request Method(Get, Post etc) sent by the client. Suppose the
client sent an HTTP GET request, so the service() will call
Servlet's doGet() method.
5. Then the Servlet uses response object to write the response back to the client.
6. After the service() method is completed the thread dies. And the request and
response objects are ready for garbage collection.

Servlet Life Cycle


Refer Notes given in class.

Content Type
Content Type is also known as MIME (Multipurpose internet Mail Extension)Type. It is
a HTTP header that provides the description about what are you sending to the browser.

MIME is an internet standard that is used for extending the limited capabilities of email by
allowing the insertion of sounds, images and text in a message.

The features provided by MIME to the email services are as given below:

o It supports the non-ASCII characters

o It supports the multiple attachments in a single message

o It supports the attachment which contains executable audio, images and video files
etc.

o It supports the unlimited message length.

List of Content Types


There are many content types. The commonly used content types are given below:

o text/html

o text/plain

o application/msword

o application/vnd.ms-excel

o application/jar

o application/pdf

o application/octet-stream

o application/x-zip

o images/jpeg

o images/png

o images/gif
o audio/mp3

o video/mp4

o video/quicktime etc.

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.

Let's see what are the interfaces of javax.servlet package.

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

Classes in javax.servlet package


There are many classes in javax.servlet package. They are as follows:

1. GenericServlet

2. ServletInputStream
3. ServletOutputStream

4. ServletException

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

Classes in javax.servlet.http package


There are many classes in javax.servlet.http package. They are as follows:

1. HttpServlet

2. Cookie

3. HttpSessionEvent

RequestDispatcher in Servlet
1. RequestDispatcher Interface

2. Methods of RequestDispatcher interface

1. forward method

2. include method

3. How to get the object of RequestDispatcher

4. Example of RequestDispatcher interface

The RequestDispatcher interface provides the facility of dispatching the request to another
resource it may be html, servlet or jsp. This interface can also be used to include the
content of another resource also. It is one of the way of servlet collaboration.

There are two methods defined in the RequestDispatcher interface.

Methods of RequestDispatcher interface


The RequestDispatcher interface provides two methods. They are:
public void forward(ServletRequest request,ServletResponse
response)throws ServletException,java.io.IOException:Forwards a request
from a servlet to another resource (servlet, JSP file, or HTML file) on the server.

public void include(ServletRequest request,ServletResponse


response)throws ServletException,java.io.IOException:Includes the content of
a resource (servlet, JSP page, or HTML file) in the response.

As you see in the above figure, response of second servlet is sent to the client. Response of
the first servlet is not displayed to the user.

How to get the object of RequestDispatcher


The getRequestDispatcher() method of ServletRequest interface returns the object of
RequestDispatcher. Syntax:

Syntax of getRequestDispatcher method


public RequestDispatcher getRequestDispatcher(String resource);

Example of using getRequestDispatcher method


RequestDispatcher rd=request.getRequestDispatcher("servlet2");
//servlet2 is the url-pattern of the second servlet
rd.forward(request, response);//method may be include or forward

You might also like