Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlets & JSPs - Sharad Ballepu. Agenda Introduction Servlet Architecture Servlet lifecycle Request and Response Being a Web Container Session management.

Similar presentations

Presentation on theme: "Servlets & JSPs - Sharad Ballepu. Agenda Introduction Servlet Architecture Servlet lifecycle Request and Response Being a Web Container Session management."— Presentation transcript:

1 Servlets & JSPs - Sharad Ballepu

2 Agenda Introduction Servlet Architecture Servlet lifecycle Request and Response Being a Web Container Session management Overview of JSP JSP Elements Q & A Servlets & JSPs

3 Request-response model. request response HTTP HTML HTTP Request … … Client Server Introduction – request-response model

4 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

5 Where does Servlet come into the picture? Web Server Application Helper Application Web Server machine I can serve only static HTML pages Not a problem. I can handle dynamic requests. The Helper Application is nothing but a SERVLET Introduction – What is a Servlet

6 What is a Web Container? GET. ….. Web Server Servlet Web Container GET. ….. GET. ….. request Client Servlet Architecture -Web Container

7 How does the Container handle a request? Web Container Servlet Thread Service() doGet() ……. request response Web Server Http request Client Servlet Architecture – Web Container

8 What is the role of Web Container ? Communication Support Lifecycle Management Multi-threading support Security JSP Support The CONTAINER S1 S3 S4 S2 JSP1 The container can contain multiple Servlets & JSPs within it Servlet Architecture – Web Container

9 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

10 The Servlet lifecycle is simple, there is only one main state – Initialized. Initialized Does not exist constructor() init() destroy() Service() Servlet Lifecycle

11 GenericServlet HttpServlet Your Servlet Servlet Interface Abstract class Concrete class If not overridden, implements init() method from the Servlet interface, If not overridden, implements service() method. We implement the HTTP methods here. Servlet Lifecycle - Hierarchy

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

13 The Container runs multiple threads to process multiple requests to a single servlet. Servlet Thread A Thread B Container Client A Client B response request Servlet Lifecycle – Thread handling

14 The HTTP request method determines whether doGet() or doPost() runs. 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 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. SizeThe parameter data is limited (the limit depends on the container) Can send huge amount of data to the server. IdempotencyGET is IdempotentPOST is not idempotent UsageGenerally used to fetch some information from the host. Generally used to process the sent data. Request and Response – GET v/s POST

15 Request Can the Servlet Serve the request? Send resource Yes Does the Servlet know Who can serve? Error page Send Redirect Request Dispatcher No Yes No Request and Response – The response

16 Servlet 2Servlet 1 Servlet 3 JSP 1 Servlet Context Servlet Config Being a Web Container – Servlet Config and Context

17 What are init parameters? Difference between Servlet Context and Config Init parameters Context Init ParametersServlet Init Parameters ScopeScope is Web ContainerSpecific to Servlet or JSP Servlet codegetServletContext()getServletConfig() Deployment Descriptor Within the element but not within a specific element Within the element for each specific servlet Being a Web Container – init parameters

18 What exactly, is an attribute? Difference between Attributes and parameters AttributesParameters Types Context Request Session Context Request Servlet Init Method to setsetAttribute(String, Object) We cannot set Init parameters. Return typeObjectString Method to getgetAttribute(String) getInitParameter (String) Being a Web Container - Attributes

19 How sessions work? ID# 42 dark ID# 42 dark ale # request, dark new setAttribute(dark)response, ID# 42 request, ale, ID# 42 HttpSession Container Client A Container Session Management – Session Tracking

20 HTTP/ OK Set-Cookie: JSESSIONID=0ABS Content-Type: text/html Server: Apache-Coyote/1.1 … POST / HTTP/1.1 Cookie: JSESSIONID=0ABS Accept: text/html…… Heres your cookie with session ID inside… OK, heres the cookie with my request HTTP Response HTTP Request HttpSession session = request.getSession(); Client A Container Session Tracking – Cookies

21 URL ;jsessionid= HTTP/ OK Content-Type: text/html Server: Apache-Coyote/1.1 click me GET /Metavante;jsessionid=0AAB HTTP / 1.1 Host: Accept: text/html Container Client A HTTP Response HTTP Request Session Tracking – URL Rewriting

22 public void doGet(request, response) { PrintWriter out = response.getWriter(); String name = request.getParameter(name); out.println( ); out.println("Hello + name); out.println( ); } <% String name = request.getParameter(name); %> Hello Servlets : HTML within Java business logic JSPs : Java within HTML Presentation logic JSP Overview - Servlets v/s JSPs

23 In the end, a JSP is just a Servlet Import javax. servlet. HttpServlet.* JSP Servlet MyJsp.jspMyJsp_jsp.javaMyJsp_jsp.class MyJsp_jsp Servlet writes Is translated toCompiles to Is loaded and Initialized as JSP Overview - What is a JSP

24 Scriptlets Expressions Declarations Directives Pages - include - taglib - = out.println(i); = out.println(d.getName()); JSP Elements

25 Where does the JSP code land in the Servlet? 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( \r ); out.println(Hello! Welcome); } JSP Elements – JSP to Servlet

26 Q & A

Download ppt "Servlets & JSPs - Sharad Ballepu. Agenda Introduction Servlet Architecture Servlet lifecycle Request and Response Being a Web Container Session management."

Similar presentations

Ads by Google