Servlet
Servlet
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 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
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.
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.
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.
3. Robust: Servlets are managed by JVM so we don't need to worry about memory
leak, garbage collection etc.
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
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.
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 attachment which contains executable audio, images and video files
etc.
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
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.
1. Servlet
2. ServletRequest
3. ServletResponse
4. RequestDispatcher
5. ServletConfig
6. ServletContext
1. GenericServlet
2. ServletInputStream
3. ServletOutputStream
4. ServletException
1. HttpServletRequest
2. HttpServletResponse
3. HttpSession
1. HttpServlet
2. Cookie
3. HttpSessionEvent
RequestDispatcher in Servlet
1. RequestDispatcher Interface
1. forward method
2. include method
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.
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.