Download presentation
Presentation is loading. Please wait.
Published byLina Johannessen Modified over 5 years ago
1
CS122B: Projects in Databases and Web Applications Winter 2019
Notes 06: Web-App Architectures Professor Chen Li Department of Computer Science
2
Outline Web applications: overview CGI and its variants
Server Extension APIs Java servlets Comparisons
3
Web Applications Create dynamic content for a web page Example:
keyword search on a document archive E-commerce … Example: Amazon.com Google.com
4
CGI CGI (Common Gateway Interface) Goal: Example:
One of the first techniques for creating dynamic HTML pages Goal: Define a standard method for a server to talk with external applications Example:
5
CGI life cycle Main Process CGI-based Web Server e.g., apache
Request for CGI 1 Child Process for CGI1 Request for CGI 2 Child Process for CGI2 Request for CGI 1 Child Process for CGI1
6
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.
7
Processes vs Thread Process Thread Typically independent
Has considerably more state info than thread Separate address spaces Interact only through system inter-process communication (IPC) Thread Subsets of a process Multiple threads within a process share process state, memory, etc. Threads share their address space Process is very expensive from the perspective of the OS!
8
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
9
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.
10
Ways to improve cgi performance
FastCGI Other solutions
11
FastCGI Main Process CGI-based Web Server Request for CGI 1
Single Child Process for CGI1 Request for CGI 2 Child Process for CGI2 Request for CGI 1
12
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.
13
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
14
Java Servlet Based Web Server
Main Process Web Server JVM Request for Servlet 1 Thread Servlet1 Thread Request for Servlet 2 Thread Servlet2 Request for Servlet 1 A Servlet is a Service Unit
15
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.
16
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.
17
Servlet Package Hierarchy
JAVAX Servlet GenericServlet HttpServlet Form Servlet Admin Servlet CGI Servlet Error Servlet File Servlet Image Servlet
18
The Servlet Interface Servlet interface { void init(ServletConfig sc)
throws ServletException; void service(ServletRequest req, ServletResponse res); throws ServletException, IOException; void destroy(); }
19
Override the service() method
GenericServlet Class et/GenericServlet.html Server GenericServlet subclass request service( ) response Override the service() method
20
HttpServlet Class et/http/HttpServlet.html java.lang.Object javax.servlet.GenericServlet javax.servlet.http.HttpServlet A function: protected void doGet(HttpServletRequest req, HttpServletResponse resp)
21
Request and Response Objects
They form the core objects for communication. HttpServletRequest contains all the details about the client request to the servlet. E.g., request.getParameter(”paramName”); HttpServletResponse contains all the information for replying back to the client. For e.g., the output stream.
22
Sharing stateful info within a session
The first thread of an HTTP request keeps stateful information as key-value pairs on the heap of the server Since they are on the heap, they remains on the server even after the first thread is finished Threads of later requests can retrieve these key-value pairs from the heap
23
Comparisons CGI/FastCGI: Servlets:
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.
24
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
25
Other techniques: JSP
26
ASP example Other techniques: ASP (Microsoft)
<html> <body> <% response.write("Hello World!") %> </body> </html> The browser shows: Hello World!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.