Document 7693539

Download Report

Transcript Document 7693539

World Wide Web
and E-Commerce
Servers & Server Side Processing
Fall 2000
C.Watters
1
Objectives
Understand the server’s role
Understand how processing is executed on
the server for dynamic web page content
CGI scripts
Active server pages
Database access
Servlets
Fall 2000
C.Watters
2
Client-Server Model
Fall 2000
C.Watters
3
HTTP Connection
• 1. Client
– makes an HTTP request for a web page
– makes a TCP/IP connection
• 2. Server accepts request
– Does any processing required
– sends page as HTTP response
• 3. Client downloads page
• 4. Server breaks the connection
Fall 2000
C.Watters
4
Server Side Processing
•
•
•
•
CGI – server applications
Server side scripts
servlets
JDBC/ODBC
• and databases
Fall 2000
C.Watters
5
Servers
• What does a server actually do
• 1.gets up and running in “listening mode”
• creates a socket bound to its IP address & waits
• 2. Handles requests
• detects attempt by client to connect
• creates a new socket for communication with client
• processes request
– retrieve file
– run cgi script
• closes socket
Fall 2000
socket is an abstract
representation of a network endpoint.
C.Watters
6
Dynamic Content
Browser
Server
Province ONT
NS
Get province from form
<html>
<html>
<h1>Catalog
<h1>Catalogfor
for NS</h1>
Ontario <h1>
<ul>
<ul>
<li>BlueRocks
<li>Niagara Falls
<li>Peggys Cove
<li>Bruce Trail
<li>Lunenburg
<li>Goderich
</ul>
</ul>
</html>
</html>
Fall 2000
Query database
Generate tags around the
query results
Send this text as body of
HTTP response
C.Watters
7
CGI & scripts
CGI - Common Gateway Interface
allows data from forms to be used by
programs on the server
script is a program on the server
perl or C or C++ etc.
data returned to client as html document
may interact with a DBMS on server
Fall 2000
C.Watters
8
What does an HTTP response
look like?
Header
object file
Plain text about data
data
• Server response = Header + object file (generally)
Fall 2000
C.Watters
9
Forms and Data
• Forms are part of HTML document
<form action=“http://www.dal.ca/doit.cgi” method = put >
Price: <input type=“text” name=“price”>
<input type=“submit”>
</form>
Submit
Price:
• user enters data or selects options
• Data from form goes to server program called
Fall 2000
C.Watters
10
Form calling Simple CGI Example
<HTML><BODY>hi
Where to find the program?
<FORM action
="http://www.cs.dal.ca/~watters/cgibin/hello2.cgi” method =post>
<Input type = "submit">
</FORM>
</BODY>
</HTML>
Fall 2000
How to get data
to the server
Do it!
C.Watters
11
Script for hello2.cgi
#!/opt/bin/perl
print
print
print
print
print
print
Print
print
Response doc type
"Content-type: text/html\n\n";
"<head>\n";
"<title>Hello</title>\n";
"</head>\n";
"<body>\n";
"<h1>Hello, Ottawa</h1>\n";
“<img src=“cancaphd.gif”>
"</body></html>\n";
HTML
doc
• try it
Fall 2000
C.Watters
12
Getting at Databases
ODBC - Open DataBase Connectivity
JDBC - java based
script programs can use these to make
database queries from server databases
Oracle, access, etc
results are sent back to client as html doc
Fall 2000
C.Watters
13
Perl example to ORACLE
print "<head>\n";
print "</head>\n";
print "<body>\n";
print "<h1>Calendar</h1>\n";
print "<b>Course description enrol</b><br>";
&SetOracle;
&RunSQL("Select * from Calendar");
&StopOracle;
print "</body></html>\n";
Fall 2000
C.Watters
14
BEING STATELESS!!
• Each http call is a new connection
• SO??
– A new version of your script is run each time
– No memory of previous events
– pretty hard to implement a shopping cart when
it gets emptied everytime you contact the
server!
Fall 2000
C.Watters
15
Example
Client puts tshirt in
shopping cart form on
web page
Cart.cgi is called
Makes purchase
(Tshirt)
Generates new page &form
Add a book
Cart.cgi is called
Makes purchase
( book)
Fall 2000
C.Watters
16
Maintaining State
in a Stateless Systems
Use your script to write data to a temporary file on
the server and to start each time by reading any
data in that file
use javascript to process activities in a given
session and send results to server at end only
Hide data in forms and send it back and forth
keep everything in a database
Use Servlets
gets
tricky!!
Fall 2000
C.Watters
17
Servlets
java applications that are part of the server
always available (faster) than cgi scripts
provide continuity (state) for clients
written in java
can use JDBC to access databases
Used for: shopping, search, certificates,
agents, portals
Fall 2000
C.Watters
18
Other ways to process on Server
to generate dynamic data for web pages
Server side includes
Active server pages
Java server pages
Fall 2000
C.Watters
19
Server Side Include Files
• Server Side Includes (.shtml)
• Active Server Pages (.asp)
• Java Server Pages
(.jsp)
These functions are performed before
any data are sent to the browser
Fall 2000
C.Watters
20
Server Side Includes (SSI)
• Shtml extension
• What happens:
– Server gets request for a page (.shtml)
– Server checks the page for SSI commands
– Server executes those commands and inserts
new values into the page
– Server sends the new page to the Browser
Fall 2000
C.Watters
21
SSI example: ssitest.shtml
<html>
<h1>Getting the date from the server as I need it</h1>
<!--#echo var=“DATE_LOCAL” -->
<h2> I could do other things as well</h2>
<ul><li>a stock quote
<li>an expected wait time
<li>a price check
<li>etc
</ul>
</html>
Fall 2000
C.Watters
22
Server Pages
• Active Server Pages (microsoft) .asp
• Java Server Pages (Sun) .jsp
• Instructions for the server are included in
the web page
• The server notices the extension and looks
for those instructions and executes them!
Fall 2000
C.Watters
23
Active server Page example:test1.asp
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<BODY>
Test1
<% If Time >= #12:00:00 AM# and Time <=#12:00:00 PM# then %>
<h3>Good Morning Ottawa</h3>
<% else %>
<H3>Hello Everyone</H3>
<% end If %>
</BODY>
</HTML>
Fall 2000
C.Watters
24
JSP (Java server pages)
Fall 2000
C.Watters
25
• <UL>
• <LI><B>Expression.</B><BR>
•
Your hostname: <%= request.getRemoteHost() %>.
• <LI><B>Scriptlet.</B><BR>
•
<% out.println("Attached GET data: " +
•
request.getQueryString()); %>
• <LI><B>Declaration (plus expression).</B><BR>
•
<%! private int accessCount = 0; %>
•
Accesses to page since server reboot: <%=
++accessCount %>
• <LI><B>Directive (plus expression).</B><BR>
•
<%@ page import = "java.util.*" %>
•
Current date: <%= new Date() %>
Fall 2000
C.Watters
26
• </UL>
Fall 2000
C.Watters
27
Fall 2000
C.Watters
28
<%@ page import="hello.NameHandler" %>
<jsp:useBean id="mybean" scope="page" class="hello.NameHandler" />
<jsp:setProperty name="mybean" property="*" />
<html>
<head><title>Hello, User</title></head>
<body bgcolor="#ffffff" background="background.gif">
<%@ include file="dukebanner.html" %>
<form method="get">
<input type="text" name="username" size="25"><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
<% if ( request.getParameter("username") != null ) {%>
<%@ include file="response.jsp" %>
<% } %>
</body>
</html>
Fall 2000
C.Watters
29
NOT the same as Active X
• Active X Components are “roughly” the
same as applets
Fall 2000
C.Watters
30
Recap
• Servers process browser requests
• CGI can be used to run applications on the server from
web pages including accessing DBMS data
• Servers are naturally stateless
• Servlets (&other tricks)can be used to maintain state
• Server side includes can be used to dynamically create
web page content before sending the page to the
browser
• Agents are little independent pieces of software
Fall 2000
C.Watters
31