Servlets & JSPS: - Sharad Ballepu
Servlets & JSPS: - Sharad Ballepu
Servlets & JSPS: - Sharad Ballepu
Agenda
Introduction Servlet Architecture Servlet lifecycle Request and Response Being a Web Container Session management Overview of JSP JSP Elements Q&A
Request-response model.
HTTP Request
request Server
<html> <head> <html> <body> <head> <body>
Client response
HTTP HTML
HTTP Request
Key elements of a request stream: HTTP method (action to be performed).
HTTP Response
Key elements of a response stream: A status code (for whether the request was successful).
Helper Application
Web Server machine
Web Server
Web Container
Servlet
Client
Http request
Web Container
request
Client
response
Service() doGet()
S2
JSP1
S4
Servlet Architecture Deployment Descriptor How does the Container know which Servlet the client has requested for? A Servlet can have 3 names Client known URL name Deployer known secret internal name Actual file name
Servlet Lifecycle
The Servlet lifecycle is simple, there is only one main state Initialized.
Does not exist
Initialized Service()
Servlet
Abstract class
GenericServlet
Abstract class
HttpServlet
Concrete class
Your Servlet
When is it called
Do you override it
init()
The container calls the init() before the servlet can service any client requests. When a new request for that servlet comes in.
Possibly
service()
doGet() or doPost()
The service() To handle the method invokes it business logic. based on the HTTP method from the request.
Always
Servlet Lifecycle Thread handling The Container runs multiple threads to process multiple requests to a single servlet.
Container
Client A Servlet
Client B
Thread A
Thread B
response
request
request
response
POST (doPost())
Along with request line and header it also contains HTTP body.
HTTP Request
Parameter passing
Size
The form elements are passed to the server by appending at the end of the URL.
The parameter data is limited (the limit depends on the container) GET is Idempotent Generally used to fetch some information from the host.
The form elements are passed in the body of the HTTP request.
Can send huge amount of data to the server. POST is not idempotent Generally used to process the sent data.
Idempotency Usage
Request
No
No
Yes Yes
Send resource
Servlet 1
Servlet 2
Servlet 3
JSP 1
Servlet Config
Servlet Config
Servlet Config
Servlet Config
What are init parameters? Difference between Servlet Context and Config Init parameters
Context Init Parameters Scope Scope is Web Container Servlet Init Parameters Specific to Servlet or JSP
Servlet code
Deployment Descriptor
getServletContext()
getServletConfig()
Within the <web-app> element Within the <servlet> element but not within a specific for each specific servlet <servlet> element
HttpSession
ale #42
ID# 42 dark
Client A
HttpSession
Client A
HTTP Response
Container
URL
HTTP/1.1 200 OK
;jsessionid=1234567
Container
Client A
Content-Type: text/html Server: Apache-Coyote/1.1 <html> <body> < a href = http:// www.sharmanj.com/Metavante;jsessionid=0AAB> click me </a> </html>
HTTP Response
GET /Metavante;jsessionid=0AAB
HTTP / 1.1 Host: www.sharmanj.com Accept: text/html
Client A
HTTP Request
Container
Servlets : HTML within Java business logic 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>
Compiles to
0010 0001 1100 1001 0001 0011
MyJsp_jsp.java MyJsp_jsp.class
JSP Elements
Scriptlets
<% int I = 10; %> <% Dog d = new Dog(); %>
= out.println(i); Expressions
<%= i %> <%= d.getName() %>
= out.println(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?
<%@ page import=foo.* %> <html> <body> <% int i = 10; %> <%! int count = 0; %> Hello! Welcome <%! Public void display() { out.println(Hello); } %> 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); } }
</body> </html>
Q&A