204489577-servlets-in-java-ppt
204489577-servlets-in-java-ppt
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
Web Server
Application
Not a problem.
I can handle
dynamic
requests.
Helper
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
The CONTAINER
What is the role of Web Container ?
S2
• Communication Support
S1
• Lifecycle Management
JSP1
• Multi-threading support
S3
• Security
• JSP Support S4
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 setAttribute(String, Object) parameters.
set
ID# 42
“ale” “dark”
#42
Client A 1 HttpSession
request, “ale”, ID# 42
2
Container
Session Tracking – Cookies
HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=0ABS
Content-Type: text/html
Server: Apache-Coyote/1.1
Client A <html>
…
</html>
Container
OK, here’s the HTTP Response
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 = out.println(d.getName());
• <%= i %>
• <%= 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>