0% found this document useful (0 votes)
45 views20 pages

Chapter 5 - SERVLETS

The document provides a comprehensive overview of servlets, which are server-side components used to create dynamic web applications. It covers key topics such as the servlet life cycle, advantages, configuration, HTTP request handling, session management, and methods for page redirection. The information is structured as a bucket list of essential concepts related to servlet technology.

Uploaded by

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

Chapter 5 - SERVLETS

The document provides a comprehensive overview of servlets, which are server-side components used to create dynamic web applications. It covers key topics such as the servlet life cycle, advantages, configuration, HTTP request handling, session management, and methods for page redirection. The information is structured as a bucket list of essential concepts related to servlet technology.

Uploaded by

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

SERVLETS

BUCKET LIST
• What is web application?
• Introduction to servlet
• Advantages of servlets
• Servlet life cycle
• Servlet Configuration
• HTTP Request handlers
• Servlet config
• Servlet context
• Page redirecting – RequestDispatcher and
sendRedirect
• Sessions
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.
Introduction to 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.
What is servlet?

• 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.
Advantages of servlets
• 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 we don't need to


worry about memory leak, garbage collection etc.

• Secure: because it uses java language..


Servlet life cycle
• Create (Servlet Instantiation)
– loading the servlet class and creating a new instance
• Initialize (Servlet Initialization)
– Initialize the servlet using the init() method
• Service (Servlet processing)
– Handling 0 or more client requests using the service() method
• Destroy (Servlet Death):
Destroying the servlet using the destroy() method
Note: Whenever the HTTP calls for a servlet and it checks
the whether the class loaded or not.
If not loaded, then it does Load, Create, Init, Service
otherwise it perform only Service
Life Cycle
Servlet Configuration
• Configuring through web.xml (deployment descriptor)

• @WebServlet annotation
HTTP Request handlers
Each HTTP Request type has a separate handler
function.
 GET
 doGet(HttpServletRequest, HttpServletResponse)
 POST
 doPost(HttpServletRequest,HttpServletResponse)
 PUT
 doPut (HttpServletRequest, HttpServletResponse)
 DELETE
 doDelete(HttpServletRequest, HttpServletResponse)
 TRACE
 doTrace (HttpServletRequest, HttpServletResponse)
 OPTIONS
 doOptions (HttpServletRequest, HttpServletResponse)
Servlet config

• What is ServletConfig?
• A ServletConfig is an object created by the web container for
each servlet. This object can be used to get configuration
information from web.xml file.
• It’s like a local parameter associated with particular
servlet. It’s a name and value pair defined inside the servlet
section in the web.xml file. So it has Servlet wide scope.
• Syntax:
<servlet>
<!-- Servlet Config -->
<init-param>
<param-name>param_name</param-name>
<param-value>value</param-value>
</init-param>

</servlet>
What is the need of servlet config?
• Sometimes we may have a requirement that a value keeps
changing time to time and so we do not want to hard code
it into a servlet. If it is done, we will have to change
the value and again recompile the servlet. Typically
used for

– The name of the database in which to start up an


instance
– The names and locations of the database control
files
– The name of the tables in the database
Servlet context

• What is ServletContext?
– ServletContext is a configuration Object which is
created when the web application is started. It
contains different initialization parameter that can
be configured in web.xml.

• When it is needed?
– This will be very useful when you want to share the
data to all servlet and jsp files.
• Syntax:
<web-app>
<context-param>
<param-name>param_name</param-name>
<param-value>value</param-value>
</context-param>
</web-app>
Page redirecting
• RequestDispatcher
• sendRedirect
Send redirect
• The sendRedirect() request is transferred to
another resource of different domain or different
server for further processing.
• The sendRedirect() is slower because completely new
request is created and old request object is lost,
so one extra round trip is required.
• Example:
response.sendRedirect("pathToResource");
Request Dispatcher
• The RequestDispatcher of forward()/include() method
request is transferred to other resource within the same
server for further processing.

• The forward () method is faster than sendRedirect().

• Example:

RequestDispatcher rd =
request.getRequestDispatcher("pathToResource");
rd.forward(request, response);
Sessions
• What is Session
Session is a conversational state between client and
server and it can consists of multiple request and
response between client and server. Since HTTP and Web
Server both are stateless, the only way to maintain a
session is when some unique information about the
session (session id) is passed between server and client
in every request and response.
• There are several ways through which we can provide
unique identifier in request and response.
– User Authentication
– HTML Hidden Field
– URL Rewriting
– Cookies Session Management API
Creating session
• The HttpServletRequest interface provides two methods to
get the object of HttpSession

• public HttpSession getSession()


– Returns the current session associated with this request, or if the
request does not have a session, creates one.

• public HttpSession getSession(boolean create)


– Returns the current HttpSession associated with this request or, if
there is no current session and create is true, returns a new
session.
Session attribute
• Session attributes are bound to a session. Session attributes are
persistent between several requests from the same user.
• To maintain the state of the user information we can use set and
get attribute.
• There are 4 attribute specific methods are used to manage the
sessions. They are as follows:
 setAttribute
public void setAttribute(String name,Object object)

 getAttribute
public void getAttribute(String name)
 getInitParameterNames
public Enumeration getInitParameterNames()
 removeAttribute
public void removeAttribute(String name)
THANK YOU

You might also like