functionality of a web server what does the web server do? let a user request a resource find the...
Post on 29-Dec-2015
215 Views
Preview:
TRANSCRIPT
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)
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)
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
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
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.
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)
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
HTML can be part of the HTTP response
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
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
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
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
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
Anatomy of an HTTP GET request
Request headers
Request line
HTTP method Path of resource Protocol version
Anatomy of an HTTP GET requestAnother example with request parameters
HTTP GET request parameters
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
Anatomy of an HTTP POST requestRequest line
Request headers
Message body
Path of resource Protocol version
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
Anatomy of an HTTP responseText version of the status code
http status codeProtocol ver
Response header
Response body
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
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
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
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
Well-known TCP port numbersFTP: 21Telnet: 23SMTP: 25HTTPS: 443POP3: 110HTTP: 80Time: 37
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?
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
Mapping URLs to contenthttp://www.wickedlysmart.com will cause the server to return to you index.html at location A
Mapping URLs to contentWhat url will cause the server to return to you index.html at location B?
Mapping URLs to contentWhat url will cause the server to return to you index.html at location C?
Mapping URLs to contenthttp://www.wickedlysmart.com will cause the server to return to you index.html at location A
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
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
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
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
CGI
CGI
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
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
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)
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>");
}}
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>
Servlet DemystifiedBuild this directory under the existing tomcat
directory…
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.
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)
Servlet DemystifiedLaunch your browser and type in http://localhost:8080/ch1/Serv1
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)
Disadvantage of using servlet
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
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
top related