Servlets & JSPS: - Sharad Ballepu
Servlets & JSPS: - Sharad Ballepu
- Sharad Ballepu
[email protected]
www.sharmanj.com
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
“The Helper Application is nothing but a SERVLET”
Servlet Architecture -Web Container
request
GET. GET. GET.
….. ….. …..
request Servlet
Http request Web
Container
response
Thread
Web
Server
<Html>
response <Body>
……. Service()
Client </Body>
</Html>
doGet()
Servlet Architecture – Web Container
The CONTAINER
What is the role of Web Container ?
S2
• Communication Support
S1
• Lifecycle Management
JSP1
• Multi-threading support
S3
• Security
• JSP Support S4
• How does the Container know which Servlet the client has
requested for? <web-app>
………
<servlet>
A Servlet can have 3 names
<servlet-name>LoginServ</servlet-name>
<servlet-class>com.Login</servlet-class>
Client known URL name </servlet>
<servlet-mapping>
Deployer known secret <servlet-name>LoginServ</servlet-name>
internal name <url-pattern>/Logon</url-pattern>
</servlet-mapping>
………..
Actual file name ………..
</web-app>
Web.xml
Servlet Lifecycle
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