Introduction to Servlet & JSP
INTERNAL
28 Dec 2023
Web Application
A web application is a program that is stored on a remote server and sent
over the internet through a browser interface.
Document Name
-2- CONFIDENTIA L
URL
A URL (Universal Resource Locator) contains the following information:
The protocol used to a access the resource
The the location of the server (whether by IP address or domain name)
The port number on the server (optional)
The location of the resource in the directory structure of the server
A fragment identifier (optional)
Query parameter (optional)
URI : https://www.google.com/search?q=java
URL : https://www.google.com/
Document Name
-3- CONFIDENTIA L
Web Server
Web server is a program which processes the network requests of the users and
serves them with files that create web pages. This exchange takes place
using Hypertext Transfer Protocol (HTTP).
• Apache Tomcat
• IIS
• Lighttpd
• NGINX
• JBoss
• Oracle iPlanet Web Server
• LiteSpeed Web Server
Document Name
CONFIDENTIA L
How a Web application works
Document Name
-5- CONFIDENTIA L
GET v/s POST
GET POST
1) In case of Get request, only limited In case of post request, large amount of
amount of data can be sent because data is data can be sent because data is sent in body.
sent in header.
2) Get request is not secured because data Post request is secured because data is not
is exposed in URL bar. exposed in URL bar.
3) Get request can be bookmarked. Post request cannot be bookmarked.
4) Get request is idempotent . It means Post request is non-idempotent.
second request will be ignored until response
of first request is delivered
5) Get request is more efficient and used Post request is less efficient and used less than
more than Post. get.
Document Name
CONFIDENTIA L
Http Request
HttpRequest
An HTTP request header is a component of a network packet sent by a browser or client
to the server to request for a specific page or data on the Web server. Each time a user
website or webpage in a browser, an HTTP request header is generated by the browser
and is sent to the website / Web server. Typically, the information within the HTTP
request header is in form of plain text record of data/page request made by the user.
Some of the information within an HTTP request header includes:
⚫ Source IP address and port number
⚫ Requested URI (data or web page)
⚫ Host (Destination website or web server)
⚫ Type of data the browser will accept in return (text, html, xml etc)
⚫ User’s browser type (Mozilla, Chrome, IE) so that the Web server can send compatible
data
Document Name
-7- CONFIDENTIA L
Http Response
An HTTP response header is a component of a network packet that is sent by a Web
server to a Web browser or client machine in response to an HTTP request. It is used in
Web communications to deliver webpage and other Web-based data from the server to
the requesting end-user browsers.
The Web server responds back by creating an HTTP response header and attaching the
requested data with it. The information embedded with the HTTP response header
includes;
1. HTTP Version - HTTP 1.0
- HTTP 1.1
2. Status code - 200 (numeric code)
3. Status Description - OK ( textual description of Status code)
4. Server - Info about the Web Server and installed modules and OS
5. Set-Cookie - a further cookie , that is, a cookie to be included in future requests
6. Pragma - a response header
- directs not to store the response in cache
7. Expires - to indicates that the response content expiry date
- a date in the past means response should not be cached
Document Name
-8- CONFIDENTIA L
Http Response ...
Document Name
-9- CONFIDENTIA L
Web application structure
Document Name
CONFIDENTIA L
What is a Servlet
A servlet is a Java programming language class that is used to extend the
capabilities of servers that host applications accessed by means of a request-
response programming model. Although servlets can respond to any type of
request, they are commonly used to extend the applications hosted by web
servers.
Document Name
CONFIDENTIA L
Servlet Life Cycle
• doHead : overridden to handle the HEAD request the response contains the only header but does not
contain the message body.
• doPut : overridden to handle the PUT request. This method allows the client to store the information
on the server.
• doDelete : overridden to handle the DELETE request. This method allows a client to remove a
document or Web page from the server.
• doOptions() : overridden to handle the OPTIONS request. This method is used to determine which
HTTP methods the server supports and returns an appropriate header.
• doTrace() : overridden to handle the TRACE request. This method returns the headers sent with the
TRACE request to the client so that they can wbe used in debugging. (used to debug web server)
connections ) - 12 Document Name
CONFIDENTIA L
-
Forward vs sendRedirect
Servlet in JEE platform provides two methods forward() and sendRedirect() to
route an HTTP request to another Servlet for processing.
sendRedirect
• We can issue a redirect which will send a HTTP 302 response instructing
the browser to perform a new GET request to the specified URL.
• To do this, we use the sendRedirect method belonging to the
HttpServletResponse interface:
response.sendRedirect("https://www.google.com");
• This is useful when we want to send the user to a different domain or
server outside of our web application.
• The drawback is that we lose the original request along with its parameters
and attributes.
Document Name
CONFIDENTIA L
Forward vs sendRedirect
Forward
• We can also choose to forward the request to another resource in our web
application. This resource is typically another servlet or JSP page.
• To do this, we use the forward method belonging to the
RequestDispatcher interface.
RequestDispatcher rd =
request.getRequestDispatcher("products.jsp"); rd.forward(request,
response);
• This approach is faster than redirection because everything occurs server-
side and there is no extra network trips required.
Document Name
CONFIDENTIA L
JSP Life Cycle
Receive HTTP Server
• JSPs run in two phases
Request
– Translation Phase
JSP
– Execution Phase Container
Page Compiler Servlet
JSP Servlet No Parse JSP
• In translation phase JSP page is Current?
compiled into a servlet Yes
– called JSP Page Implementation JSP Servlet Generate JSP
Loaded? Servlet Source
class
Yes No
Compile JSP
Load Servlet
• In execution phase the compliled JSP Servlet
is processed
JSP Page
Generate Servlet
Response
Send
Response
- 15 Document Name
CONFIDENTIA L
-
JSP Tags
• Declaration Tag Declarations are used to define methods & instance variables
Do not produce any output that is sent to client
Embedded in <%! and %> delimiters
<%!
Date d=new Date();
%>
• Expression Tag : To evaluate an expression/print the same.
Embedded in <%= and %> delimiters
<%= d %>
• Scriptlet Tag : Used to embed java code in JSP pages.
Embedded in <% and %> delimiters
<%
int x = 5;
int y = 7;
int z = x + y;
%>
- 16 Document Name
CONFIDENTIA L
-
Request
The request.getParameter() always returns String value and the data come
from client. This is the statement we are using to get the value from the client
to server.
String name=request.getParameter(“uname”);
The request.getAttribute() to get an object added to the request scope on
the server side i.e. using request.setAttribute(). You can add any type of
object you like here, Strings, Custom objects, in fact any object. This can
be use to communicate between;
⚫ servlet to servlet
⚫ servlet to jsp.
Employee
emp=(Employee)request.getAttribute(“currentUser”);
- 17 Document Name
CONFIDENTIA L
-
SendRedirect & Forward
SendRedirect ()
This method is used to redirect client request to some other location for
further processing ,the new location is available on different server or
different context. The web container handle this and transfer the request
using browser ,and this request is visible in browser as a new request. Some
time this is also called as client side redirect.
void sendRedirect(String url)
Forward()
This method is used to pass the request to another resource for further
processing within the same server, another resource could be any servlet,
jsp page any kind of file. This process is taken care by web container when
we call forward method request is sent to another resource without the client
being informed, which resource will handle the request it has been mention
on requestDispatcher object.
RequestDispatcher rd =
request.getRequestDispatcher("pathToResource");
rd.forward(request, response);
- 18 Document Name
CONFIDENTIA L
-
Happy Learning !