Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSCI 6962: Server-side Design and Programming History and Background."— Presentation transcript:

1 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 2

3 3 Client-Server Web Architecture Client Browser www.csis.ysu.edu/ ~john/Syllabus.html Request to www.csis.ysu.edu www.csis.ysu.edu for Syllabus.html Server john  public_html port Response containing Syllabus.htm as a long string ( CSCI 6962 Syllabus …) Syllabus.html

4 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 >http://www.csis.ysu.edu/~john/cgi- bin/test.pl Quantity: /FORM>

5 5 Form Handling Server 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 6 Simple Form Elements The FORM tag … TEXT tag SUBMIT tag

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 7

8 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 form print "Content-type: text/html\n\n"; print " cgi-bin response "; print "Thank you for your order of "; print $formdata{"quantity"}; print " widgets!"; print " ";

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 9

10 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(" "); out.println(" Thank you for your order of " + quantity + " widgets! "); out.println(" "); } finally { out.close(); } Request object containing quantity = 3 Reesponse object containing html

11 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 12

13 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

14 Example Servlet 14

15 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 server somepage.jsp html html Java html Java html html html html Java html html resulting html page html html html

16 16 JSP Syntax Basic tag form: Simplest form: –Glassfish evaluates expression to get value –Inserts that value in place of expression in generated html page

17 17 JSP Simple Example Simple example: Two plus two is. Two plus two is 4. Java Server Page Resulting html Page 2 + 2 evaluated to value of 4

18 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 Two plus two is.

19 19 Scriptlets Two plus two is. Stores value of 4 in sum variable Value of 4 in sum used in this JSP Two plus two is 4. No html here

20 20 Example JSP

21 21 Acquiring Form Data Same syntax as servlets: request.getParameter Key idea: Server Pages implemented as servlet Server Page request Servlet translated to html Run to create response

22 22 Displaying Values in Response 5

23 Limits of Server Pages Sever pages very complex mix of html/Java if conditions or loops needed Simple example: Plural/singular response 23 Html displayed if condition true Html displayed if condition false


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

Similar presentations


Ads by Google