csci 6962: server-side design and programming history and background
TRANSCRIPT
2
Overview
Review of client/server architecture
Previous approaches to server-side programming• CGI-BIN Perl programming• Web containers• Java servlets• Java Server Pages
Limits to previous approaches
3
Client-Server Web Architecture
ClientBrowserwww.csis.ysu.edu/~john/Syllabus.html
Request to www.csis.ysu.edu for Syllabus.html
Server
john public_html
port
Response containing Syllabus.htm as a long string (<html><head><title>CSCI 6962 Syllabus</title> </head><body>…)
Syllabus.html
4
Dynamic Form Handling
• Form data appended to request string
Generates the request:http://www.cis.ysu.edu/~john/cgi-bin/test.pl&quantity=3
<FORM NAME="purchaseform" METHOD=GET ACTION=http://www.csis.ysu.edu/~john/cgi-bin/test.pl >
Quantity: <INPUT TYPE="text" NAME="quantity" /><BR /><BR />
<INPUT TYPE="submit" VALUE="SUBMIT">
/FORM>
5
Form HandlingServer must:
– Listen on port for requests– Parse request to determine values of parameters– Dynamically generate appropriate response page based on
parameter values– Send response page back to client
6
Simple Form Elements• The FORM tag<form action=”url of response page” method=”get or post”>…</form>
• TEXT tag<input type=“text” name = “elementname” />
• SUBMIT tag<input type=”submit” value=”buttonlabel”/>
7
Perl/CGI-Bin
• Request string manually parsed by code– Perl used because has built-in parsing procedures– String split on “&” and then on “=“
• Response string manually generated – Based on data retrieved from request– Entire web page printed to response one html tag
at a time
Simple perl cgi-bin Program#!/opt/local/bin/perl#program to print back results of test form#parse input string into an associative list@pairs=split(/&/, $ENV{'QUERY_STRING'});foreach $pair (@pairs) { @item=split(/=/, $pair); $key=@item[0]; $value=@item[1]; $formdata{$key}=$value; }#print response to formprint "Content-type: text/html\n\n";print "<HTML><HEAD><TITLE>cgi-bin response</TITLE><BODY>";print "Thank you for your order of ";print $formdata{"quantity"};print " widgets!";print "</BODY></HTML>";
9
Java Servlets
• Java classes designed for server-side programming• Constructed for and run by web container when
request received– doGet and doPost methods called by web
container– request object contains form data constructed
from request by web container– request object built by servlet contains html for
response page
10
Servlets and Web Containers
Client
Browser
Web Container
Port
http://homer.cis.ysu.edu/reciept.jsp&quantity=3
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String quantity = request.getParameter("quantity"); try { out.println("<!DOCTYPE html>"); out.println("<html><head></head>"); out.println("<body>"); out.println("<p>Thank you for your order of " + quantity + " widgets!</p>"); out.println("</body></html>"); } finally { out.close(); } }
Request objectcontaining quantity = 3
Reesponse objectcontaining
html
11
Servlet Request Object
• Java object created from request string• Contains request data and methods to access that data
• Most useful method:String request.getParameter(String)
Data from form methods to access data
Servlet code
request
Takes name of form element as parameter
Returns the corresponding value passed to the server
12
Servlet Response Object
Usual steps:• PrintWriter out = request.getWriter
gets link to response object• out.println(html)
writes text to response object
Basic Servlet Structure
Key methods:• void doGet(HttpServletRequest request, HttpServletResponse response)Called if servlet invoked using get method
• void doPost(HttpServletRequest request, HttpServletResponse response) Called if servlet invoked using post method
• Have access to request object
15
Java Server Pages
• Html document with executable code interspersed• When page requested:
– Code executed– Html generated and inserted in its place– Final all html document sent back as response
request for somepage.jsp
Glassfish serversomepage.jsp
html html Javahtml Java htmlhtml html htmlJava html html
resulting html page
html html htmlhtml html htmlhtml html htmlhtml html html
16
JSP Syntax
• Basic tag form: <% … %>
• Simplest form:<%= some Java expression %>
– Glassfish evaluates expression to get value– Inserts that value in place of expression in generated html
page
17
JSP Simple Example• Simple example:
<html><body><p>Two plus two is <%= 2 + 2 %>.</p></body></html>
<html><body><p>Two plus two is 4.</p></body></html>
Java Server Page
Resulting html Page
2 + 2 evaluated to value of 4
18
Scriptlets• Basic tag form: <% … %>• Executes code inside brackets without generating html
– Set variables later used to generate html later– Store/access values in session/databases
<html><body><% int sum = 2 + 2; %><p>Two plus two is <%= sum %>.</p></body></html>
19
Scriptlets<html><body><% int sum = 2 + 2; %><p>Two plus two is <%= sum %>.</p></body></html>
Stores value of 4 in sum variable
Value of 4 in sum used in this JSP
<html><body><p>Two plus two is 4.</p></body></html>
No html here
21
Acquiring Form Data
• Same syntax as servlets: request.getParameter
• Key idea: Server Pages implemented as servlet
ServerPage
requestServlet
translated to
html
Run tocreate
response