3b202cc010ec6a555ccc70843cc0c263.ppt
- Количество слайдов: 26
Servlets & JSPs - Sharad Ballepu
Servlets & JSPs Agenda • • • Introduction Servlet Architecture Servlet lifecycle Request and Response Being a Web Container Session management Overview of 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” stream: Key elements of a “response” stream: Ø HTTP method (action to be performed). Ø A status code (for whether 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 Helper Application Not a problem. I can handle dynamic requests. Web Server machine “The Helper Application is nothing but a SERVLET”
Servlet Architecture -Web Container • What is a Web Container? request GET. …. . Web Server Client GET. …. . Web Container Servlet
Servlet Architecture – Web Container • How does the Container handle a request? Http request Web Container Servlet request response Thread Web Server Client response <Html> <Body> ……. </Body> </Html> Service() do. Get()
Servlet Architecture – Web Container The CONTAINER What is the role of Web Container ? • Communication Support S 2 S 1 • Lifecycle Management • Multi-threading support • Security JSP 1 S 3 S 4 • JSP Support 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 Ø Client known URL name Ø Deployer known secret internal name Ø Actual file name <web-app> ……… <servlet> <servlet-name>Login. Serv</servlet-name> <servlet-class>com. Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login. Serv</servlet-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() init() Initialized Service() destroy()
Servlet Lifecycle - Hierarchy Interface Abstract class Servlet Generic. Servlet Abstract class Http. Servlet Concrete class Your Servlet If not overridden, implements init() method from the ‘Servlet’ interface, If not overridden, implements service() method. We implement the HTTP methods here.
Servlet Lifecycle – 3 big moments When is it called What it’s for The container calls the init() before the servlet can service any client requests. To initialize your servlet before handling any client requests. service() When a new request for that servlet comes in. To determine which HTTP No. Very unlikely method should be called. do. Get() or do. Post() The service() To handle the method invokes it business logic. based on the HTTP method from the request. init() Do you override it Possibly Always
Servlet Lifecycle – Thread handling • The Container runs multiple threads to process multiple requests to a single servlet. Container Client B Client A Servlet Thread A response request Thread B request response
Request and Response – GET v/s POST • The HTTP request method determines whether do. Get() or do. Post() runs. GET (do. Get()) HTTP Request POST (do. Post()) The request contains only the Along with request line and HTTP header. and header it also contains HTTP body. Parameter passing The 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. Idempotency GET is Idempotent POST is not idempotent Usage Generally used to fetch some information from the host. Generally used to process the sent data.
Request and Response – The response Request Can the Servlet Serve the request? No Does the Servlet know Who can serve? No Yes Send resource Send Redirect Request Dispatcher Error page
Being a Web Container – Servlet Config and Context Servlet 1 Servlet Config Servlet 2 Servlet Config Servlet 3 Servlet Config JSP 1 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 is Web Container Specific to Servlet or JSP Servlet code get. Servlet. Context() get. Servlet. Config() Deployment Descriptor Within the <web-app> element Within the <servlet> element but not within a specific for each specific servlet <servlet> element Scope
Being a Web Container - Attributes • What exactly, is an attribute? • Difference between Attributes and parameters Attributes Types Parameters Context Request Session Context Request Servlet Init We cannot set Init parameters. Method to set. Attribute(String, Object) Return type Object String get. Attribute(String) get. Init. Parameter (String) Method to get
Session Management – Session Tracking new • How sessions work? 1 request, “dark” 2 ID# 42 “dark” Client A Http. Session response, ID# 42 set. Attribute(“dark”) 4 3 Container “ale” #42 Client A Http. Session 1 request, “ale”, ID# 42 Container ID# 42 “dark” 2
Session Tracking – Cookies Http. Session session = request. get. Session(); HTTP/1. 1 200 OK Set-Cookie: JSESSIONID=0 ABS Client A OK, here’s the cookie with my request Here’s your cookie with session ID inside… Content-Type: text/html Server: Apache-Coyote/1. 1 <html> … </html> HTTP Response Container POST / login. do HTTP/1. 1 Cookie: JSESSIONID=0 ABS Accept: text/html…… Client A HTTP Request Container
Session Tracking – URL Rewriting URL ; jsessionid=1234567 Container HTTP/1. 1 200 OK Client A Content-Type: text/html Server: Apache-Coyote/1. 1 <html> <body> < a href =“ http: // www. sharmanj. com/Metavante; jsessionid=0 AAB”> click me </a> </html> HTTP Response GET /Metavante; jsessionid=0 AAB HTTP / 1. 1 Host: www. sharmanj. com Accept: text/html Client A HTTP Request Container
JSP Overview - Servlets v/s JSPs Servlets : HTML within Java business logic public void do. Get(request, response) { Print. Writer out = response. get. Writer(); String name = request. get. Parameter(name); out. println(“<html><body>”); out. println("Hello” + name); out. println(“</body></html>”); } JSPs : Java within HTML Presentation logic <html> <body> <% String name = request. get. Parameter(name); %> Hello <%= name %> </body> </html>
JSP Overview - What is a JSP • In the end, a JSP is just a Servlet. Is translated to writes JSP My. Jsp. jsp Import javax. servlet. Http. Servlet. * Compiles to Is loaded and Initialized as 0010 0001 1100 1001 0011 My. Jsp_jsp. java My. Jsp_jsp. class Servlet My. Jsp_jsp Servlet
JSP Elements • Scriptlets • <% int I = 10; %> • <% Dog d = new Dog(); %> • Expressions • <%= i %> • <%= d. get. Name() %> = out. println(i); = out. println(d. get. Name()); • Declarations • <%! int i=10; %> • <%! void display() { System. out. println(“Hello”); } %> • Directives • Pages - <%@ page import=“foo. *, bar. *” %> • include - <%@ include file=“/foo/my. Jsp. 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”); } %> </body> </html> import javax. servlet. Http. Servlet. * import foo. *; public class My. Jsp_jsp extends Http. Servlet { int count = 0; public void display() { out. println(“Hello”); } public void _jsp. Service(req, res) { int i = 0; out. println(“<html>r<body>”); out. println(“Hello! Welcome”); } }
Q&A
3b202cc010ec6a555ccc70843cc0c263.ppt