Servlets & JSPs

- Sharad Ballepu

Servlets & JSPs


• • • • • • • • •

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 .

picture.  Form parameters.Introduction – what is a request and response HTTP Request Key elements of a “request” stream:  HTTP method (action to be performed). . etc…). HTTP Response Key elements of a “response” stream:  A status code (for whether the request was successful).  Content-type (text.  The page to access (a URL).  The content ( the actual content). html.

I can handle dynamic requests.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. Helper Application Web Server machine “The Helper Application is nothing but a SERVLET” .

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

Servlet Architecture – Web Container • How does the Container handle a request? Servlet response Web Server Thread Http request Web Container request Client response <Html> <Body> ……. </Body> </Html> Service() doGet() .

Servlet Architecture – Web Container The CONTAINER What is the role of Web Container ? • Communication Support • Lifecycle Management • Multi-threading support • Security • JSP Support The container can contain multiple Servlets & JSPs within it S3 S1 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 <web-app> ……… <servlet> <servlet-name>LoginServ</servlet-name> <servlet-class>com...xml . </web-app> Web.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServ</servlet-name> <url-pattern>/Logon</url-pattern> </servlet-mapping> ………. ……….

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

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

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

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 .

Idempotency Usage . Can send huge amount of data to the server. The parameter data is limited (the limit depends on the container) GET is Idempotent Generally used to fetch some information from the host. POST (doPost()) Along with request line and header it also contains HTTP body. GET (doGet()) The request contains only the request line and HTTP header. The form elements are passed in the body of the HTTP request. POST is not idempotent Generally used to process the sent data.Request and Response – GET v/s POST • The HTTP request method determines whether doGet() or doPost() runs. HTTP Request Parameter passing Size The form elements are passed to the server by appending at the end of the URL.

Request and Response – The response Request Can the Servlet Serve the request? No Does the Servlet know Who can serve? No Yes Yes Send resource Send Redirect 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 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 .

is an attribute? • Difference between Attributes and parameters Attributes Types Context Request Session setAttribute(String.Attributes • What exactly. Method to set Return type Method to get String getInitParameter (String) . Object) Object getAttribute(String) Parameters Context Request Servlet Init We cannot set Init parameters.Being a Web Container .

“dark” Client A new 2 ID# 42 “dark” HttpSession response.Session Management – Session Tracking • How sessions work? 1 request. ID# 42 2 Container HttpSession . ID# 42 setAttribute(“dark”) 4 3 Container “ale” #42 ID# 42 “dark” Client A 1 request. “ale”.

1 <html> … </html> OK.1 Cookie: JSESSIONID=0ABS Accept: text/html…… Client A HTTP Request Container .Session Tracking – Cookies Here’s your cookie with session ID inside… HttpSession session = HTTP/1.getSession(). HTTP/1. here’s the cookie with my request HTTP Response Container POST / login.1 200 OK Set-Cookie: JSESSIONID=0ABS Client A Content-Type: text/html Server: Apache-Coyote/1.

jsessionid=0AAB”> click me </a> </html> HTTP Response GET /Metavante.Session Tracking – URL Rewriting URL HTTP/1.jsessionid=1234567 Container Client A Content-Type: text/html Server: Apache-Coyote/1.sharmanj.jsessionid=0AAB HTTP / 200 OK .1 Host: Accept: text/html Client A HTTP Request Container .sharmanj.1 <html> <body> < a href =“ http:// www.

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

java MyJsp_jsp.JSP Overview .class .jsp Import javax. a JSP is just a Servlet.What is a JSP • In the end.* Compiles to 0010 0001 1100 1001 0001 0011 Is loaded and Initialized as Servlet MyJsp_jsp Servlet MyJsp_jsp. servlet. HttpServlet. Is translated to writes JSP MyJsp.

• Declarations • <%! int i=10. %> • <%! void display() { System.jsp” %> • taglib . } %> • Directives • Pages .<%@ page import=“foo. bar.println(“Hello”).getName()).out.JSP Elements • Scriptlets • <% int I = 10. %> • <% Dog d = new Dog().println(i). %> = out. • Expressions • <%= i %> • <%= d.*” %> • include .<%@ taglib uri=“Tags” prefix=“cool” %> .<%@ include file=“/foo/myJsp.*.println(d.getName() %> = out.

public void display() { out. } public void _jspService(req.println(“<html>\r<body>”). res) { int i = 0. out. public class MyJsp_jsp extends HttpServlet { int count = 0. } %> import javax.servlet. } } </body> </html> .JSP Elements – JSP to Servlet • Where does the JSP code land in the Servlet? <%@ page import=“foo.HttpServlet.*” %> <html> <body> <% int i = 10. out. %> Hello! Welcome <%! Public void display() { out. %> <%! int count = 0.* import foo.*.println(“Hello”).println(“Hello! Welcome”).println(“Hello”).

Q&A .

Sign up to vote on this title
UsefulNot useful