Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files...

23
Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents based on form values or other data • Plug-ins: Proprietary extensions of server logic. (ISAPI, NSAPI). Custom code extensions running in Server memory space

Transcript of Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files...

Page 1: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

Server Side Programming

• Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents based on form values or other data

• Plug-ins: Proprietary extensions of server logic. (ISAPI, NSAPI). Custom code extensions running in Server memory space

Page 2: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

• Server-side Includes: Extension to HTML. Server-side scripting. Embedding code in HTML documents.

• Java Server Pages: JavaServer Pages technology uses XML-like tags and scriptlets written in the Java programming language to encapsulate the logic that generates the content for the page. Dynamic data in web page. JSP compiled to Servlet

Page 3: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

HTML Forms

• Interactive HTML• Composed of input elements (buttons,

text fields, check boxes) witn <form> tag

• On Submission, browser packages user input and sends to server

• Server passes information to supporting application that formats reply (HTML page)

Page 4: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<form> Tag

• <form> �</form> comprise single form• Two Special Attributes: Name of form’s

processing server and method to pass parameters to server (Third for security)

• action attribute give URL of application that receives and processes form’s data (cgi-bin)<form action= �http://www.kumquat.com/cgi-bin/update �>�</form>

Page 5: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

• enctype attribute to change encryption• method attribute sets the method by

which data sent to server• POST: Data sent in two steps.

Designed for Posting information.– Browser contacts server– Sends data

• GET: Contacts server and sends data in single step. Appends data to action URL separated by question mark. Designed to get information.

Page 6: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

Other Methods

• HEAD: Client sees only header of response to determine size, etc…

• PUT: Place documents directly on server• DELETE: Opposite of PUT• TRACE: Debugging aid returns to client

contents of its request• OPTIONS: what options available on server

Page 7: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<form method=GET action=“http://www.kumquat.com/cgi-bin/update”>…

</form>

http://www.kumquat.com/cgi-bin/update?x=19&y=104

Page 8: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<HTML><HEAD><TITLE>Introductions</TITLE></HEAD><BODY><FORM METHOD=GET ACTION="/servlet/Hello">If you don't mind me asking, what is your name?<INPUT TYPE=TEXT NAME="name"><P><INPUT TYPE=SUBMIT></FORM></BODY></HTML>

Page 9: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class Hello extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

// Set the Content-Type header res.setContentType("text/html");

// Return early if this is a HEAD if (req.getMethod().equals("HEAD")) return;

// Proceed otherwise PrintWriter out = res.getWriter(); String name = req.getParameter("name"); out.println("<HTML>"); out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>"); out.println("<BODY>"); out.println("Hello, " + name); out.println("</BODY></HTML>"); }

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); }}

Page 10: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<HTML><HEAD><TITLE>Introductions</TITLE></HEAD><BODY><FORM METHOD=GET ACTION="/servlet/Hello">If you don't mind me asking, what is your name?<INPUT TYPE=TEXT NAME="name"><P><INPUT TYPE=SUBMIT></FORM></BODY></HTML>

Page 11: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWorldServlet extends GenericServlet{ public void service(ServletRequest request,

ServletResponse response)throws ServletExcepti on, IOException

{PrintWriter out;

response.setContentType("text/html");

out = response.getWriter();

out.println("<HTML><HEAD><TITLE>");out.println("Hello World");out.println("</TITLE></HEAD><BODY>");out.println("<H1>Hello World </H1>");out.println("</BODY></HTML>");

out.close(); }}

Page 12: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

Book ReviewWhat is your first name?

C h e c k i f y o u a r e p l a n n i n g t o w r i t e a s e r v l e t

W h a t w a s y o u r f a v o r i t e c h a p t e r s o f a r ?

C h p . 1

C h p . 2

Chp. 3C h p . 4

Chp. 5C h p . 6

S ubmit R eset

Page 13: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<html> <head> <title>Book Review</title> </head>

<body> <H1>Book Review</H1> <form action=/cgi-bin/bookreview.pl method=POST> What is your first name? <input type=text name=fname>

<BR><BR>Check if you are planning to write a servlet <input type=checkbox name=writeservlet>

<BR><BR>What was your favorite chapter so far?<BR> <BR>Chp. 1<input type=radio name=favchap value=1> <BR>Chp. 2<input type=radio name=favchap value=2> <BR>Chp. 3<input type=radio name=favchap value=3> <BR>Chp. 4<input type=radio name=favchap value=4> <BR>Chp. 5<input type=radio name=favchap value=5> <BR>Chp. 6<input type=radio name=favchap value=6> <BR><BR><input type=submit><input type=reset> </form> </body></html>

Page 14: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

#!/usr/bin/ perl

require "cgilib.pl";

print "Content-type: text/ html\n\n";

%dataDict = ();

&readData(*data);&parseData(*data,*dataDict);

$fName = $dataDict{"fname"};$chp = $dataDict{"favchap"};$writeServlet = $dataDict{"writeservlet"};

print "<HTML>";print "<TITLE>Book Review Response</TITLE>";print $fName;print ", thank you for your feedback.<BR>";print "Your favorite chapter so far is chapter ";print $chp;print ".<BR>";print "<HR>";print "You are ";

($writeServlet ne "") || print "not ";

print "planning to write a servlet.";

print "</HTML>";

Page 15: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

Comparison

• Servlets work on any servlet-enabled server

• Portable• CGI: Request, open process, shut down• Not persistent. Restablish resources

each time• Each request requires new CGI process• Single process with no IPC

Page 16: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<form action=http://localhost:8080/servlet/BookReviewServlet method=POST>

Page 17: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class BookReviewServlet extends HttpServlet{ // Write a response to the client. public void doPost(HttpServletRequest req, HttpServletResponse res)

thro ws ServletException, IOException { /*

Set the "content type" header of the response so that the browser knows we are sending HTML and not just raw text. If you don't do this then the HTML tags will not be interpreted. Try co mmenting out this line to see what happens.*/res.setContentType("text/html");

//Get the response's PrintWriter to return text to the client.PrintWriter toClient = res.getWriter();

String fName = req.getParameter("fname");String chp = req. getParameter("favchap");String writeServlet = req.getParameter("writeservlet");

// Respond to client with a thank youtoClient.println("<HTML>");toClient.println("<TITLE>Book Review Response</TITLE>");toClient.print(fName);toClient.print ln(", thank you for your feedback.<BR>");toClient.print("Your favorite chapter so far is chapter ");toClient.println(chp + ".<BR>");toClient.println("<HR>");toClient.println("You are ");if(writeServlet == null)

toClient.print("not ");toClient.println("planning to write a servlet.");

toClient.println("</HTML>");

// Close the writer; the response is done.toClient.close();

}}

Page 18: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

HTML Forms

• FORM: container for a form with control elements for user to interact with

• Data is entered from a number of controls

• text boxes, radio buttons, • INPUT: specifies controls as input

elements to FORM• http://webreference.com

Page 19: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

The FORM element is a container for a form. It doesn't actually do anything special by itself, but it can contain form controls that make up the form and are displayed for the user to manipulate.

The FORM element

Context: This is a block element (but see below) Contents: May contain block-level elements and form controls. Cannot be nested. Tags: Both start-tag and end-tag are required.

Attributes for the FORM element

action (URI) The URI that is used for form submission. method (get or post) The method used for form submission, get or post enctype (Content type) The MIME content type that should be used for encoding the form data when it is submitted. The default is application/x-www-form-urlencoded. An alternative is multipart/form-data when sending files (see below). accept (List of content types) A space- or comma-separated list of MIME content types that may be sent (as files) when submitting the form. accept-charset (List of character sets) A space- or comma-separated list of character sets that the form data may be in. The default is the character set of the document. Identifier and classification attributes Language information attributes Inline style information attribute Title attribute Intrinsic event handler attributes

Page 20: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

The INPUT element

Context: Can only appear inside a FORM element Contents: This is an empty element Tags: Empty element; Start-tag is required, end-tag is forbidden

Attributes for the INPUT element

TYPE (Control type) A contol's type can be one of text, password, checkbox, radio, submit, reset, file, hidden, image or button. An explanation of each is given below. NAME (Name) The name that will be used for the name/value pair that the control creates. VALUE (Text) The default value for the control. The precise handling of this value depends on the type of control. SIZE (Integer) In the case of text and password controls, this is the number of characters that will fit into the control. In other cases, it is the default width of the control in pixels. The latter use is discouraged. Both uses can theoretically be replaced by CSS, although this is not possible with all current browsers. MAXLENGTH (Integer) Only applicable to text and password controls, this attribute sets the maximum size of the string that can be entered. By default, there is no limit. If the maximum size is larger than that specified in the SIZE attribute, the browser must use some form of scrolling to allow the user to enter more data than can be seen at one time. CHECKED (Boolean) Only applicable to radio and checkbox controls, this attribtue specifies that the controls should be selected by default. SRC (URI) Only applicable to image controls, this attribute specifies the location of the image to be used in the control. accept (List of content types) Applicable only to file controls. A space- or comma-separated list of MIME content types that may be sent (as files) when submitting the form. This overrides the value of the accept attribute on the form element.

Page 21: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<FORM ACTION="/cgi-bin/html/formdump.cgi" METHOD="GET" ENCTYPE="application/x-www-form-urlencoded"> <P>Enter e-mail: <INPUT TYPE="text" NAME="email" VALUE="[email protected]" SIZE="16" MAXLENGTH="50" > <INPUT TYPE="submit" VALUE="Submit"> </FORM>

<FORM ACTION="/cgi-bin/html/formdump.cgi" METHOD="GET" ENCTYPE="application/x-www-form-urlencoded"> <P>Username: <INPUT TYPE="text" NAME="user" SIZE="8"> <BR>Password: <INPUT TYPE="password" NAME="pass" SIZE="8"> <BR><INPUT TYPE="submit" VALUE="Submit"> </FORM>

Page 22: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<FORM ACTION="/cgi-bin/html/formdump.cgi" METHOD="GET" ENCTYPE="application/x-www-form-urlencoded"> <P>E-mail address: <INPUT TYPE="text" NAME="email" SIZE="20"> <P><INPUT TYPE="checkbox" NAME="use" VALUE="subscribe" CHECKED> Subscribe to our newsletter <BR><INPUT TYPE="checkbox" NAME="use" VALUE="mailing"> Check this box if you do not want to receive e-mails about our products. <BR><INPUT TYPE="submit" VALUE="Submit"> </FORM>

<FORM ACTION="/cgi-bin/html/formdump.cgi" METHOD="GET" ENCTYPE="application/x-www-form-urlencoded"> <P>E-mail address: <INPUT TYPE="text" NAME="email" SIZE="20"> <P>Please select which format you would like to receive our newsletter in: <BR><INPUT TYPE="radio" NAME="format" VALUE="text" CHECKED> Text <BR><INPUT TYPE="radio" NAME="format" VALUE="html"> HTML <BR><INPUT TYPE="radio" NAME="format" VALUE="pdf"> Adobe Acrobat <P><INPUT TYPE="submit" VALUE="Submit"> </FORM>

Page 23: Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.

<FORM METHOD="POST" ACTION="/cgi-bin/html/formdump.cgi" ENCTYPE="multipart/form-data"> <H2>Graphics Contest</H2> <P>Name: <INPUT TYPE="text" NAME="name" SIZE="30"> <P>Please submit your entry in PNG or GIF format: <INPUT TYPE="file" NAME="entry" ACCEPT="image/png, image/gif"> <P><INPUT TYPE="submit"> </FORM>

<FORM ACTION="/cgi-bin/html/formdump.cgi" METHOD="GET" ENCTYPE="application/x-www-form-urlencoded"> <P>Enter your e-mail address: <INPUT TYPE="text" NAME="email" SIZE="30"> <P>Subscribe <INPUT TYPE="radio" NAME="action" VALUE="subscribe"> Unsubscribe <INPUT TYPE="radio" NAME="action" VALUE="unsubscribe" CHECKED> <INPUT TYPE="submit" VALUE="Submit"> </FORM>