CS122B - Chen Li -- Application Server

Download Report

Transcript CS122B - Chen Li -- Application Server

CS122B: Projects in Databases and Web
Applications
Winter 2017
Notes 03: Web-App Architectures
Professor Chen Li
Department of Computer Science
CS122B
1
Outline
•
•
•
•
•
Web applications: overview
CGI and its variants
Server Extension APIs
Java servlets
Comparisons
CS122B
2
Web Applications
• Create dynamic content for a web
page
– keyword search on a document archive
– E-commerce
–…
• Example:
– Amazon.com
– Google.com
CS122B
3
CGI
• CGI (Common Gateway Interface)
– One of the first techniques for creating
dynamic HTML pages
• Goal:
– Define a standard method for a server to talk
with external applications
• Example:
http://www.tigernt.com/cgi-bin/ecdict.cgi
CS122B
4
CGI life cycle
CGI-based Web Server
e.g., apache
Main Process
Request for
CGI 1
Child Process
for CGI1
Request for
CGI 2
Child Process
for CGI2
Request for
CGI 1
Child Process
for CGI1
CS122B
5
CGI Life Cycle (cont)
• Server receives a request for a CGI
program
• Server creates a new process to run the
CGI program
• Server passes information to the program:
– via environment variables and standard input.
CS122B
6
Processes are expensive
• Comparison between processes and threads
http://www.cs.berkeley.edu/~istoica/classes/
cs194/05/notes/4-ProcThread.pdf
CS122B
7
Problems with CGI
• Expensive to create a process for each
request:
– requires time and significant server resources,
– limits the # of requests a server can handle
concurrently
• Stateless: No history remembered
–
–
–
–
“Memento” 
A big problem for web-based development
Solutions: cookies
Still expensive
CS122B
8
Problems with CGI (cont)
• Once the CGI program starts:
– It cannot interact with the web server
– It cannot take advantage of the server's
abilities once it begins execution
• Reasons: the program is running in a
separate process.
• Example, a CGI script cannot write to the
server's log file.
CS122B
9
Ways to improve cgi performance
• FastCGI
• Other solutions
CS122B
10
FastCGI
CGI-based Web Server
Main Process
Request for
CGI 1
Single Child Process
for CGI1
Request for
CGI 2
Child Process
for CGI2
Request for
CGI 1
http://www.rebol.com/docs/fastcgi.html
CS122B
11
FastCGI (cont)
• Developed by a company “Open Market”
• FastCGI creates a single persistent process
for each FastCGI program
• It eliminates the need to create a new
process for each request.
CS122B
12
FastCGI: /
•  No need to start multiple processes for different requests
for the same fastcgi program
•  Still needs one process for each cgi program.
– Thus not scalable to handle many concurrent requests
•  It does nothing to help the FastCGI program more closely
interact with the server.
– Not implemented by some of the more popular servers, I.e.,
Microsoft's Internet Information Server.
•  Not very portable
CS122B
13
Other solutions
• Proprietary solutions that work only with
certain web servers.
• Platform dependent
• Examples: ASP (Microsoft)
CS122B
14
Server Extension APIs life cycle
Web Server with
Server Extension API
Main Process
Request for
Server Extension 1
Server Extension 1
Request for
Server Extension 2
Server Extension 2
Request for
Server Extension 1
CS122B
15
Server Extension APIs 
• Write server extensions that enhance or
change the base functionality of the server
• Allowing the server to handle tasks that
were once relegated to external CGI
programs.
• Use linked C or C++ code
– Thus can run extremely fast; and
– make full use of the server's resources.
CS122B
16
Server Extension APIs: 
• Difficult to develop and maintain
• Pose significant security and reliability
hazards
– a crashed server extension can bring down the
entire server.
• Proprietary server extensions are tied to
the server API on a particular OS
– Not very portable
CS122B
17
Active Server Pages (ASP)
• Developed by Microsoft
• With ASP, an HTML page on the web server can
contain snippets of embedded code:
– usually VBScript or Jscript
– it's possible to use nearly any language
• This code is read and executed by the web
server before it sends the page to the client.
• ASP is optimized for generating small portions of
dynamic content.
• Support for ASP is built into Microsoft Internet
Information Server Version 3.0 and above
CS122B
18
ASP example
• http://www.w3schools.com/asp/showasp.asp?filename=demo_text
• http://www.w3schools.com/asp/showasp.asp?filename=demo_format
ting
<html>
<body>
<%
response.write("Hello World!")
%>
</body>
</html>
The browser shows:
Hello World!
CS122B
19
Java Servlet Based Web Server
Web Server
Main Process
Request for
Servlet 1
Request for
Servlet 2
Request for
Servlet 1
JVM
Threa
d
Servlet1
Threa
d
Threa
d
Servlet2
A Servlet is a Service Unit
CS122B
20
Life With Servlets
A servlet is a service unit which runs inside a
web server, extending its functionality beyond
the basic GET, POST, DELETE and PUT
operations.
CS122B
21
Inside a Servlet
• A servlet does not have a main() method.
– a Java program: yes
– an applet: no
• Certain methods of a servlet are invoked by the
server in the process of handling requests.
• Each time the server dispatches a request to a
servlet, it invokes the servlet's service() method.
CS122B
22
Servlet Package Hierarchy
JAVAX
Servlet
GenericServlet
HttpServlet
Form
Servlet
CS122B
Admin
Servlet
CGI
Servlet
Error
Servlet
File
Servlet
Image
Servlet
23
The Servlet Interface
Servlet interface
{
void init(ServletConfig sc)
throws ServletException;
void service(ServletRequest req,
ServletResponse res);
throws ServletException,
IOException;
void destroy();
}
CS122B
24
GenericServlet Class
http://java.sun.com/j2ee/1.4/docs/api/javax/servl
et/GenericServlet.html
Server
GenericServlet subclass
request
response
service( )
Override the service() method
CS122B
25
HttpServlet Class
http://java.sun.com/j2ee/1.4/docs/api/javax/servl
et/http/HttpServlet.html
java.lang.Object  javax.servlet.GenericServlet
 javax.servlet.http.HttpServlet
A function:
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
CS122B
26
Request and Response Objects
They form the core objects for communication.
HttpServletRequest contains all the details
about the client request to the servlet.
For e.g., request.getParameter(”paramName”);
HttpServletResponse contains all the
information for replying back to the client. For
e.g., the output stream.
CS122B
27
Comparisons
CGI/FastCGI:
use multiple processes to handle separate programs
and/or separate requests
Outside the server
Servlets:
handled by separate threads within the web server
process.
Thus more efficient and scalable.
can interact very closely with the server to do things
that are not possible with CGI scripts.
CS122B
28
Comparisons (cont)
Java Servlets are portable:
across operating systems (java)
across web servers, since all major web servers
support servlets.
Thus, Java servlets offer a good platform for
web-application development
CS122B
29