Top Banner
CSCI 6962: Server-side Design and Programming History and Background
23

CSCI 6962: Server-side Design and Programming History and Background.

Dec 23, 2015

Download

Documents

Karen Dennis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCI 6962: Server-side Design and Programming History and Background.

CSCI 6962: Server-side Design and Programming

History and Background

Page 2: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 3: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 4: CSCI 6962: Server-side Design and Programming History and Background.

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>

Page 5: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 6: CSCI 6962: Server-side Design and Programming History and Background.

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”/>

Page 7: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 8: CSCI 6962: Server-side Design and Programming History and Background.

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>";

Page 9: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 10: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 11: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 12: CSCI 6962: Server-side Design and Programming History and Background.

12

Servlet Response Object

Usual steps:• PrintWriter out = request.getWriter

gets link to response object• out.println(html)

writes text to response object

Page 13: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 14: CSCI 6962: Server-side Design and Programming History and Background.

14

Example Servlet

Page 15: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 16: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 17: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 18: CSCI 6962: Server-side Design and Programming History and Background.

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>

Page 19: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 20: CSCI 6962: Server-side Design and Programming History and Background.

20

Example JSP

Page 21: CSCI 6962: Server-side Design and Programming History and Background.

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

Page 22: CSCI 6962: Server-side Design and Programming History and Background.

22

Displaying Values in Response

5

Page 23: CSCI 6962: Server-side Design and Programming History and Background.

23

Limits of Server Pages

• Sever pages very complex mix of html/Java if conditions or loops needed

• Simple example: Plural/singular response

Html displayed if condition trueHtml displayed if

condition false