View
56
Download
2
Category
Tags:
Preview:
DESCRIPTION
JSP
Citation preview
Servlets & JSPs - Sharad Ballepu sharadballepu@sharmanj.com www.sharmanj.com
Agenda
Introduction Servlet ArchitectureServlet lifecycleRequest and ResponseBeing a Web ContainerSession managementOverview of JSPJSP ElementsQ & A
Servlets & JSPs
Request-response model. requestresponseHTTP
HTMLHTTP Request
ClientServerIntroduction request-response model
HTTP Request HTTP Response Key elements of a request stream:
HTTP method (action to be performed).
The page to access (a URL).
Form parameters.
Key elements of a response stream:
A status code (for whether the request was successful).
Content-type (text, picture, html, etc).
The content ( the actual content).
Introduction what is a request and response
Where does Servlet come into the picture?Web ServerApplicationHelper ApplicationWeb Server machineI can serve only static HTML pagesNot a problem. I can handle dynamic requests.The Helper Application is nothing but a SERVLETIntroduction What is a Servlet
What is a Web Container?GET...Web ServerServletWeb ContainerGET...GET...requestClientServlet Architecture -Web Container
How does the Container handle a request?Web ContainerServletThreadService()doGet()
. requestresponseresponseWeb ServerHttp requestClientServlet Architecture Web Container
What is the role of Web Container ?
Communication Support
Lifecycle Management
Multi-threading support
Security
JSP Support
The CONTAINERS1S3S4S2JSP1The container can contain multiple Servlets & JSPs within itServlet Architecture Web Container
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
LoginServ com.Login
LoginServ /Logon .. ..
Web.xml
Servlet Architecture Deployment Descriptor
The Servlet lifecycle is simple, there is only one main state Initialized.InitializedDoes not existconstructor()init()destroy()Service()Servlet Lifecycle
GenericServletHttpServletYour Servlet Servlet
InterfaceAbstract classAbstract classConcrete classIf not overridden, implements init() method from the Servlet interface,If not overridden, implements service()method.We implement the HTTP methods here.Servlet Lifecycle - Hierarchy
Servlet Lifecycle 3 big moments
When is it calledWhat its forDo you override itinit()The container calls the init() before the servlet can service any client requests.To initialize your servlet before handling any client requests.
Possiblyservice()When a new request for that servlet comes in.To determine which HTTP method should be called. No. Very unlikelydoGet() or doPost()The service() method invokes it based on the HTTP method from the request.To handle the business logic. Always
The Container runs multiple threads to process multiple requests to a single servlet.ServletThread AThread BContainerClient AClient BresponseresponserequestrequestServlet Lifecycle Thread handling
The HTTP request method determines whether doGet() or doPost() runs.Request and Response GET v/s POST
GET (doGet()) POST (doPost())HTTP Request The request contains only the request line and HTTP header.Along with request line and header it also contains HTTP body.Parameter passingThe form elements are passed to the server by appending at the end of the URL.The form elements are passed in the body of the HTTP request.Size The parameter data is limited (the limit depends on the container)Can send huge amount of data to the server.IdempotencyGET is IdempotentPOST is not idempotentUsageGenerally used to fetch some information from the host.Generally used to process the sent data.
RequestCan the Servlet Serve the request?Send resourceYesDoes the Servlet knowWho can serve?Error pageSend RedirectRequest DispatcherNoYesNoRequest and Response The response
Servlet 2Servlet 1Servlet 3JSP 1Servlet ContextServlet ConfigServlet ConfigServlet ConfigServlet ConfigBeing a Web Container Servlet Config and Context
What are init parameters?Difference between Servlet Context and Config Init parameters
Being a Web Container init parameters
Context Init ParametersServlet Init Parameters ScopeScope is Web ContainerSpecific to Servlet or JSPServlet codegetServletContext()getServletConfig()Deployment DescriptorWithin the element but not within a specific elementWithin the element for each specific servlet
What exactly, is an attribute?Difference between Attributes and parameters
Being a Web Container - Attributes
AttributesParametersTypesContextRequestSessionContextRequestServlet InitMethod to setsetAttribute(String, Object)We cannot set Init parameters. Return typeObjectStringMethod to getgetAttribute(String)getInitParameter (String)
How sessions work?ID# 42dark1432ID# 42darkale#4212request, darknewsetAttribute(dark)response, ID# 42request, ale, ID# 42HttpSessionHttpSessionContainerClient AClient AContainerSession Management Session Tracking
HTTP/1.1 200 OKSet-Cookie: JSESSIONID=0ABSContent-Type: text/htmlServer: Apache-Coyote/1.1
POST / login.do HTTP/1.1
Cookie: JSESSIONID=0ABSAccept: text/html
Heres your cookie with session ID insideOK, heres the cookie with my requestHTTP ResponseHTTP RequestHttpSession session = request.getSession();Client AClient AContainerContainerSession Tracking Cookies
URL ;jsessionid=1234567HTTP/1.1 200 OKContent-Type: text/htmlServer: Apache-Coyote/1.1
< a href = http:// www.sharmanj.com/Metavante;jsessionid=0AAB> click me
GET /Metavante;jsessionid=0AAB
HTTP / 1.1Host: www.sharmanj.comAccept: text/html
ContainerContainerClient AClient AHTTP ResponseHTTP RequestSession Tracking URL Rewriting
public void doGet(request, response){ PrintWriter out = response.getWriter(); String name = request.getParameter(name);out.println();out.println("Hello + name);out.println(); }
Hello
Servlets : HTML within Java business logicJSPs : Java within HTML Presentation logicJSP Overview - Servlets v/s JSPs
In the end, a JSP is just a Servlet.
0010 00011100 10010001 0011Import javax.servlet.HttpServlet.*JSPServletMyJsp.jspMyJsp_jsp.javaMyJsp_jsp.classMyJsp_jsp ServletwritesIs translated toCompiles toIs loaded andInitialized asJSP Overview - What is a JSP
Scriptlets
Expressions
Declarations
DirectivesPages - include - taglib -
= out.println(i);= out.println(d.getName());JSP Elements
Where does the JSP code land in the Servlet?
Hello! Welcome
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(\r); out.println(Hello! Welcome); } } JSP Elements JSP to Servlet
Q & A
Recommended