SlideShare a Scribd company logo
1 of 368
Java Servlets and Java Server Pages (JSP) Atul Kahate   (akahate@gmail.com)
Introduction to HTTP and Web Interactions
Request-Response Interchange
Java and Web Interactions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
HTTP Request-Response Example Web Browser Web Server GET /files/new/image1  HTTP/1.1 Accept: image/gif Accept: image/jpeg HTTP /1.1  200  OK Date: Tue, 19-12-00 15:58:10 GMT Server: MyServer Content-length: 3010 …  (Actual data for the image) Request Response
Important HTTP Request Commands Remove a Web page DELETE Request to accept data that will be written to the client’s output stream POST Request to obtain just the header of a Web page HEAD Request to obtain a Web page GET Description HTTP command
Introduction to Servlets
Servlets Basics ,[object Object],[object Object],[object Object],[object Object]
JSP-Servlet Relationship ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP-Servlet Concept Browser Web server HTTP request (GET) JSP Compile Servlet Interpret and Execute HTML HTTP response (HTML) Servlet Container
Servlet Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Web Server and Servlet Container – Difference
Need for Container ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Servlet Multi-threading
Servlet Advantages ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Development and Execution Environment
How to Create and Run Servlets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Tomcat Directory/File Structure
Tomcat Directories/Files Description Temporary files and directories for Tomcat work Web applications webapps Internal classes server Log files logs JAR files that contain classes that are available to all Web applications lib Configuration files conf Classes available to internal and Web applications common Unpacked classes that are available to all Web applications classes The binary executables and scripts bin Description Directory
Using Tomcat ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
How to Deploy a Web Application ,[object Object],[object Object],[object Object],[object Object],[object Object]
Tomcat: Important Directories Contains any JAR files that contain Java classes that are needed by this application, but not by other Web applications. EB-INFib Contains servlets and other Java classes that are not compressed into a JAR file. If Java packages are used, each package must be stored in a subdirectory that has the same name as the package. EB-INFlasses Contains a file named web.xml. It can be used to configure servlets and other components that make up the application. EB-INF Contains sub-directories, the index file, and HTML/JSP files for the application. doument root Description Directory
Deploying Servlets and JSPs ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Servlet Lifecycle and Execution
 
GenericServlet and HttpServlet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
<<interface>> javax.servlet.Servlet service (…) init (…) destroy (…) … <<abstract class>> javax.servlet.GenericServlet service (…) init (…) destroy (…) … <<abstract class>> javax.servlet. http.HttpServlet service (…) init (…) destroy (…) doGet  (…) doPost (…) …
Servlet Lifecycle ,[object Object],[object Object],[object Object],[object Object],[object Object]
service () Method ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Servlet Execution Overview – 1 Browser HTTP  request Web server Container Servlet Container creates HTTPServletRequest and HTTPServletResponse objects and invokes the servlet, passing these objects to the servlet User clicks on a link that sends an HTTP request to the Web server to invoke a servlet Web server receives the request and hands it over to the container Thread Servlet thread is created to serve this request
Servlet Execution Overview – 2 Container Servlet Container calls (a) The servlet’s service method, if supplied, else (b) The doGet or doPost method The doGet or doPost method generates the response, and embeds it inside the response object – Remember the container still has a reference to it! HTTP  response doGet ( ) { … } Thread
Servlet Execution Overview – 3 Browser HTTP  response Web server Container Servlet Servlet thread and the request and response objects are destroyed by now Web server forwards the HTTP response to the browser, which interprets and displays HTML Container forwards the HTTP response to the Web server HTTP  response Thread
Understanding Servlet Lifecycle - 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Deployment Descriptor ,[object Object],[object Object],[object Object]
DD Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Name for referring to it elsewhere in the DD Full name, including any package details Name of the servlet again Client’s URL will have this
Understanding Servlet Lifecycle - 2 ,[object Object],[object Object],[object Object]
HTTP Request-Response – Step 1 Payment Details Card number:  Valid till: Submit Cancel Client Server
HTTP Request-Response – Step 2 Client Server POST /project/myServlet HTTP/1.1 Accept: image/gif, application/msword, … … cardnumber=1234567890123&validtill=022009 … HTTP request
init () Method ,[object Object],[object Object],[object Object],[object Object],[object Object]
HTTP Request-Response – Step 3 Client Server http/1.1 200 OK … <html> <head> <title>Hello World!</title> <body> <h1> … … HTTP response
Difference Between doGet ( ) and doPost ( ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Servlets and Multi-threading ,[object Object],[object Object],[object Object]
Multi-threading in Servlets Web browser Container Web browser HTTP request HTTP request Servlet Thread A Thread B request response request response
destroy () Method ,[object Object],[object Object]
“ Hello World” Servlet Example (HelloWWW.java) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Servlet Code: Quick Overview ,[object Object],[object Object],[object Object],[object Object]
PrintWriter Class ,[object Object],[object Object],[object Object]
Exercise: Currency Conversion ,[object Object]
Servlets: Accepting User Input – 1  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Servlets: Accepting User Input – 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exercise ,[object Object]
Process Credit Card Details (HTML Page) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Credit Card Details Servlet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exercises ,[object Object],[object Object],[object Object]
How to read multiple form values using a different syntax? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Servlet (ShowParameters.java) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Configuring Applications
More Details on the web.xml File ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Deploy or Redeploy Servlets Without Restarting Tomcat ,[object Object],[object Object],[object Object],[object Object],[object Object]
Invoking a Servlet Without a web.xml Mapping
No need to Edit web.xml Every Time ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Invoking Servlets Now ,[object Object],[object Object],[object Object],[object Object]
Use of init ()
Use of init () ,[object Object],[object Object],[object Object]
Use of init () ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Introduction to JSP
JSP Advantages ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP Lifecycle JSP source code Java source code (Servlet) Compiled Java class Interpret and Execute Written by the developer. Text file ( .jsp ) consisting of HTML code, Java statements, etc. 1. JSP source code JSP container translates JSP code into a servlet (i.e. Java code) as needed.  2. Java source code Servlet code is compiled into Java bytecode (i.e. a  .class  file), ready to be loaded and executed. 3. Compiled Java class Description Step
 
“ Hello World” JSP Example (HelloWWW.jsp) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP is also Multi-threaded
Elements of JSP JSP Syntax and Semantics
JSP Page Anatomy
Components of a JSP Page ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
More Details
Directives Overview
Directives Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The page Directive  ,[object Object],[object Object],[object Object],[object Object],[object Object],Include other classes, packages etc import Specifies the MIME type to be used contentType Can the page handle multiple client requests? isThreadSafe Indicates whether the JSP page needs session management session Inheritance relationship, if any extends Always Java language Purpose Attribute
Usage of the page Directive – 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Usage of the page Directive – 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP Comments: Two Types ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Scripting Elements Expressions Scriptlets Declarations
Expressions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],HTML portion Java portion
More Expression Examples ,[object Object],[object Object],[object Object]
Exercise: Identify Valid Expressions <%= 5 > 3 %> <% = 42*20 %> <%= String s = “foo” %> <%= Math.random () %> <%= “27” %> <%= ((Math.random () + 5 * 2); %> <%= 27 %> Valid/Invalid and why? Expression
Exercise: Identify Valid Expressions Valid: Results into a Boolean <%= 5 > 3 %> Invalid: Space between % and = <% = 42*20 %> Invalid: Variable declaration not allowed <%= String s = “foo” %> Valid: method returning a  double <%= Math.random () %> Valid: String literal <%= “27” %> Invalid: No semicolon allowed <%= ((Math.random () + 5 * 2); %> Valid: All primitive literals are ok <%= 27 %> Valid/Invalid and why? Expression
Scriptlets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],HTML code JSP code HTML code JSP code
Understanding how this works ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Another Scriptlet Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP Exercise ,[object Object]
Scriptlets Exercise ,[object Object],[object Object]
Solution to the Scriptlets Exercise ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Declarations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Where to declare a Variable? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Declaring a variable in a scriptlet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Declaring a variable in a declaration ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Another Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP: A Complete Example – All Elements
Actions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exercise ,[object Object],<%= pageContext.getAttribute (“Name”) %> <jsp include file = “test.html” <%@ page import = “java.util.*” <%! int y = 3; %> <% Float one = new Float (42.5); %> Language element Syntax
Exercise ,[object Object],Expression Action Directive Declaration Scriptlet Language element Gets translated into  write  statements of the  service  method <%= pageContext.getAttribute (“Name”) %> Causes the file to be included <jsp include file = “test.html” A page directive with an  import  attribute translates into a Java  import  statement <%@ page import = “java.util.*”> A member declaration is inside a class, but outside of any method <%! int y = 3; %> Goes inside the  service  method of the JSP <% Float one = new Float (42.5); %> Explanation Syntax
Exercises ,[object Object],[object Object],[object Object]
Exercise ,[object Object]
factorial.html ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
factorial.jsp ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exercise ,[object Object]
CurrencyCrossConversion.html ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
CurrencyCrossConversion.jsp ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exercise ,[object Object]
forexample.jsp ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Simple Example of using a Java Class in a Servlet
Servlet (HelloWWW2.java) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
ServletUtilities.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Important Implicit Objects in JSP request response pageContext session application out
Implicit Objects – Basic Concepts ,[object Object],[object Object],[object Object],[object Object]
Implicit Objects:  request  Object ,[object Object],[object Object],Web Browser Web Server GET /file/ login.jsp  HTTP/1.1 … <Other data> …  ID = atul , password = … HTTP Request login.jsp { String uID = request.getParameter (ID); …
Implicit Objects:  response  Object ,[object Object],[object Object],Web Browser Web Server <HTTP headers> <HTML> Cookie ID = atul … HTTP Response login.jsp ... <HTML> Cookie mycookie = new Cookie (“ID&quot;, “atul&quot;); response.addCookie (mycookie);
Implicit Objects:  pageContext  Object ,[object Object],Application Session Request Page
Using pageContext ,[object Object],[object Object],[object Object],[object Object],[object Object]
pageContext Examples ,[object Object],[object Object],[object Object],[object Object],[object Object]
Implicit Objects:  session  Object ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implicit Objects:  application  Object ,[object Object],[object Object],[object Object]
Implicit Objects:  out  Object ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating Welcome Files for a Web Application
Configuring Welcome Files ,[object Object],[object Object]
Edit Your Application Directory’s web.xml file ,[object Object],[object Object],[object Object],[object Object],[object Object]
Transferring Control to Other Pages
Two methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Understanding Redirect – 1 HTTP Request … 1. Client types a URL in the browser’s URL bar 2. Servlet decides that it cannot handle this – hence, REDIRECT! response.sendRedirect (New URL) HTTP Response Status = 301 Location = new URL 3. Browser sees the 301 status code and looks for a  Location  header
Understanding Redirect – 2 HTTP Request … 1. Browser sends a new request to the  new URL 2. Servlet processes this request like any other request HTTP Response Status = 200 OK … 3. Browser renders HTML as usual
Understanding Redirect – 3 ,[object Object],[object Object]
Redirect Example – 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Redirect Example – 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Understanding Request Dispatch – 1 HTTP Request … 1. Browser sends a request for a servlet 2. Servlet decides to forward this request to another servlet/JSP HTTP Response Status = 200 OK … 4. Browser renders HTML as usual RequestDispatcher view = request.getRequestDispatcher (“result.jsp”); view.forward (request, response); 3. result.jsp produces an HTTP response
Understanding Request Dispatch – 2 ,[object Object],[object Object]
Dispatch Example – 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Dispatch Example – 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using Regular Java Classes with JSP http://localhost:8080/examples/join_email.html
Using Java Classes in JSP: An Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
User class: Plain Java class (Actually a JavaBean) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
UserIO class: Plain Java class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
HTML page ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP page that uses the Java classes defined earlier ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using Servlets and JSP Together Model-View-Control (MVC) Architecture
Traditional Web Applications using Servlets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The basics of MVC ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Separating Request Processing, Business Logic, and Presentation
MVC: Depicted Client JSP Servlet Legacy code View Controller Model <% %> JSP: The  View 1. Gets the state of the model from the controller 2. Gets the user input and provides it to the controller Servlet: The  Controller 1. Takes user input and decides what action to take on it 2. Tells the model to update itself and makes the new model state available to the view (the JSP) class shopping { … Java code: The  Model 1. Performs business logic and state management 2. Performs database interactions DB
Case Study using MVC ,[object Object],[object Object]
Application Flow – Part 1 Web browser Web server Container 1 <html> <head> … </html> SelectPlayer. html 2 1. User sends a request for the page SelectPlayer.html. 2. Web server sends that page to the browser. See next slide Container logic Servlet Player Expert Controller Model Result.jsp View
Initial HTML Screen – SelectPlayer.html
Application Flow – Part 2 Web browser Web server Container 3 <html> <head> … </html> 10 3. User selects player. 4. Container calls  getPlayer  servlet. 5. Servlet calls  PlayerExpert  class. 6.  PlayerExpert  class returns an answer, which the servlet adds to the  request  object. 7. Servlet forwards the  request  to the JSP. 8. JSP reads the  request  object. 9. JSP generates the output and sends to the container. 10. Container returns result to the user. Container logic Servlet Player Expert Controller Model Result.jsp View 4 5 6 Request 7 8 9
Developing the Application – SelectPlayer.html ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Developing the Application – Controller Servlet (getPlayer) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Output of the Servlet – First Version 1
Building and Testing the Model Class (PlayerExpert.java) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Controller Servlet (getPlayer) – Version 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Sample Output as a Result of Modifying the Controller Servlet
Our Application Flow At This Stage Web browser Web server Container 1 <html> <head> … </html> 5 1. Browser sends user’s request to the container. 2. The container finds the correct servlet based on the URL and passes on the user’s request to the servlet. 3. The servlet calls the  PlayerExpert  class. 4. The servlet outputs the response (which prints more information about the player). 5. The container returns this to the user. Container logic Servlet Player Expert Controller Model 2 3 4
Our Application Flow - The Ideal Architecture Web browser Web server Container 1 <html> <head> … </html> 8 Container logic Servlet Player Expert Controller Model Result.jsp View 2 3 4 Request 5 6 7
Developing the Application – result.jsp – The  View ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Changes to the Controller Servlet ,[object Object],[object Object],[object Object],[object Object]
Controller Servlet (getPlayer) – Version 3 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
RequestDispatcher ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Session Management
The Need for Session Management ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Session Concept
Technology behind Session Management ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Possible Approaches Browser Server HTTP Response HTTP/1.0 200 OK Set-cookie: sid=test123 HTTP Request GET /next.jsp HTTP/1.0 Cookie: sid=test123 Cookie method HTTP Response HTTP/1.0 200 OK <input type=hidden name=sid value=test123> HTTP Request POST /next.jsp HTTP/1.0 sid=test123 Hidden form field method HTTP Response HTTP/1.0 200 OK <a href=next.jsp; sid=test123 >Next page</a> HTTP Request GET /next.jsp; sid=test123  HTTP/1.0 URL rewriting method
Cookie Exchange: Technical Level – 1 ,[object Object],Web browser Server/ Container HTTP/1.1 200 OK Set-Cookie: JSESSIONID = 0AAB6C8DE415 Content-type: text/html Date: Tue, 29 Mar 2005 11:25:40 GMT … <html> … </html> HTTP Response Here is your cookie containing the session ID
Cookie Exchange: Technical Level – 2 ,[object Object],Web browser Server/ Container POST  SelectDetails HTTP/1.1 Host: www.cricket.com Cookie: JSESSIONID = 0AAB6C8DE415 Accept: text/xml, … Accept-Language: en-us, … … … HTTP Request Here is my cookie containing the session ID
Programmer and Session Management – 1 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Programmer and Session Management – 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Adding and Retrieving Session Variables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Session Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Another Session Example – 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Another Session Example – 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Another Session Example – 3 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Session Objects are Threadsafe
URL Rewriting
URL Rewriting Basics ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
URL Rewriting ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],URL + ;jsessionid = B1237U5619T mail.yahoo.com;jsessionid = B1237U5619T
URL Rewriting – 1 ,[object Object],Web browser Server/ Container HTTP/1.1 200 OK Content-type: text/html Date: Tue, 29 Mar 2005 11:25:40 GMT … <html> <a href=http://www.cricket.com/PlayerExpert;jsessionid=B1237U5619T Click me </a> … </html> HTTP Response
URL Rewriting – 2 ,[object Object],Web browser Server/ Container GET /SelectDetails; jsessionid= B1237U5619T   Host: www.cricket.com Accept: text/xml, … … HTTP Request The session ID comes back to the server as extra information added to the end of the URL
Programmer and URL Rewriting ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Add session ID info to the URL Get a session
URL Rewriting: The Pitfalls ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle
Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle

More Related Content

What's hot

Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletFahmi Jafar
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technologyvikram singh
 
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servletsServlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servletsJavaEE Trainers
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)Talha Ocakçı
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course JavaEE Trainers
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtapVikas Jagtap
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsi krishna
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1vikram singh
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technologyMinal Maniar
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsKaml Sah
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 

What's hot (20)

Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
 
Servlets
ServletsServlets
Servlets
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
 
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servletsServlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Jsp servlets
Jsp servletsJsp servlets
Jsp servlets
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
JDBC
JDBCJDBC
JDBC
 
Servlets
ServletsServlets
Servlets
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
Java servlets
Java servletsJava servlets
Java servlets
 

Similar to Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle

Similar to Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle (20)

Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
 
Servlet 01
Servlet 01Servlet 01
Servlet 01
 
Servlet
Servlet Servlet
Servlet
 
Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, API
 
Servlets
ServletsServlets
Servlets
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Unit5 servlets
Unit5 servletsUnit5 servlets
Unit5 servlets
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Java servlets
Java servletsJava servlets
Java servlets
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Web Applications and Deployment
Web Applications and DeploymentWeb Applications and Deployment
Web Applications and Deployment
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 

Recently uploaded

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 

Recently uploaded (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 

Java Servlets and JSPs: Intro to HTTP Requests and Servlet Lifecycle

  • 1. Java Servlets and Java Server Pages (JSP) Atul Kahate (akahate@gmail.com)
  • 2. Introduction to HTTP and Web Interactions
  • 4.
  • 5. HTTP Request-Response Example Web Browser Web Server GET /files/new/image1 HTTP/1.1 Accept: image/gif Accept: image/jpeg HTTP /1.1 200 OK Date: Tue, 19-12-00 15:58:10 GMT Server: MyServer Content-length: 3010 … (Actual data for the image) Request Response
  • 6. Important HTTP Request Commands Remove a Web page DELETE Request to accept data that will be written to the client’s output stream POST Request to obtain just the header of a Web page HEAD Request to obtain a Web page GET Description HTTP command
  • 8.
  • 9.
  • 10. JSP-Servlet Concept Browser Web server HTTP request (GET) JSP Compile Servlet Interpret and Execute HTML HTTP response (HTML) Servlet Container
  • 11.
  • 12. Web Server and Servlet Container – Difference
  • 13.
  • 15.
  • 17.
  • 19. Tomcat Directories/Files Description Temporary files and directories for Tomcat work Web applications webapps Internal classes server Log files logs JAR files that contain classes that are available to all Web applications lib Configuration files conf Classes available to internal and Web applications common Unpacked classes that are available to all Web applications classes The binary executables and scripts bin Description Directory
  • 20.
  • 21.
  • 22. Tomcat: Important Directories Contains any JAR files that contain Java classes that are needed by this application, but not by other Web applications. EB-INFib Contains servlets and other Java classes that are not compressed into a JAR file. If Java packages are used, each package must be stored in a subdirectory that has the same name as the package. EB-INFlasses Contains a file named web.xml. It can be used to configure servlets and other components that make up the application. EB-INF Contains sub-directories, the index file, and HTML/JSP files for the application. doument root Description Directory
  • 23.
  • 25.  
  • 26.
  • 27. <<interface>> javax.servlet.Servlet service (…) init (…) destroy (…) … <<abstract class>> javax.servlet.GenericServlet service (…) init (…) destroy (…) … <<abstract class>> javax.servlet. http.HttpServlet service (…) init (…) destroy (…) doGet (…) doPost (…) …
  • 28.
  • 29.
  • 30. Servlet Execution Overview – 1 Browser HTTP request Web server Container Servlet Container creates HTTPServletRequest and HTTPServletResponse objects and invokes the servlet, passing these objects to the servlet User clicks on a link that sends an HTTP request to the Web server to invoke a servlet Web server receives the request and hands it over to the container Thread Servlet thread is created to serve this request
  • 31. Servlet Execution Overview – 2 Container Servlet Container calls (a) The servlet’s service method, if supplied, else (b) The doGet or doPost method The doGet or doPost method generates the response, and embeds it inside the response object – Remember the container still has a reference to it! HTTP response doGet ( ) { … } Thread
  • 32. Servlet Execution Overview – 3 Browser HTTP response Web server Container Servlet Servlet thread and the request and response objects are destroyed by now Web server forwards the HTTP response to the browser, which interprets and displays HTML Container forwards the HTTP response to the Web server HTTP response Thread
  • 33.
  • 34.
  • 35.
  • 36.
  • 37. HTTP Request-Response – Step 1 Payment Details Card number: Valid till: Submit Cancel Client Server
  • 38. HTTP Request-Response – Step 2 Client Server POST /project/myServlet HTTP/1.1 Accept: image/gif, application/msword, … … cardnumber=1234567890123&validtill=022009 … HTTP request
  • 39.
  • 40. HTTP Request-Response – Step 3 Client Server http/1.1 200 OK … <html> <head> <title>Hello World!</title> <body> <h1> … … HTTP response
  • 41.
  • 42.
  • 43. Multi-threading in Servlets Web browser Container Web browser HTTP request HTTP request Servlet Thread A Thread B request response request response
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 58.
  • 59.
  • 60. Invoking a Servlet Without a web.xml Mapping
  • 61.
  • 62.
  • 64.
  • 65.
  • 67.
  • 68. JSP Lifecycle JSP source code Java source code (Servlet) Compiled Java class Interpret and Execute Written by the developer. Text file ( .jsp ) consisting of HTML code, Java statements, etc. 1. JSP source code JSP container translates JSP code into a servlet (i.e. Java code) as needed. 2. Java source code Servlet code is compiled into Java bytecode (i.e. a .class file), ready to be loaded and executed. 3. Compiled Java class Description Step
  • 69.  
  • 70.
  • 71. JSP is also Multi-threaded
  • 72. Elements of JSP JSP Syntax and Semantics
  • 74.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82. Scripting Elements Expressions Scriptlets Declarations
  • 83.
  • 84.
  • 85. Exercise: Identify Valid Expressions <%= 5 > 3 %> <% = 42*20 %> <%= String s = “foo” %> <%= Math.random () %> <%= “27” %> <%= ((Math.random () + 5 * 2); %> <%= 27 %> Valid/Invalid and why? Expression
  • 86. Exercise: Identify Valid Expressions Valid: Results into a Boolean <%= 5 > 3 %> Invalid: Space between % and = <% = 42*20 %> Invalid: Variable declaration not allowed <%= String s = “foo” %> Valid: method returning a double <%= Math.random () %> Valid: String literal <%= “27” %> Invalid: No semicolon allowed <%= ((Math.random () + 5 * 2); %> Valid: All primitive literals are ok <%= 27 %> Valid/Invalid and why? Expression
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98. JSP: A Complete Example – All Elements
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111. Simple Example of using a Java Class in a Servlet
  • 112.
  • 113.
  • 114. Important Implicit Objects in JSP request response pageContext session application out
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124. Creating Welcome Files for a Web Application
  • 125.
  • 126.
  • 127. Transferring Control to Other Pages
  • 128.
  • 129. Understanding Redirect – 1 HTTP Request … 1. Client types a URL in the browser’s URL bar 2. Servlet decides that it cannot handle this – hence, REDIRECT! response.sendRedirect (New URL) HTTP Response Status = 301 Location = new URL 3. Browser sees the 301 status code and looks for a Location header
  • 130. Understanding Redirect – 2 HTTP Request … 1. Browser sends a new request to the new URL 2. Servlet processes this request like any other request HTTP Response Status = 200 OK … 3. Browser renders HTML as usual
  • 131.
  • 132.
  • 133.
  • 134. Understanding Request Dispatch – 1 HTTP Request … 1. Browser sends a request for a servlet 2. Servlet decides to forward this request to another servlet/JSP HTTP Response Status = 200 OK … 4. Browser renders HTML as usual RequestDispatcher view = request.getRequestDispatcher (“result.jsp”); view.forward (request, response); 3. result.jsp produces an HTTP response
  • 135.
  • 136.
  • 137.
  • 138. Using Regular Java Classes with JSP http://localhost:8080/examples/join_email.html
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144. Using Servlets and JSP Together Model-View-Control (MVC) Architecture
  • 145.
  • 146.
  • 147. Separating Request Processing, Business Logic, and Presentation
  • 148. MVC: Depicted Client JSP Servlet Legacy code View Controller Model <% %> JSP: The View 1. Gets the state of the model from the controller 2. Gets the user input and provides it to the controller Servlet: The Controller 1. Takes user input and decides what action to take on it 2. Tells the model to update itself and makes the new model state available to the view (the JSP) class shopping { … Java code: The Model 1. Performs business logic and state management 2. Performs database interactions DB
  • 149.
  • 150. Application Flow – Part 1 Web browser Web server Container 1 <html> <head> … </html> SelectPlayer. html 2 1. User sends a request for the page SelectPlayer.html. 2. Web server sends that page to the browser. See next slide Container logic Servlet Player Expert Controller Model Result.jsp View
  • 151. Initial HTML Screen – SelectPlayer.html
  • 152. Application Flow – Part 2 Web browser Web server Container 3 <html> <head> … </html> 10 3. User selects player. 4. Container calls getPlayer servlet. 5. Servlet calls PlayerExpert class. 6. PlayerExpert class returns an answer, which the servlet adds to the request object. 7. Servlet forwards the request to the JSP. 8. JSP reads the request object. 9. JSP generates the output and sends to the container. 10. Container returns result to the user. Container logic Servlet Player Expert Controller Model Result.jsp View 4 5 6 Request 7 8 9
  • 153.
  • 154.
  • 155. Output of the Servlet – First Version 1
  • 156.
  • 157.
  • 158. Sample Output as a Result of Modifying the Controller Servlet
  • 159. Our Application Flow At This Stage Web browser Web server Container 1 <html> <head> … </html> 5 1. Browser sends user’s request to the container. 2. The container finds the correct servlet based on the URL and passes on the user’s request to the servlet. 3. The servlet calls the PlayerExpert class. 4. The servlet outputs the response (which prints more information about the player). 5. The container returns this to the user. Container logic Servlet Player Expert Controller Model 2 3 4
  • 160. Our Application Flow - The Ideal Architecture Web browser Web server Container 1 <html> <head> … </html> 8 Container logic Servlet Player Expert Controller Model Result.jsp View 2 3 4 Request 5 6 7
  • 161.
  • 162.
  • 163.
  • 164.
  • 166.
  • 168.
  • 169. Possible Approaches Browser Server HTTP Response HTTP/1.0 200 OK Set-cookie: sid=test123 HTTP Request GET /next.jsp HTTP/1.0 Cookie: sid=test123 Cookie method HTTP Response HTTP/1.0 200 OK <input type=hidden name=sid value=test123> HTTP Request POST /next.jsp HTTP/1.0 sid=test123 Hidden form field method HTTP Response HTTP/1.0 200 OK <a href=next.jsp; sid=test123 >Next page</a> HTTP Request GET /next.jsp; sid=test123 HTTP/1.0 URL rewriting method
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179. Session Objects are Threadsafe
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.

Editor's Notes

  1. /* * CurrencyConvertor.java * * Created on March 25, 2005, 4:14 PM */ import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; /** * * @author comp * @version */ public class CurrencyConvertor extends HttpServlet { /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); } /** Destroys the servlet. */ public void destroy() { } /** Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(&amp;quot;text/html&amp;quot;); PrintWriter out = response.getWriter(); /* TODO output your page here */ out.println(&amp;quot;&lt;html&gt;&amp;quot;); out.println(&amp;quot;&lt;head&gt;&amp;quot;); out.println(&amp;quot;&lt;title&gt;Dollars to Rupees Conversion Chart&lt;/title&gt;&amp;quot;); out.println(&amp;quot;&lt;/head&gt;&amp;quot;); out.println(&amp;quot;&lt;body&gt;&amp;quot;); out.println (&amp;quot;&lt;center&gt;&amp;quot;); out.println (&amp;quot;&lt;h1&gt;Currency Conversion Chart&lt;/h1&gt;&amp;quot;); out.println (&amp;quot;&lt;table border=&apos;1&apos; cellpadding=&apos;3&apos; cellspacing=&apos;0&apos;&gt;&amp;quot;); out.println (&amp;quot;&lt;tr&gt;&amp;quot;); out.println (&amp;quot;&lt;th&gt;Dollars&lt;/th&gt;&amp;quot;); out.println (&amp;quot;&lt;th&gt;Rupees&lt;/th&gt;&amp;quot;); out.println (&amp;quot;&lt;/tr&amp;quot;); for (int dollars = 1; dollars &lt;= 50; dollars++) { int rupees = dollars * 45; out.println (&amp;quot;&lt;tr&gt;&amp;quot; + &amp;quot;&lt;td align=&apos;right&apos;&gt;&amp;quot; + dollars + &amp;quot;&lt;/td&gt;&amp;quot; + &amp;quot;&lt;td align=&apos;right&apos;&gt;&amp;quot; + rupees + &amp;quot;&lt;/td&gt;&amp;quot; + &amp;quot;&lt;/tr&gt;&amp;quot;); } out.println(&amp;quot;&lt;/table&gt;&amp;quot;); out.println(&amp;quot;&lt;/center&gt;&amp;quot;); out.println(&amp;quot;&lt;/body&gt;&amp;quot;); out.println(&amp;quot;&lt;/html&gt;&amp;quot;); out.close(); } /** Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return &amp;quot;Short description&amp;quot;; } }
  2. import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; /** * * @author comp * @version */ public class emailServlet extends HttpServlet { /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); } /** Destroys the servlet. */ public void destroy() { } /** Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String email; email = request.getParameter (&amp;quot;email&amp;quot;); response.setContentType(&amp;quot;text/html&amp;quot;); PrintWriter out = response.getWriter(); out.println(&amp;quot;&lt;html&gt;&amp;quot;); out.println(&amp;quot;&lt;head&gt;&amp;quot;); out.println(&amp;quot;&lt;title&gt;Servlet Example&lt;/title&gt;&amp;quot;); out.println(&amp;quot;&lt;/head&gt;&amp;quot;); out.println(&amp;quot;&lt;body&gt;&amp;quot;); out.println (&amp;quot;&lt;P&gt; The email ID you have entered is: &amp;quot; + email + &amp;quot;&lt;/P&gt;&amp;quot;); out.println(&amp;quot;&lt;/body&gt;&amp;quot;); out.println(&amp;quot;&lt;/html&gt;&amp;quot;); out.close(); } /** Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return &amp;quot;Short description&amp;quot;; } }
  3. /* * cookieWriter.java * * Created on March 25, 2005, 4:51 PM */ import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; /** * * @author comp * @version */ public class cookieWriter extends HttpServlet { /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); } /** Destroys the servlet. */ public void destroy() { } /** Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* TODO output your page here */ Cookie myCookie = new Cookie (&amp;quot;userID&amp;quot;, &amp;quot;123&amp;quot;); response.addCookie (myCookie); response.setContentType(&amp;quot;text/html&amp;quot;); PrintWriter out = response.getWriter(); out.println(&amp;quot;&lt;html&gt;&amp;quot;); out.println(&amp;quot;&lt;head&gt;&amp;quot;); out.println(&amp;quot;&lt;title&gt;Writing Cookie&lt;/title&gt;&amp;quot;); out.println(&amp;quot;&lt;/head&gt;&amp;quot;); out.println(&amp;quot;&lt;body&gt;&amp;quot;); out.println (&amp;quot;&lt;H1&gt; This servlet has written a cookie &lt;/H1&gt;&amp;quot;); out.println(&amp;quot;&lt;/body&gt;&amp;quot;); out.println(&amp;quot;&lt;/html&gt;&amp;quot;); out.close(); } /** Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return &amp;quot;Short description&amp;quot;; } }
  4. /* * cookieReader.java * * Created on March 25, 2005, 5:12 PM */ import java.io.*; import java.net.*; import java.lang.*; import javax.servlet.*; import javax.servlet.http.*; /** * * @author comp * @version */ public class cookieReader extends HttpServlet { /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); } /** Destroys the servlet. */ public void destroy() { } /** Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(&amp;quot;text/html&amp;quot;); PrintWriter out = response.getWriter(); /* TODO output your page here */ out.println(&amp;quot;&lt;html&gt;&amp;quot;); out.println(&amp;quot;&lt;head&gt;&amp;quot;); out.println(&amp;quot;&lt;title&gt;Cookie Reader&lt;/title&gt;&amp;quot;); out.println(&amp;quot;&lt;/head&gt;&amp;quot;); out.println(&amp;quot;&lt;body&gt;&amp;quot;); out.println (&amp;quot;&lt;H1&gt; This servlet is reading the cookie set previously &lt;/H1&gt;&amp;quot;); out.println (&amp;quot;&lt;P&gt; &lt;/P&gt;&amp;quot;); Cookie [] cookies = request.getCookies (); if (cookies == null) { out.println (&amp;quot;No cookies&amp;quot;); } else { Cookie MyCookie; for (int i=0; i &lt; cookies.length; i++) { MyCookie = cookies [i]; String name = MyCookie.getName (); // Cookie name String value = MyCookie.getValue (); // Cookie value int age = MyCookie.getMaxAge(); // Lifetime: -1 if cookie expires on browser closure String domain = MyCookie.getDomain (); // Domain name out.println (name + &amp;quot; &amp;quot; + value + &amp;quot; &amp;quot; + domain + &amp;quot; &amp;quot; + age); } } out.println(&amp;quot;&lt;/body&gt;&amp;quot;); out.println(&amp;quot;&lt;/html&gt;&amp;quot;); out.close(); } /** Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return &amp;quot;Short description&amp;quot;; } }