<!DOCTYPE html><html><head><title>This is a title</title></head><body><p>Hello world!</p></body></html>
CGI
Common Gateway Interface
Forward requests to external programs
Spawn one process for each new request
Communicate via Standard input/output
Process terminates after request is handled
CGI Example
#!perl -w use CGI qw/:standard/;
{
my $cgi= new CGI;
print $cgi->header,
$cgi->start_html('A Simple Example'),
$cgi->h1('A Simple Example');
if ( $cgi->param())
{
print"Your name is ",
$cgi->param('name');
}
$cgi->end_html();
}
Active Pages
<html><head><title>First JSP</title></head><body>
<%
double num = Math.random();
if (num > 0.95) {
%>
<h2>You will have a luck day!</h2><p>(<%= num %>)</p>
<% } else { %>
<h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
<% } %>
<ahref="<%= request.getRequestURI() %>"><h3>Try Again</h3></a></body></html>
Servlet
Containing application-specific code
Web server does generic part of request handling
Servlets run “in” the web server and do some of the handling
Servlet
Advantages:
Running a servlet doesn’t require creating a separate process each time and one instance can handle multiple requests, not a separate instance for every request
It stays in memory and doesn’t have to be reloaded each time
Untrusted servlets can be run in a “sandbox”
Disadvantage:
Less choice of languages (CGI scripts can be in any language)
Servlet Example
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
publicclassHelloWorldServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter( );
out.println("<html>");
out.println( "<head><title>Have you seen this before?</title></head>");
out.println( "<body><h1>Hello, World!</h1><h6>Again.</h6></body></html>");
}
}