Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.

Similar presentations


Presentation on theme: "COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction."— Presentation transcript:

1 COMP 321 Week 7

2 Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction

3 What does a web server do? Receives request from client Returns response to client

4 What does a web client do? Sends request to server Receives response Displays response to the user

5 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

6 HTML A Login Page A Login Page Skyler's Login Page Skyler's Login Page Name: Name: Password: Password:

7 HTML

8 HTTP Runs on top of TCP/IP Request Contains: –HTTP Method (GET, POST, etc.) –Page to access (ex. www.google.com) –Form Parameters Response Contains: –Status Code (ex. 200, 404, etc.) –Content-type (ex. text/html, image/jpg) –Content (ex. HTML document, picture, etc.)

9 HTTP Methods GET –Used to retrieve an object –Idempotent POST –Used to send data, perform an operation, etc. –Not idempotent

10 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

11 HTTP Requests WebServe Demo Check out http://gauss.ececs.uc.edu/Users/Franco/W eek7/J0/WebServe.java.html for more info http://gauss.ececs.uc.edu/Users/Franco/W eek7/J0/WebServe.java.html http://gauss.ececs.uc.edu/Users/Franco/W eek7/J0/WebServe.java.html

12 HTTP Response HTTP/1.1 200 OK Set-Cookie: JSESSIONID=0AAB6C8DE415E2E5F307CF334BFCA0C1; Path=/testEL Content-Type: text/html Content-Length: 397 Date: Wed, 19 Nov 2003 03:25:40 GMT Server: Apache-Coyote/1.1 Connection: close <html>...</html>

13 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

14 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

15 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

16 URL: Uniform Resource Locator http://www.wickedlysmart.com:80/beeradvi ce/select/beer1.html http://www.wickedlysmart.com:80/beeradvi ce/select/beer1.html Protocol – http Server – www.wickedlysmart.com Port – 80 Path – /beeradvice/select Resource – beer1.html Query String – not shown ?type=...

17 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

18 Mapping URLs to Content A: http://www.wickedlysmart.com/

19 Mapping URLs to Content B: http://www.wickedlysmart.com/skiingAdvice

20 Mapping URLs to Content C: http://www.wickedlysmart.com/beerAdvice

21 Mapping URLs to Content D: www.wickedlysmart.com/beerAdvice/select/selectBeer.html

22 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

23 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

24 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

25 Servlets Live in a container Are called by container when specific requests arrive  doGet() called for GET requests  doPost() called for POST requests

26 The Servlet Container Handles all communication Manages Servlet Lifecycle Handles Multithreading Allows Declarative Security Handles JSP Support

27 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

28 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 + " " + " " + " "); " "); }}

29 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

30 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>

31 Case Study – GeekDates Bryan creates a dating site One Servlet per page  MainPageServlet  InputDQLServlet  DoDQLQueryServlet  etc.

32 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 }}

33 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

34 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

35 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

36 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?

37 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

38 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

39 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)

40 Tomcat with Eclipse Demonstration

41 Lab 7-1 Introduction

42 Progress Check Due this week:  Lab 5-2 Database Application Interfaces Due next week:  Lab 7-1 Preparing to Build Web Applications


Download ppt "COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction."

Similar presentations


Ads by Google