Chapter 4 - Servlets
Chapter 4 - Servlets
HTTP is a protocol that clients and servers use on the web to communicate.
The client sends an HTTP request and the server answers with an
HTML page to the client, using HTTP.
There are many interfaces and classes in the servlet API such as
Servlet,GenericServlet,HttpServlet,ServletRequest,ServletResponse
etc.
A servlet is a compiled Java class that run server-side under the control of the Web
server.
Servlet is an API that provides many interfaces and classes including documentations.
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.
When the Web Server receives a request that is for a servlet, the request is
passed to the servlet container
oThe container makes sure the servlet is loaded and calls it
oThe servlet call has two parameter objects, one with the request and one for
the response
oWhen the servlet is finished, the container reinitializes itself and returns
control to the Web server
HTTP Web
Server
Servlet
Dynamic Server
02/06/25
Tomcat = Web Server + Servlet
COMPILED BY: ZIYAD A. 11
Server
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.
It is platform dependent
02/06/25 COMPILED BY: ZIYAD A. 13
Advantages 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:
better performance: because it creates a thread for each request not
process.
Portability: because it uses java language.
Robust: Servlets are managed by JVM so no need to worry about
memory leak, garbage collection etc.
02/06/25 COMPILED BY: ZIYAD A. 14
Secure: because it uses java language..
Advantages of Servlet
doHead() etc.
getWriter method returns a PrintWriter for sending text data to
client
getOutputStream method returns a ServletOutputStream for
sending binary data to client.
Need to close the Writer or ServletOutputStream after you send
the response.
HTTP Header Data
Must set HTTP header data before you access the Writer or
OutputStream.
HttpServletResponse interface provides methods to manipulate
the header data.
For example, the setContentType method sets the content26 type.
(This header is often the only one manuallyCOMPILED
02/06/25 set.)BY: ZIYAD A.
1. Install Apache Tomcat in your computer
CATALINA_HOME\bin\startup.bat
CATALINA_HOME\bin\shutdown.bat
02/06/25 COMPILED BY: ZIYAD A. 27
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.
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML
file) on the server.
As you see in the above figure, response of second servlet is sent to the client. Response
02/06/25 COMPILED BY: ZIYAD A.
29
As you can see in the above figure, response of second servlet is
included in the response of the first servlet that is being sent to the
client.
02/06/25 COMPILED BY: ZIYAD A.
30
How to get the object of RequestDispatcher
Syntax:
public RequestDispatcher getRequestDispatche
r(String resource);
Example:
RequestDispatcher rd=request.getRequestDispatche
r("servlet2");
//servlet2 is the url-pattern of the second servlet
rd.forward(request, response);//
02/06/25 COMPILED BY: ZIYAD A.
method may be include or forward
SendRedirect in servlet
It works at client side because it uses the url bar of the browser to
make another request. So, it can work inside and outside the server
The forward() method works at server side. The sendRedirect() method works at client side.
It can work within the server only. It can be used within and outside the server.
Example:
request.getRequestDispacher("servlet2").forward Example: response.sendRedirect("servlet2");
(request,response);
02/06/25 COMPILED BY: ZIYAD A.
33
Syntax of sendRedirect() method
Example
response.sendRedirect("http://www.google.com");
1.Cookies
3.URL Rewriting
4.HttpSession
Types of Cookie
1. Non-persistent cookie
2. Persistent cookie
02/06/25 COMPILED BY: ZIYAD A. 39
1. Cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes
the browser.
Persistent cookie
It is valid for multiple session . It is not removed each time when user
closes the browser. It is removed only if user logout or signout.
Disadvantage of Cookies
Constructor Description
In such case, we store the information in the hidden field and get it from
another servlet. This approach is better if we have to submit form in all the
pages and we don't want to depend on the browser.
Here, uname is the hidden field name and admin is the hidden field value.
In such case, container creates a session id for each user. The container
uses this id to identify the particular user.
1.bind objects
public long getCreationTime():Returns the time when this session was created,
measured in milliseconds since midnight January 1, 1970 GMT.
public long getLastAccessedTime():Returns the last time the client sent a request
associated with this session, as the number of milliseconds since midnight January 1,
1970 GMT.
public void invalidate():Invalidates this session then unbinds any objects bound to
it.