How CGI and Java Servlets are Run By

How CGI and Java Servlets are Run By David Stein 14 November 2006

CGI

What is CGI? l Common Gateway Interface l server side program or script used to process data entered into an online form l commonly referred to as a script

What is needed in order to create and run a CGI program? l Computer with: 1. 2. 3. l l Internet access Web browser Text editor Access to a web server Knowledge of HTML and a programming/scripting language

What languages are associated with CGI l Any programming language that can be run or executed in a server environment – – – – C C++ Visual Basic PHP Perl Java Etc…

How is a CGI program run? 1. User requests a web page 2. Web server responds with request 3. User enters data into an HTML form and sends it back to the web server • Server receives the request from the user and processes the form 4. • 5. HTML form (Get or Post) Compiled or Interpreted Server responds with appropriate data to the user

CGI Process HTTP Request Internet HTTP Response Script Processed

Example Form l l HTTP request for a webpage HTTP Request – Request Line l – Request Header l – Command, URL (http: //cslab 103. cs. edinboro. edu/~d 301987 s/it 665/handle_pres _form. html), and HTTP version Variety of optional info (i. e. web browser and date) Request Body l Information that needs to be sent to the server – Data typed into the form

HTTP Response l Response Status – l Response Header – l HTTP version #, server, status code (200 or 400), and a response phrase Variety of optional info (web server used, date, exact URL of page, etc…. ) Response Body – Webpage

Processed PHP Script l <html> l l <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Your Feedback</title> </head> l <body> l <? php // handle_php. php l l //This page receives the data from feedback. html. //It will receive: title, name, email, response, comments, and submit l print "Thank you $title $name for your comments. "; l print "You stated that you found this example to be $response and added: $comments"; l ? > l </body> l </html> l l

Java Servlets

What are Java Servlets? l Modules of Java code that run in a server application (similar to an applet on the client side) l Counterpart to dynamic web technologies such as PHP, ASP. Net, etc…

Uses of Java Servlets l Processing/storage of data submitted by an HTML form l Provide dynamic content to a web server l Managing of state information on top of stateless HTTP

Java Servlet Process HTTP Response Java VM (servlet engine) Internet HTTP Request Developer with JSDK Client

Example Java Servlet Code l l l l l import java. io. *; import javax. servlet. http. *; public class Hello. Client. Servlet extends Http. Servlet { protected void do. Get(Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException { res. set. Content. Type("text/html"); Print. Writer out = res. get. Writer(); out. println("<HTML><HEAD><TITLE>Hello Client!</TITLE>"+ "</HEAD><BODY>Hello Client!</BODY></HTML>"); out. close(); } public String get. Servlet. Info() { return "Hello. Client. Servlet 1. 0 by Stefan Zeiger"; } }

Servlet Output

Servlet Advantages Over CGI l Servlet does not run in a separate process – (removes overhead of creating a new process) l Servlet stays in memory between requests l Servlet can be run in restrictive Sandbox


Sources l PHP (Visual Quick Start Guide) – l l l By Larry Ullman http: //www. novocode. com/doc/servletessentials http: //java. sun. com/j 2 ee/tutorial/1_3 fcs/doc/Servlets. html http: //java. sun. com/products/servlet/whitepap er. html
- Slides: 19