0% found this document useful (0 votes)
74 views

204489577-servlets-in-java-ppt

a

Uploaded by

findinrafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

204489577-servlets-in-java-ppt

a

Uploaded by

findinrafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Servlets & JSPs

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

HTTP Request HTTP


Response
Key elements of a “request” Key elements of a “response”
stream: stream:

 HTTP method (action to be  A status code (for whether


performed). the request was successful).

 The page to access (a URL).  Content-type (text, picture,


html, etc…).
 Form parameters.
 The content ( the actual
content).
Introduction – What is a Servlet

Where does Servlet come into the picture?


I can serve only
static HTML
pages

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

• What is a Web Container?

request
GET.
GET. GET.
…..
….. …..

Web Web Servlet


Server Container
Client
Servlet Architecture – Web Container

• How does the Container handle a request?

request Servlet
Http request Web
Container
response
Thread
Web
Server

response <Html> Service()


Client <Body>
…….
</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

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?

A Servlet can have 3 names <web-app>


………
 Client known URL name <servlet>
<servlet-name>LoginServ</servlet-name>
<servlet-class>com.Login</servlet-class>
 Deployer known secret </servlet>
internal name
<servlet-mapping>
<servlet-name>LoginServ</servlet-name>
 Actual file name <url-pattern>/Logon</url-pattern>
</servlet-mapping>
………..
………..
</web-app>
Web.xml
Servlet Lifecycle

• The Servlet lifecycle is simple, there is only one main


state – “Initialized”.

Does not exist

constructor()
destroy()
init()

Initialized

Service()
Servlet Lifecycle - Hierarchy

Interface
Servlet

Abstract class GenericServlet If not overridden, implements init()


method from the ‘Servlet’ interface,

Abstract class HttpServlet If not overridden, implements service()


method.

Concrete class Your Servlet We implement the HTTP methods


here.
Servlet Lifecycle – 3 big moments

When is it What it’s for Do you


called override it
init() The container To initialize
calls the init() your servlet
before the before handling
Possibly
servlet can any client
service any requests.
client requests.
service() When a new To determine
request for that which HTTP No. Very
servlet comes method should unlikely
in. be called.
doGet() or The service() To handle the
doPost() method invokes business logic.
it based on the
Always
HTTP method
from the
request.
Servlet Lifecycle – Thread handling

• The Container runs multiple threads to process


multiple requests to a single servlet.
Container

Client A Client B

Servlet

Thread Thread
A B

response request request response


Request and Response – GET v/s POST

• The HTTP request method determines whether


doGet() or doPost() runs.
GET (doGet()) POST
(doPost())
The request contains only Along with request line
HTTP the request line and HTTP and header it also
Request header. contains HTTP body.
Parameter The form elements are The form elements are
passing passed to the server by passed in the body of
appending at the end of the the HTTP request.
URL.
Size The parameter data is Can send huge amount
limited (the limit depends of data to the server.
on the container)
Idempotenc GET is Idempotent POST is not idempotent
y
Usage Generally used to fetch Generally used to
Request and Response – The response

Request

Can the Servlet No Does the Servlet know


Serve the request? Who can serve?
No

Yes

Yes

Send Redirect
Send resource
Request Dispatcher Error page
Being a Web Container – Servlet Config and Context

Servlet Context

Servlet 1 Servlet 2 Servlet 3 JSP 1

Servlet Config Servlet Config Servlet Config Servlet Config


Being a Web Container – init parameters

• What are init parameters?


• Difference between Servlet Context and Config Init
parameters

Context Init Parameters Servlet Init Parameters

Scope Scope is Web Container Specific to Servlet or JSP

Servlet getServletContext() getServletConfig()


code
Deployme Within the <web-app> Within the <servlet>
nt element but not within a element for each specific
Descriptor specific <servlet> element servlet
Being a Web Container - Attributes

• What exactly, is an attribute?


• Difference between Attributes and parameters

Attributes Parameters
Context Context
Types Request Request
Session Servlet Init
We cannot set Init
Method to setAttribute(String, Object) parameters.
set

Return type Object String


getInitParameter
Method to getAttribute(String) (String)
get
Session Management – Session Tracking

• How sessions work? new


1 2
request, “dark”
ID# 42
“dark”
HttpSession
Client A

response, ID# 42 setAttribute(“dark”)


4 3
Container

ID# 42
“ale” “dark”
#42

Client A 1 HttpSession
request, “ale”, ID# 42
2
Container
Session Tracking – Cookies

HttpSession session = request.getSession(); Here’s your cookie


with session ID
inside…

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

POST / login.do HTTP/1.1

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

Client A HTTP Request Container


JSP Overview - Servlets v/s JSPs

JSPs : Java within HTML


Servlets : HTML within Java
Presentation logic
business logic

public void doGet(request, response) <html>


{ <body>
PrintWriter out = response.getWriter(); <% String name =
String name = request.getParameter(name); %>
request.getParameter(name);
out.println(“<html><body>”); Hello <%= name %>
out.println("Hello” + name);
out.println(“</body></html>”); </body>
} </html>
JSP Overview - What is a JSP

• In the end, a JSP is just a Servlet.

Is translated to Compiles to Is loaded and


Initialized as
writes Import javax. 0010 0001
JSP
servlet. 1100 1001 Servlet
HttpServlet.* 0001 0011

MyJsp.jsp MyJsp_jsp.java MyJsp_jsp.class MyJsp_jsp


Servlet
JSP Elements

• 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

• Where does the JSP code land in the 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>

You might also like