web tech unit 5
web tech unit 5
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.
Servlets Architecture:
The following diagram shows the position of Servlets in a Web Application.
1. Client
The client shown in the architecture above is the web browser and it primarily works as a
medium that sends out HTTP requests over to the web server and the web server generates a
response based on some processing in the servlet and the client further processes the response.
2. Web Server
Primary job of a web server is to process the requests and responses that a user sends over time
and maintain how a web user would be able to access the files that has been hosted over the
server. The server we are talking about here is a software which manages access to a centralized
resource or service in a network. There are precisely two types of webservers:
● Static web server
● Dynamic web server
3. Web Container
Web container is another typical component in servlet architecture which is responsible for
communicating with the servlets. Two prime tasks of a web container are:
● Managing the servlet lifecycle
● URL mapping
Web container sits at the server-side managing and handling all the requests that are coming in
either from the servlets or from some JSP pages or potentially any other file system.
The entire life cycle of a Servlet is managed by the Servlet container which uses the
javax.servlet.Servlet interface to understand the Servlet object and manage it. So, before
creating a Servlet object, let’s first understand the life cycle of the Servlet object which is
actually understanding how the Servlet container manages the Servlet object.
Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes through four stages,
● Loading a Servlet.
● Initializing the Servlet.
● Request handling.
● Destroying the Servlet.:
1. Loading a Servlet: The first stage of the Servlet lifecycle involves loading and
initializing the Servlet by the Servlet container. The Web container or Servlet
Container can load the Servlet at either of the following two stages :
● Initializing the context, on configuring the Servlet with a zero or positive
integer value.
● If the Servlet is not preceding stage, it may delay the loading process until
the Web container determines that this Servlet is needed to service a
request.
2. The Servlet container performs two operations in this stage :
● Loading : Loads the Servlet class.
● Instantiation : Creates an instance of the Servlet. To create a new
instance of the Servlet, the container uses the no-argument constructor.
3.
4. Initializing a Servlet: After the Servlet is instantiated successfully, the Servlet
container initializes the instantiated Servlet object. The container initializes the
Servlet object by invoking the Servlet.init(ServletConfig) method which accepts
ServletConfig object reference as parameter.
The Servlet container invokes the Servlet.init(ServletConfig) method only once,
immediately after the Servlet.init(ServletConfig) object is instantiated successfully.
This method is used to initialize the resources, such as JDBC datasource.
Now, if the Servlet fails to initialize, then it informs the Servlet container by throwing
the ServletException or UnavailableException.
5. Handling request: After initialization, the Servlet instance is ready to serve the client
requests. The Servlet container performs the following operations when the Servlet
instance is located to service a request :
● It creates the ServletRequest and ServletResponse objects. In this case, if
this is a HTTP request, then the Web container creates
HttpServletRequest and HttpServletResponse objects which are
subtypes of the ServletRequest and ServletResponse objects
respectively.
● After creating the request and response objects it invokes the
Servlet.service(ServletRequest, ServletResponse) method by passing the
request and response objects.
6. The service() method while processing the request may throw the ServletException
or UnavailableException or IOException.
7. Destroying a Servlet: When a Servlet container decides to destroy the Servlet, it
HTTP Requests
The request sent by the computer to a web server, contains all sorts of potentially interesting
information; it is known as HTTP requests.
The HTTP client sends the request to the server in the form of request message which includes
following information:
○ The Request-line
○ The analysis of source IP address, proxy and port
○ The analysis of destination IP address, protocol, port and host
○ The Requested URI (Uniform Resource Identifier)
○ The Request method and Content
○ The User-Agent header
○ The Connection control header
○ The Cache control header
The HTTP request method indicates the method to be performed on the resource identified by t
The Requested URI (Uniform Resource Identifier). This method is case-sensitive and should be
used in uppercase.
SendRedirect in servlet:
1. sendRedirect method
2. Syntax of sendRedirect() method
3. Example of RequestDispatcher interface
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.
Session Tracking is a way to maintain state (data) of a user. It is also known as session
management in servlet.
Http protocol is stateless so we need to maintain state using session tracking techniques. Each
time a user requests to the server, the server treats the request as the new request. So we need to
maintain the state of a user to recognize a particular user.
HTTP is stateless, which means each request is considered as the new request. It is shown in the
figure given below:
Why use Session Tracking?
To recognize the user It is used to recognize the particular user.
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
HTTP cookie:
HTTP cookies (also called web cookies, Internet cookies, browser cookies, or simply cookies)
are small blocks of data created by a web server while a user is browsing a website and placed on
the user's computer or other device by the user's web browser. Cookies are placed on the device
used to access a website, and more than one cookie may be placed on a user's device during a
session.
Cookies serve useful and sometimes essential functions on the web. They enable web servers to
store stateful information (such as items added in the shopping cart in an online store) on the
user's device or to track the user's browsing activity (including clicking particular buttons,
logging in, or recording which pages were visited in the past).[1] They can also be used to save
information that the user previously entered into form fields, such as names, addresses,
passwords, and payment card numbers for subsequent use.
HttpSession interface:
1. HttpSession interface
2. How to get the HttpSession object
3. Commonly used methods of HttpSession interface
4. Example of using HttpSession
In such case, container creates a session id for each user.The container uses this id to identify the
particular user.An object of HttpSession can be used to perform two tasks:
1. bind objects
2. view and manipulate information about a session, such as the session identifier, creation
time, and last accessed time.
1. public HttpSession getSession():Returns the current session associated with this request,
or if the request does not have a session, creates one.
2. 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.
Introduction to JSP:
In Java, JSP stands for Jakarta Server Pages( (JSP; formerly JavaServer Pages)). It is a
server-side technology that is used for creating web applications. It is used to create dynamic
web content. JSP consists of both HTML tags and JSP tags. In this, JSP tags are used to insert
JAVA code into HTML pages. It is an advanced version of Servlet Technology i.e. a web-based
technology that helps us to create dynamic and platform-independent web pages. In this, Java
code can be inserted in HTML/ XML pages or both. JSP is first converted into a servlet by the
JSP container before processing the client’s request. JSP has various features like JSP
Expressions, JSP tags, JSP Expression Language, etc.
JSP allows embedding Java code in HTML pages, making it easier to build interactive web
applications. To further enhance your knowledge of Java web technologies, consider enrolling in
the Java Backend course, which covers essential concepts for backend development.
How is JSP more Advantageous than Servlet?
● They are easy to maintain.
● No recompilation or redeployment is required.
● Less coding is required in JSP.
● JSP has access to the entire API of JAVA.
● JSP is an extended version of Servlet.
A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a
user interface for a Java web application. Web developers write JSPs as text files that combine
HTML or XHTML code, XML elements, and embedded JSP actions and commands.
Using JSP, you can collect input from users through Web Page forms, present records from a
database or another source, and create Web Pages dynamically.
Syntax :
import javax.servlet.http.HttpServletRequest;
response object:
This is the HttpServletResponse object associated with the response to the client. The response
object also defines the interfaces that deal with creating new HTTP headers. Through this object
the JSP programmer can add new cookies or date stamps, HTTP status codes, etc.
In JSP, java code can be written inside the jsp page using the scriptlet tag.
The scripting element provides the ability to insert java code inside the jsp. There are three types
of scripting elements:
○ scriptlet tag
○ expression tag
○ declaration tag
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
There are many JSP action tags or elements. Each JSP action tag is used to perform some
specific tasks.
The action tags are used to control the flow between pages and to use Java Bean. The Jsp action
tags are given below.
JSP Action Tags Description
JSP directives:
1. JSP directives
1. page directive
2. Attributes of page directive
The jsp directives are messages that tells the web container how to translate a JSP
page into the corresponding servlet.
● page directive
● include directive
● taglib directive