Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlet Fudamentals.

Similar presentations


Presentation on theme: "Servlet Fudamentals."— Presentation transcript:

1 Servlet Fudamentals

2 Revisiting the Three-tier Model
Web-based e-commerce applications are usually built according to the “Three-tier Model”, which includes: The First-tier: Web Client The Second-tier: Server-side Application The Third-tier: Database Management Systems (DBMS)

3 Three-Tier Model First-tier : Web Clients Third-tier (DBMS) :
Database Cluster Client Browser First-tier : Web Clients Second-tier : Server-side Web Applications Web Server Application Server Third-tier (DBMS) : Database Cluster(s) SQL Server Other databases Oracle DB2 Internet Connectivity

4 Server-side programming technologies
There are numerous ways to implement server-side applications. Common examples include: CGI (Common Gateway Interface) ASP (Active Server Page) Java Servlets PHP, Perl, & other server programming languages

5 Common Gateway Interface (CGI)
Early Web page designs were “static” in the sense that a client can only request for a static HTML document from the Web server Later, CGI programming techniques were introduced to remove this constraint by providing dynamic Web pages via server-side interaction, as shown in Fig.

6 Static Web Page Retrieval (Fig. 4.2)
 HTTP Request  HTTP Document  Retrieve Document  Request for Web Document Web Documents Web Browser Web Server

7 CGI-based Web Application (Fig. 4.3)
 Get Data  HTTP Request  HTTP Document  Output (HTML)  HTML forms to invoke CGI scripts CGI Scripts/ Applications Web Browser Web Server Database  Return data

8 Disadvantages of CGI programs
Each new request activates a new process to run the CGI program Creating a process requires time and resource so CGI programs is not as scaleable a solution Also, CGI programs may raise security problems.

9 Java Servlet A servlet is a small piece of server-side application, which can be viewed as the server-side analog of an applet. In a typical servlet application, a servlet-enabled Web server receives an HTTP request from the client. It then forwards the request to the servlet engine for performing the necessary operations as specified by the program. Finally it returns a response (e.g., HTML document) to the client via the Web server (Fig. 4.5)

10 Typical Web-based Servlet Interaction (Fig. 4.5)
Servlet Engine HTTP Request HTTP Response Web Browser Web Server Database

11 What can you build with Servlets?
Search Engines E-Commerce Applications Shopping Carts Product Catalogs Intranet Applications Groupware Applications: bulletin boards file sharing

12 Servlets vs. CGI A Servlet does not run in a separate process.
A Servlet stays in memory between requests. There is only a single instance of a servlet which answers all requests concurrently. Browser 1 Web Server Browser 2 Browser N Servlet

13 How Servlets Work is servlet No Receive loaded? Request Yes is servlet
current? No Load Servlet Yes Send Response Process Request

14 Servlet Architecture The client makes a request via HTTP
Web Server Servlet Container Client (web browser) HTTP request Servlet HTTP response The client makes a request via HTTP The web server receives the requests and forwards it to the servlet If the servlet has not yet been loaded, the web server loads it into the JVM and executes it The servlet receives the HTTP request and performs some type of process The servlet returns a response to the web server The web server forwards the response to the client

15 Servlets follow a three-phase life cycle:
Servlet Life Cycle Servlets follow a three-phase life cycle: 1) initialization 2) service 3) destruction

16 Life Cycle: Initialization
A servlet container: loads a servlet class during startup, or when the servlet is needed for a request After the Servlet class is loaded, the container will instantiate it.

17 Life Cycle: Initialization
Initialization is performed by container before any request can be received. Persistent data configuration, heavy resource setup (such as JDBC connection) and any one-time activities should be performed in this state. The init() method will be called in this stage.

18 Life Cycle: Service 1 The service method is defined for handling client request. The Container of a servlet will call this method every time a request for that specific servlet is received.

19 Life Cycle: Service The Container generally handles concurrent requests with multithreads. All interactions with response and requests will occur within this phase until the servlet is destroyed.

20 Life Cycle: Service Method
The service() method is invoked to every request and is responsible for generating the response to that request. The service() method takes two parameters: javax.servlet.ServletRequest javax.servlet.ServletResponse public void service ( ServletRequest request, ServletResponse response ) throws IOException { . . . }

21 Life Cycle: Destruction
When the servlet container determines that the servlet should be removed, it calls the destroy method of the servlet. The servlet container waits until all threads running in the service method have been completed or time out before calling the destroy method.

22 Types of Servlet Generic Servlet Http Servlet javax.servlet (package)
extends javax.servlet.Servlet service method Http Servlet javax.servlet.http (package) extends javax.servlet.HttpServlet doget(), doPost()….

23 Types of servlets (cont..)
Generic servlet service(Request, Response) throws ServletException, IOException HttpServlet doGet(HttpServletRequest req, HttpServletResponse res) doPost(HttpServletRequest req, HttpServletResponse res)

24 Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet javax.servlet.http.HttpServlet

25 Generic Servlet & HTTP Servlet
Client request Server service ( ) response HTTPServlet Browser request doGet ( ) HTTP Server service ( ) response doPost ( )

26 Interface javax.servlet.Servlet
The Servlet interface defines methods to initialize a servlet to receive and respond to client requests to destroy a servlet and its resources to get any startup information to return basic information about itself, such as its author, version and copyright. Developers need to directly implement this interface only if their servlets cannot (or choose not to) inherit from GenericServlet or HttpServlet. Life Cycle Methods

27 GenericServlet - Methods
void init(ServletConfig config) Initializes the servlet. void service(ServletRequest req, ServletResponse res) Carries out a single request from the client. void destroy() Cleans up whatever resources are being held (e.g., memory, file handles, threads) and makes sure that any persistent state is synchronized with the servlet's current in-memory state. ServletConfig getServletConfig() Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet. String getServletInfo() Returns a string containing information about the servlet, such as its author, version, and copyright.

28 HTTPServlet A general servlet knows nothing about the HyperText Transfer Protocol (HTTP), which is the major protocol used for Internet. A special kind of servlet, HTTPServlet, is needed to handle requests from HTTP clients such as web browsers. HTTPServlet is included in the package javax.servlet.http as a subclass of GenericServlet.

29 Hello World Example import java.io.*; import javax.servlet.*;
import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println ("<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.0 Transitional//EN\”>"); out.println("<HTML><HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println(“<BODY><BIG>Hello World </BIG></BODY></HTML>"); out.close(); }

30 Hello World Example import java.io.*; import javax.servlet.*;
import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println ("<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.0 Transitional//EN\”>"); out.println("<HTML><HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println(“<BODY><BIG>Hello World </BIG></BODY></HTML>"); out.close(); }

31

32 DEMO


Download ppt "Servlet Fudamentals."

Similar presentations


Ads by Google