Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
- Sharad Ballepu
Servlets & JSPs
Agenda
• Introduction
• Servlet Architecture
• Servlet lifecycle
• Request and Response
• Being a Web Container
• Session management
• Overview of JSP
• JSP Elements
• Q&A
Introduction – request-response model
• Request-response model.
HTTP
Request
request
Server
<html>
<head>
<html>
<body>
…
<head>
<body>
…
Client
response
HTTP
HTML
Introduction – what is a request and response
Not a
problem. I
can handle
dynamic
Helper requests.
Application
Web Server machine
request
GET.
GET. GET.
….. ….. …..
request Servlet
Http request Web
Container
response
Thread
Web
Server
The CONTAINER
What is the role of Web Container ?
S2
• Communication Support S1
• Lifecycle Management
JSP1
• Multi-threading support S3
• Security
S4
• JSP Support
The container can contain multiple Servlets & JSPs within it
Servlet Architecture – Deployment Descriptor
• How does the Container know which Servlet the client has
requested for?
constructor()
destroy()
init()
Initialized
Service()
Servlet Lifecycle - Hierarchy
Interface Servlet
Client A Client B
Servlet
Thread Thread
A B
Request
Yes
Yes
Send Redirect
Send resource
Request Dispatcher Error page
Being a Web Container – Servlet Config and Context
Servlet Context
Attributes Parameters
Context Context
Types Request Request
Session Servlet Init
We cannot set Init
Method to set setAttribute(String, Object) parameters.
getInitParameter
Method to get getAttribute(String) (String)
Session Management – Session Tracking
ID# 42
“ale” “dark”
#42
Client A 1 HttpSession
request, “ale”, ID# 42
2
Container
Session Tracking – Cookies
Container
OK, here’s HTTP Response
the cookie
with my
request
Cookie: JSESSIONID=0ABS
Accept: text/html……
Client A
HTTP Request Container
Session Tracking – URL Rewriting
URL ;jsessionid=1234567
Container
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>
Client A </html>
HTTP Response
GET /Metavante;jsessionid=0AAB
HTTP / 1.1
Host: www.sharmanj.com
Accept: text/html
• Scriptlets
• <% int I = 10; %>
• <% Dog d = new Dog(); %>
= out.println(i);
• Expressions
• <%= i %>
= out.println(d.getName());
• <%= d.getName() %>
• Declarations
• <%! int i=10; %>
• <%! void display() { System.out.println(“Hello”); } %>
• Directives
• Pages - <%@ page import=“foo.*, bar.*” %>
• include - <%@ include file=“/foo/myJsp.jsp” %>
• taglib - <%@ taglib uri=“Tags” prefix=“cool” %>
JSP Elements – JSP to Servlet
import javax.servlet.HttpServlet.*
<%@ page import=“foo.*” %>
import foo.*;
public class MyJsp_jsp extends
<html>
HttpServlet
<body>
{
int count = 0;
<% int i = 10; %>
public void display()
<%! int count = 0; %>
{
out.println(“Hello”);
Hello! Welcome
}
public void _jspService(req, res)
<%! Public void display()
{
{
int i = 0;
out.println(“Hello”);
out.println(“<html>\r<body>”);
} %>
out.println(“Hello! Welcome”);
}
</body>
}
</html>
Q&A