Transcript Slide 1

COMP 321
Week 7
Overview
HTML and HTTP Basics
Dynamic Web Content
Servlets
MVC
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
<html>
<!– Some sample HTML -->
<head>
<title>A Login Page</title>
</head>
<body>
<h1 align="center">Skyler's Login Page</h1>
<p align="right">
<img src="SKYLER2.jpg" width="130" height="150"/>
</p>
<form action="date2">
Name: <input type="text" name="param1"/><br/>
Password: <input type="text" name="param2"/><br/><br/><br/>
<center><input type="SUBMIT"/></center>
</form>
</body>
</html>
HTML
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.)
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
http://gauss.ececs.uc.edu/Users/Franco/W
eek7/J0/WebServe.java.html for more info
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>
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
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=...
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: http://www.wickedlysmart.com/
Mapping URLs to Content
B: http://www.wickedlysmart.com/skiingAdvice
Mapping URLs to Content
C: http://www.wickedlysmart.com/beerAdvice
Mapping URLs to Content
D: www.wickedlysmart.com/beerAdvice/select/selectBeer.html
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
3.
4.
5.
6.
HttpServletResponse objects
Container finds correct Servlet, and creates thread for
request
Container calls doGet() or doPost() with request and
response objects
Servlet generates dynamic page, and puts it in the
response object
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,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
Date today = new Date();
out.println(
"<html>" +
"<body>" +
"<h1 style=\"text-align:center\">" +
"HF\'s Chapter2 Servlet</h1>" +
"<br/>" + today +
"</body>" +
"</html>");
}
}
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
<web-app ...>
<servlet>
<servlet-name>Internal Name 1</servlet-name>
<servlet-class>foo.Servlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>Internal Name 2</servlet-name>
<servlet-class>foo.Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Internal Name 1</servlet-name>
<url-pattern>/Public1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Internal Name 2</servlet-name>
<url-pattern>/Public2</url-pattern>
</servlet-mapping>
</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,
HttpServletResponse response) throws IOException
// business logic goes here
// (read db, write db, etc.)
PrintWriter out = response.getWriter();
// 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 (ModelView-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
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 <servlet-class> in DD
Web Server
Container
Servlet
Who’s Responsible?
Task
Web Server
Container
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
x
Finds URLs in the DD
x
Deletes the request and response
x
Coordinates making dynamic content
Manages lifecycles
Name that matches <servlet-class> in DD
Servlet
x
x
x
x
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