functionality of a web server what does the web server do? let a user request a resource find the...

51

Upload: earl-dalton

Post on 29-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can
Page 2: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Functionality of a web serverWhat does the web server do?

Let a user request a resourceFind the resourceReturn something to the user

The resource can be different things, such asAn HTML pageA pictureA PDF documentData (xml, json, plain/text)

Page 3: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Functionality of a web serverIf the requested resource is not there, you

will get an error “404 Not Found” error in the browser

In the context of this book, when we say “server”, we mean either the physical machine (hardware) or the web server application (software)

Page 4: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Functionality of a web clientWhen we talk about clients, we usually mean

both (or either) the human user and browser application

The browser is the piece of software that knows how to render HTML pages

Page 5: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

How do the clients and servers talk to each other?The clients and servers speak HTTP

HTTP stands for Hyper Text Transfer ProtocolHTTP is the protocol clients and servers use on

the web to communicateThe client sends an HTTP request, and the

server answers with an HTTP response.

The browsers must know HTMLHTML stands for HyperText Markup LanguageHTML tells the browser how to display the

content to the user

Page 6: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

HTMLWhen you develop a web page, you use

HTML to describe what the page should look like and how it should behave

The goal of HTML is to take a text document and add tags that tell the browser how to format the text.

Page 7: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

What is HTTP HTTP runs on top of TCP/IP.

TCP stands for Transmission Control ProtocolIt is a connection-oriented, end-to-end reliable

protocol for message transmission on the network

TCP is responsible for making sure that a file sent from one network node to another ends up as a complete file at the destination

IP stands for Internet Protocol (protocol used for communicating data across a packet-switched internetwork)

Page 8: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

HTML can be part of the HTTP responseAn HTTP response can contain HTMLHTTP adds header information to the top of

whatever content is in the responseAn HTML browser uses that header info to

help process the HTML page

Page 9: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

HTML can be part of the HTTP response

Page 10: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

What is in the HTTP requestThe first thing you’ll find is an HTTP method

nameHTTP protocols has several methods, the

ones you’ll use most often areGETPOST

Page 11: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

HTTP GET

User

User clicks a link to a new page

Browser

Browser sends out an HTTP GET to the server, asking the server to get the page

Server

Page 12: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

HTTP POST

User

User types in a form and hits the Submit button

Browser

Browser sends out an HTTP POST to the server, giving the server what the user typed into the form

Server

Page 13: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

What is in the HTTP requestThe main job of HTTP GET is to ask the

server to get a resource such as an HTML page, a JPEG, a PDF, etc.

The main job of HTTP POST is to request something and at the same time send form data to the server

Page 14: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

What is in the HTTP requestThat does not mean HTTP GET cannot be

used to send dataThe data you send with HTTP GET is

appended to the URL up in the browser barSo whatever you send is exposed

You can even use HTTP GET to send form data to the web serverDoing this cause any form data to be exposed

in the browser bar, that is why people usually do not do this

Page 15: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP GET request

Request headers

Request line

HTTP method Path of resource Protocol version

Page 16: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP GET requestAnother example with request parameters

HTTP GET request parameters

Page 17: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP GET requestIn a GET request

Parameters (if there are any) are appended to the first part of the request URL, Starting with a “?”.

Parameters are separated with an ampersand “&”

E.g., GET /select/selectBeerTaste.jsp?color=dark&taste=malty HTTP/1.1

Page 18: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP POST requestRequest line

Request headers

Message body

Path of resource Protocol version

Page 19: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP POST requestHTTP POST requests are designed to be used

by the browser to make complex requests on the serverFor example, it can be used to send all of the

form data use completed to the web server and then added the data to a database

The data sent back to the server is known as the “message body” or “payload”

The data can be quite large

Page 20: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP responseText version of the status code

http status codeProtocol ver

Response header

Response body

Page 21: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP responseAn HTTP response has both a header and a

body. The header info tells the browser about the

protocol being usedWhether the request was successfulWhat kind of content is included in the bodyThe body contains the contents (e.g., HTML)

for the browser to display

Page 22: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Anatomy of an HTTP responseThe Content-Type response header’s value is

known as a MIME type.The MIME type tells the browser what kind of

data the browser is about to receive so that the browser will know how to render it

Notice that the MIME type value relates to the value listed in the HTTP request’s “Accept” header MIME stands for Multipurpose Internet Mail

Extensions

Page 23: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

URLURL stands for Uniform Resource LocatorEvery resource on the web has its own unique

address, in the URL format

http://www.wickedlysmart.com:80/beeradvice/select/beer1.html

ProtocolSever name

Port

Path Resource

If not specified, default to index.htmlif not specified,

then port 80 is the default

Page 24: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

TCP portA TCP port is just a number

A port represents a logical connection to a particular piece of software running on the server hardware A TCP port can be any number from 0-65535

A port does not represent a place to plug in some physical device, it is just a number representing a server application

The TCP port numbers from 0 to 1023 are reserved for well-known services

Page 25: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Well-known TCP port numbersFTP: 21Telnet: 23SMTP: 25HTTPS: 443POP3: 110HTTP: 80Time: 37

Page 26: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Directory structure for a simple Apache web siteApache is a popular open source web serverSuppose we have a web site

www.wickedlysmart.com running on Apache It hosts two applications

One giving skiing advice One giving beer-related advice

What would the directory structure look like for this web site?

Page 27: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Directory structure for a simple Apache web site Apache

Home

htdocs

skiingAdvice

select

beerAdvice

selectcheckou

t

<html>...

</html>

<html>...

</html>

<html>...

</html> <html>...

</html>

Index.html

Index.html

selectBeer.html

Index.html

htdocs is the dir that is the root for all of the web applicationsIndex.html is the default

page that will be returned to a user who keys www.wickedlysmart.com

Index.html is the default page for the beerAdvice application

The root folder for the skiingAdvice application

The root folder for the beerAdvice application

Index.html is the default page for the skiingAdvice application

An HTML page that gives the user some advice

A

B C

D

Page 28: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Mapping URLs to contenthttp://www.wickedlysmart.com will cause the server to return to you index.html at location A

Page 29: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Mapping URLs to contentWhat url will cause the server to return to you index.html at location B?

Page 30: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Mapping URLs to contentWhat url will cause the server to return to you index.html at location C?

Page 31: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Mapping URLs to contenthttp://www.wickedlysmart.com will cause the server to return to you index.html at location A

Page 32: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Web server loves serving static web pagesWeb server sends back the page the client ask for with

added HTTP header info without doing any change or computation on the page If the client want a dynamic web page such as showing the

time on the web server, the server cannot do that

<html>

<body>

The current time is [insertTimeOnServer]

</body>

</html>

Web server cannot insert the time on the html page directly

Page 33: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Helper applicationSo a helper application is needed to generate the dynamic

content.

Web server

Another application on server

Client

Web server sends the request to the helper application (to generate

dynamic content), then take the app’s response and send it back to

the client.

In fact, the client never needs to know that someone else did some of the work

Page 34: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Two things the web server alone won’t doDynamic Content

A separate “helper” application that the web server can communicate with can build non-static, just-in-time pages

Saving data on the serverWhen the user submits data in a form, in order to

process form data, you need a help applicationThe helper application can either save data to a

database or use the data to generate the response page

Page 35: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

CGINon-java term for a web server helper

application is “CGI” programMost CGI programs are written as Perl

scripts, but many other languages can be used includingC, Python, and PHP

Page 36: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

CGI

Page 37: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

CGI

Page 38: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Differences between Servlets and CGIServlets have better performance in serving

client requestsClient requests for a Servlet resource are

handled as separate threads of a single running Servlet

With CGI, the server has to launch a heavy-weight process for each and every request for that resource

Page 39: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet DemystifiedLet us use a simple example to show how to

write, deploy, and run a servlet that generates a HTML page that displays the current date and time of the server

Build this directory tree

Page 40: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet DemystifiedWrite a servlet named Ch1Servlet.java and

put it in the src directory. Alternatively, you may download the servlet code from the blackboard (lab assignment section for this class)

Page 41: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet Demystifiedimport javax.servlet.*;import javax.servlet.http.*;import java.io.*;public class Ch1Servlet extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException {

PrintWriter out = response.getWriter();java.util.Date today = new java.util.Date();out.println("<html> " +

"<body>"+ "<h1 align=center>HF\'s Chapter1

Servlet</h1>" + "<br>"+today+"</body>"+"</html>");

}}

Page 42: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Create a deployment descriptor (DD)<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-

instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5"> <servlet> <servlet-name>Chapter_1_Servlet</servlet-

name> <servlet-class>Ch1Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Chapter_1_Servlet</servlet-

name> <url-pattern>/Serv1</url-pattern> </servlet-mapping> </web-app>

Page 43: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet DemystifiedBuild this directory under the existing tomcat

directory…

Page 44: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet Demystified From the project1 directory, compile the servlet

javac -classpath \tomcat_dir\lib\servlet-api.jar -d classes src\Ch1Servlet.java

Please be noted that in the book, it uses a different version of tomcat, thus it uses a different classpath as follows:

javac -classpath /your path/tomcat/common/lib/servlet-api.jar -d classes src/Ch1Servlet.java (do not use this command line)

In our installation of version 6.0.18 of Tomcat, there is no sub-dir common under tomcat dir. Thus, if you use the command in the book as it, it will prompt a compilation error.

Page 45: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet DemystifiedCopy the Ch1Servlet.class file to

WEB-INF/classes, and copy the web.xml file to WEB-INF.

From the tomcat’s bin directory, start Tomcat C:\apache-tomcat-6.0.18\bin>startup or # ./startup.sh (for linux)

Page 46: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet DemystifiedLaunch your browser and type in http://localhost:8080/ch1/Serv1

Page 47: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Servlet DemystifiedFrom now, every time you update either a

servlet class or the deployment descriptor, shutdown tomcat and then restart it.

C:\apache-tomcat-6.0.18\bin>shutdown or # ./shutdown.sh (for linux)

Page 48: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Disadvantage of using servlet

Page 49: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Disadvantage of using servletQuestion:

Why can’t I just copy a whole page of HTML from my web page editor, like Microsoft Front Page, Dreamweaver, and paste it into the println()?

Answer:You cannot have a carriage return (a real one)

inside a String literal. Simply copy a whole page of HTML into println() will cause compilation errors.

Quotes in HTML page can be a problem too

Page 50: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can
Page 51: Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can

Overview of JSP A JSP page looks like an HTML page, except you

can put Java and Java-related things inside the page

So it really like inserting a variable into your HTML Putting Java into HTML is a solution for two

issues:1. Not all HTML page designers know Java2. It is difficult to format HTML into a string literal in

servlet With JSP, Java Developers can do Java, and

HTML developers can do web pages