200 likes | 411 Views
How CGI and Java Servlets are Run. By David Stein 14 November 2006. CGI. What is CGI?. Common Gateway Interface server side program or script used to process data entered into an online form commonly referred to as a script. What is needed in order to create and run a CGI program?.
E N D
How CGI and Java Servlets are Run By David Stein 14 November 2006
What is CGI? • Common Gateway Interface • server side program or script used to process data entered into an online form • commonly referred to as a script
What is needed in order to create and run a CGI program? • Computer with: • 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 • 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? • User requests a web page • Web server responds with request • User enters data into an HTML form and sends it back to the web server • HTML form (Get or Post) • Server receives the request from the user and processes the form • Compiled or Interpreted • Server responds with appropriate data to the user
CGI Process HTTP Request Internet HTTP Response Script Processed
Example Form • HTTP request for a webpage • HTTP Request • Request Line • Command, URL (http://cslab103.cs.edinboro.edu/~d301987s/it665/handle_pres_form.html), and HTTP version • Request Header • Variety of optional info (i.e. web browser and date) • Request Body • Information that needs to be sent to the server • Data typed into the form
HTTP Response • Response Status • HTTP version #, server, status code (200 or 400), and a response phrase • Response Header • Variety of optional info (web server used, date, exact URL of page, etc….) • Response Body • Webpage
Processed PHP Script • <html> • <head> • <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> • <title>Your Feedback</title> • </head> • <body> • <?php // handle_php.php • //This page receives the data from feedback.html. • //It will receive: title, name, email, response, comments, and submit • print "Thank you $title $name for your comments. <br />"; • print "You stated that you found this example to be $response and added: $comments"; • ?> • </body> • </html>
What are Java Servlets? • Modules of Java code that run in a server application (similar to an applet on the client side) • Counterpart to dynamic web technologies such as PHP, ASP.Net, etc…
Uses of Java Servlets • Processing/storage of data submitted by an HTML form • Provide dynamic content to a web server • Managing of state information on top of stateless HTTP
Java Servlet Process HTTP Response Internet Java VM (servlet engine) HTTP Request Developer with JSDK Client
Example Java Servlet Code • import java.io.*; • import javax.servlet.*; import javax.servlet.http.*; • public class HelloClientServlet extends HttpServlet • { • protected void doGet(HttpServletRequest req, • HttpServletResponse res) • throws ServletException, IOException • { • res.setContentType("text/html"); • PrintWriter out = res.getWriter(); • out.println("<HTML><HEAD><TITLE>Hello Client!</TITLE>"+ "</HEAD><BODY>Hello Client!</BODY></HTML>"); • out.close(); • } public String getServletInfo() • { • return "HelloClientServlet 1.0 by Stefan Zeiger"; • } • }
Servlet Advantages Over CGI • Servlet does not run in a separate process • (removes overhead of creating a new process) • Servlet stays in memory between requests • Servlet can be run in restrictive Sandbox
Sources • PHP (Visual Quick Start Guide) • By Larry Ullman • http://www.novocode.com/doc/servlet-essentials • http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html • http://java.sun.com/products/servlet/whitepaper.html