COMP 321 Week 7
Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction
What does a web server do? Receives request from client Returns response to client
What does a web client do? Sends request to server Receives response Displays response to the user
HTML and HTTP HTTP (Hypertext Transport Protocol) –Protocol web clients and servers use to communicate –Specifies format of requests and responses HTML (Hypertext Markup Language) –Format for specifying contents of a web page
HTML A Login Page A Login Page Skyler's Login Page Skyler's Login Page Name: Name: Password: Password:
HTML
HTTP Runs on top of TCP/IP Request Contains: –HTTP Method (GET, POST, etc.) –Page to access (ex. –Form Parameters Response Contains: –Status Code (ex. 200, 404, etc.) –Content-type (ex. text/html, image/jpg) –Content (ex. HTML document, picture, etc.)
HTTP Methods GET –Used to retrieve an object –Idempotent POST –Used to send data, perform an operation, etc. –Not idempotent
GET vs. POST Both can send data Reasons to use POST –GET parameters are visible in URL –GET data size is extremely limited Reasons to use GET –Parameters can be bookmarked along with URL
HTTP Requests WebServe Demo Check out eek7/J0/WebServe.java.html for more info eek7/J0/WebServe.java.html eek7/J0/WebServe.java.html
HTTP Response HTTP/ OK Set-Cookie: JSESSIONID=0AAB6C8DE415E2E5F307CF334BFCA0C1; Path=/testEL Content-Type: text/html Content-Length: 397 Date: Wed, 19 Nov :25:40 GMT Server: Apache-Coyote/1.1 Connection: close <html>...</html>
GET or POST? A user is returning a login name and password A user is requesting a new page via a hyperlink A chat room user is sending a written response A user hits the ‘Next’ button to see the next page A user hits the ‘Log out’ button on a secure banking site A user hits the ‘Back’ button on the browser A user sends a name and address from to the server A user makes a radio button selection
GET or POST? A user is returning a login name and password POST – the login and password shouldn’t be exposed in the address bar A user is requesting a new page via a hyperlink GET – this is the typical usage for get A chat room user is sending a written response POST – the message could easily be too long for a GET A user hits the ‘Next’ button to see the next page Depending on the browser, nothing is sent to the server in this case
GET or POST? A user hits the ‘Log out’ button on a secure banking site Could be either – depends on implementation A user hits the ‘Back’ button on the browser Could be either – the browser remembers which A user sends a name and address form to the server This would usually be a POST A user makes a radio button selection Nothing is sent to the server in this case
URL: Uniform Resource Locator ce/select/beer1.html ce/select/beer1.html Protocol – http Server – Port – 80 Path – /beeradvice/select Resource – beer1.html Query String – not shown ?type=...
TCP Ports Used to allow multiple servers to run on a single machine Each connection has a server name and destination port Standard ports are defined for common services: 21 – FTP 25 – SMPT 80 – HTTP 443 – HTTPS
Mapping URLs to Content A:
Mapping URLs to Content B:
Mapping URLs to Content C:
Mapping URLs to Content D:
Dynamic Content Web servers know how to serve static pages Many sites need to generate content dynamically: at the time it is requested A helper (or CGI) application communicates with the web server to generate the content
First There Was CGI… Client requests a link to a CGI URL Server launches and runs CGI program with supplied parameters Program creates web page and returns to server Server shuts down CGI program and returns response to client
CGI vs. Servlets Launching a new CGI process each time a request is made is expensive Servlets reuse the same JVM and even the same Servlet object for each request, and only create a new thread Servlets can use J2EE security, transactions, and EJBs directly
Servlets Live in a container Are called by container when specific requests arrive doGet() called for GET requests doPost() called for POST requests
The Servlet Container Handles all communication Manages Servlet Lifecycle Handles Multithreading Allows Declarative Security Handles JSP Support
Servlet Request Processing 1. User clicks a link that contains a Servlet URL 2. Container creates HttpServletRequest and HttpServletResponse objects 3. Container finds correct Servlet, and creates thread for request 4. Container calls doGet() or doPost() with request and response objects 5. Servlet generates dynamic page, and puts it in the response object 6. The thread completes, the server converts the response to an HTTP response, and returns it to the client
Servlet Example public class Ch2Servlet extends HttpServlet { public void doGet(HttpServletRequest request, public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter(); Date today = new Date(); Date today = new Date(); out.println( out.println( " " + " " + "HF\'s Chapter2 Servlet " + "HF\'s Chapter2 Servlet " + " " + today + " " + today + " " + " " + " "); " "); }}
Servlet Mapping Basics Public URL Name – the URL that is exposed to the browser “Secret” Deployment Name – internal name used to tie URL and File Path together File Path Name – actual location of class file on disk
Deployment Descriptor Internal Name 1 Internal Name 1 foo.Servlet1 foo.Servlet1 Internal Name 2 Internal Name 2 foo.Servlet2 foo.Servlet2 Internal Name 1 Internal Name 1 /Public1 /Public1 Internal Name 2 Internal Name 2 /Public2 /Public2 </web-app>
Case Study – GeekDates Bryan creates a dating site One Servlet per page MainPageServlet InputDQLServlet DoDQLQueryServlet etc.
Case Study – GeekDates cont’d public class DatingServlet extends HttpServlet { public void doGet(HttpServletRequest request, public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException HttpServletResponse response) throws IOException // business logic goes here // business logic goes here // (read db, write db, etc.) // (read db, write db, etc.) PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter(); // Ugly HTML strings embedded in Java go here // Ugly HTML strings embedded in Java go here }}
GeekDates 2.0 Bryan moves HTML to JSPs Each servlet performs business logic, then redirects to JSP JSPs contain HTML with small amounts of Java sprinkled in JSPs only display data they’re given, and presentation is now separate from business logic
Case Study – GeekDates cont’d What if we want access GeekDates from a Swing app? Can’t be done – business logic is entangled with Servlet-specific code Bryan should have used the MVC (Model- View-Controller) pattern
MVC Model Holds state Holds business logic - contains rules for getting and updating state View Gets state from Model (through Controller), and displays it Sends user input to Controller Controller Takes user input from View and sends to Model Allows View to access Model
GeekDates 3.0 Now Bryan creates a model class for each page InputSignupModel.java InputSignupServlet.java InputSignup.jsp All of the Servlets have nearly the same logic. What can we do about this?
Who’s Responsible? Task Web Server ContainerServlet Creates request & response objects Calls the service() method Starts a new thread to handle request Converts a response object to an HTTP response Knows HTTP Adds HTML to the response object Has a reference to the response objects Finds URLs in the DD Deletes the request and response Coordinates making dynamic content Manages lifecycles Name that matches in DD
Who’s Responsible? Task Web Server ContainerServlet Creates request & response objects X Calls the service() method X Starts a new thread to handle request X Converts a response object to an HTTP response x Knows HTTP x Adds HTML to the response object x Has a reference to the response objects xx Finds URLs in the DD x Deletes the request and response x Coordinates making dynamic content xx Manages lifecycles x Name that matches in DD x
J2EE J2EE is a group of specifications – Servlet, JSP, EJB, etc. A fully-compliant container must have a Servlet container and an EJB container Tomcat is a web container, not a full J2EE application server (no EJBs)
Tomcat with Eclipse Demonstration
Lab 7-1 Introduction
Progress Check Due this week: Lab 5-2 Database Application Interfaces Due next week: Lab 7-1 Preparing to Build Web Applications