4. Client : someone who request for some resource.
Server : servers the request requested by client
WebServer / HTTPServer : machine that takes HTTP
request from client over a web and sends the
response back in HTML page back to client over
a web through HTTP.
request-response model
5. HTTP Request HTTP
Response
Key elements of a “request”
stream:
HTTP method (action to be
performed) like GET,POST, PUT.
The page to access (a URL).
Form parameters.
Key elements of a “response”
stream:
A status code (for whether
the request was successful).
Content-type (text, picture,
html, etc…).
The content ( the actual
content).
Introduction – what is a request and response
6. Where does Servlet come into the picture?
Web Server
Application
Helper
Application
Web Server
machine
I can serve only
static HTML pages
Not a problem. I
can handle
dynamic
requests.
“The Helper Application is nothing but a SERVLET”
Introduction – What is a Servlet
7. What is a Web Container?
GET.
…..
Web
Server
ServletWeb
Container
GET.
…..
GET.
…..
request
Client
Servlet Architecture -Web Container
8. How does the Container handle a request?
Web
Container
Servlet
Thread
Service()
doGet()
<Html>
<Body>
…….
</Body>
</Html>
request
response
response
Web
Server
Http request
Client
Servlet Architecture – Web Container
9. Helps web server to communicate to servlet
Servlet does not have any main method, it is only
having call back method. So when request
comes to HTTP Server, it will first go to web
container. Web container know which call back
method can server HTTP request.
Servlet and JSP resides inside a we container.
Servlet is just a java program which is present on
server side.
10. What is the role of Web Container ?
Communication Support- between servlet & web
server
Lifecycle Management- servlet is just having
callback method so container is only
responsible for creation and destroy threads
Multi-threading support- for each request it
creates new request.
Security- as client cannot directly communicate
with servlet.
JSP Support
The CONTAINER
S1
S3
S4
S2
JSP1
The container can contain multiple Servlets & JSPs within it
Servlet Architecture – Web Container
11. How does the Container know which Servlet the client has
requested for?
A Servlet can have 3 names
Client known URL name
- <url-pattern>
Deployer known secret
internal name –
<servlet-name>
Actual file name-
<servlet-class>
<web-app>
………
<servlet>
2) <servlet-name>LoginServ</servlet-name>
3) <servlet-class>com.Login</servlet-class>
</servlet>
<servlet-mapping>
2) <servlet-name>LoginServ</servlet-name>
<url-pattern>/Logon</url-pattern> 1)
</servlet-mapping>
………..
………..
</web-app>
Web.xml
Servlet Architecture – Deployment Descriptor
12. The Servlet lifecycle is simple, there is only one main state –
“Initialized”.
Initialized
Does not exist
constructor()
init()
call only once in lifetime
destroy()
Service()
Servlet Lifecycle
14. When is it called What it’s for Do you override
it
init() The container
calls the init()
before the servlet
can service any
client requests.
To initialize your
servlet before
handling any
client requests.
Possibly
service() When a new
request for that
servlet comes in.
To determine
which HTTP
method should be
called.
No. Very unlikely
doGet() or
doPost()
The service()
method invokes it
based on the HTTP
method from the
request.
To handle the
business logic.
Always
Servlet Lifecycle – 3 big moments
15. The Container runs multiple threads to process multiple
requests to a single servlet.
Servlet
Thread
A
Thread
B
Container
Client A Client B
response
responserequest request
Servlet Lifecycle – Thread handling
16. The HTTP request method determines whether doGet() or
doPost() runs.
GET (doGet()) POST (doPost())
HTTP Request
The request contains only the
request line and HTTP header.
Along with request line
and header it also contains
HTTP body.
Parameter
passing
The form elements are passed
to the server by appending at
the end of the URL.
The form elements are
passed in the body of the
HTTP request.
Size The parameter data is limited
(the limit depends on the
container)
Can send huge amount of
data to the server.
Idempotency GET is Idempotent- browsing
something on chrome
POST is not idempotent-
filling app form, secured
data, login
Usage Generally used to fetch some
information from the host.
Generally used to process
the sent data.
Request and Response – GET v/s POST
17. Request
Can the Servlet
Serve the request?
Send resource
Yes
Does the Servlet know
Who can serve?
Error page
Send Redirect
Request Dispatcher
No
Yes
No
Request and Response – The response
18. Key Notes
Constructor is not having access to servlet
context and servlet config obj. That’s why in
Servlet init() method is there which is having
access to servlet context and config.
Send Redirect- Servlet sends response back to
browser that some other servlet can serve this
request. So browser sends again the request that
servlet. Client will know about this. This is 2 step
process.
Request Dispatcher- Servlet sends request to
other servlet instead of sending back to web
browser. Client will not know about this. This can
use only when other servlet resides over same
web application.
19. Servlet 2Servlet 1 Servlet 3 JSP 1
Servlet Context- only one in whole web application
Servlet Config Servlet ConfigServlet Config Servlet Config
Being a Web Container – Servlet Config and Context
20. What are init parameters?
Difference between Servlet Context and Config Init parameters
Context Init Parameters Config Init Parameters
Scope Scope is Web Container Specific to Servlet or JSP
Servlet code getServletContext() getServletConfig()
Deployment
Descriptor
Within the <web-app> element
but not within a specific
<servlet> element- for
initializing datasource by
providing datasource name &
uri
Within the <servlet> element
for each specific servlet
Being a Web Container – init parameters
21. What exactly, is an attribute? – both attribute and parameter are used
to store and retrieve data
Difference between Attributes and parameters
Attributes Parameters
Types
Context
Request
Session
Context
Request
Servlet Init
Method to set
setAttribute(String, Object)
so if object value changes we should
use attibutes
We cannot set Init
parameters. Initialize
Only once in web.xml
Return type Object String
Method to get getAttribute(String)
getInitParameter
(String)
Being a Web Container - Attributes
23. Http is a stateless protocol so it does not have the
capability to store the state of the request. When request
comes it cannot understand that this request is coming
from different user
Cookies- It is created by server which is sent back to client
and store at client machine.It contains info about client
and whenever client request to server cookie will go with
the request to server.
URL Rewriting- Client can disable cookie. Even if server
sends a cookie it will not store inside client machine. So
appending jsessionId to url
URL+ jsessionId=12355
Which is best cookie or url rewriting ?
For the first time server employees both method.If client
disable cookie on next request , cookie will be sent to
server else URL rewriting is done if required
24. HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=0ABS
Content-Type: text/html
Server: Apache-Coyote/1.1
<html>
…
</html>
POST / login.do HTTP/1.1
Cookie: JSESSIONID=0ABS
Accept: text/html……
Here’s your cookie
with session ID
inside…
OK, here’s the
cookie with
my request
HTTP Response
HTTP Request
HttpSession session = request.getSession();
Client A
Client A
Container
Container
Session Tracking – Cookies
25. URL ;jsessionid=1234567
HTTP/1.1 200 OK
Content-Type: text/html
Server: Apache-Coyote/1.1
<html>
<body>
< a href =“ http:// www.sharmanj.com/Metavante;jsessionid=0AAB”>
click me </a>
</html>
GET /Metavante;jsessionid=0AAB
HTTP / 1.1
Host: www.sharmanj.com
Accept: text/html
Container
Container
Client A
Client A
HTTP Response
HTTP Request
Session Tracking – URL Rewriting
26. public void doGet(request, response)
{
PrintWriter out = response.getWriter();
String name =
request.getParameter(name);
out.println(“<html><body>”);
out.println("Hello” + name);
out.println(“</body></html>”);
}
<html>
<body>
<% String name =
request.getParameter(name); %>
Hello <%= name %>
</body>
</html>
Servlets : HTML within Java
business logic
JSPs : Java within HTML
Presentation logic
JSP Overview - Servlets v/s JSPs
27. In the end, a JSP is just a Servlet.Web container is
reposible for all these conversions
0010 0001
1100 1001
0001 0011
Import javax.
servlet.
HttpServlet.*JSP Servlet
MyJsp.jsp MyJsp_jsp.java MyJsp_jsp.class MyJsp_jsp
Servlet
writes
Is translated to Compiles to Is loaded and
Initialized as
JSP Overview - What is a JSP
28. Scriptlets- whatever java code you want to write you can put in <% %>.It will treat that as
java code
<% int I = 10; %>
<% Dog d = new Dog(); %>
Expressions- whatever you wrtite in <%= %>
will go in out.println() statement and prints
on UI
<%= i %>
<%= d.getName() %>
Declarations- whatever you want to initialize only once you can put in <%! %>
<%! int i=10; %>
<%! void display() { System.out.println(“Hello”); } %
Directives- when container converts the jsp to servlet it looks for directives which start
with <%@ %>.Then is will process jsp accordingly.
Pages - <%@ page import=“foo.*, bar.*” %>
include - <%@ include file=“/foo/myJsp.jsp” %>
taglib - <%@ taglib uri=“Tags” prefix=“cool” %>
= out.println(i);
= out.println(d.getName());
JSP Elements
29. Where does the JSP code land in the Servlet?
<%@ page import=“foo.*” %>
<html>
<body>
<% int i = 10; %>
<%! int count = 0; %>
Hello! Welcome
<%! Public void display()
{
out.println(“Hello”);
} %>
</body>
</html>
import javax.servlet.HttpServlet.*
import foo.*;
public class MyJsp_jsp extends
HttpServlet
{
int count = 0;
public void display()
{
out.println(“Hello”);
}
public void _jspService(req, res)
{
int i = 0;
out.println(“<html>r<body>”);
out.println(“Hello! Welcome”);
}
}
JSP Elements – JSP to Servlet