Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name  In multiple web applications, a “context name” is used.

Similar presentations


Presentation on theme: "Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name  In multiple web applications, a “context name” is used."— Presentation transcript:

1 Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name  In multiple web applications, a “context name” is used to access a specific Web application.  The general syntax of a servlet Web application URL is: http://host:port/context/path/fil e

2 Java Enterprise Edition Programming Page 2 of 9Configuring Servlets Accessing Servlet Using the Fully-Qualified Class  The problem in accessing servlet using the fully-qualified class is that the names can be very long.  It reveals the implementation details of about the Web application.  Example http://localhost:8080/ValidateUser/servlet/adprog1.w eb.ValidateUserServlet

3 Java Enterprise Edition Programming Page 3 of 9Configuring Servlets Servlet Mapping  In the deployment descriptor, servlet mapping is performed in two steps. 1. A servlet definition is configured which names a particular servlet and specifies the fully qualified Java technology class that implements that servlet. 2. A servlet mapping is created which identifies a URL structure that maps to the named servlet definition.

4 Java Enterprise Edition Programming Page 4 of 9Configuring Servlets Example of Deployment Descriptor <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web- app_2_3.dtd"> ADPROG1 Web Application Example This Web Application demonstrates a simple deployment descriptor. It also demonstrates a servlet definition and servlet mapping. </description Hello adprog1.web. HelloServlet Hello /greeting

5 Java Enterprise Edition Programming Page 5 of 9Configuring Servlets Deployment Environment  When a Web application is deployed to the Web container, the directory structure must follow a particular format:  The static HTML files are stored in the top level directory of the Web application.  The servlet and related Java technology class files must be stored in the WEB-INF/classes directory.  The auxiliary Java Archive (JAR) files must be stored in the WEB-INF/lib directory.  The deployment descriptor must be stored in the file called web.xml in the WEB-INF directory.

6 Java Enterprise Edition Programming Page 6 of 9Configuring Servlets Servlet Life Cycle Overview  The Web container manages the life cycle of a servlet instance by calling three methods defined in the Servlet interface: init, service and destroy.

7 Java Enterprise Edition Programming Page 7 of 9Configuring Servlets init Life Cycle Method  The init method is called by the Web container when the servlet instance is first created.  The Servlet specification guarantees that no requests will be processed by this servlet until the init method has completed.

8 Java Enterprise Edition Programming Page 8 of 9Configuring Servlets service and destroy Life Cycle Method  The service method is called by the Web container to process a user request.  The destroy method is called by the Web container when the servlet instance is being destroyed.  The Servlet specification guarantees that all requests will be completely processed before the destroy method is called.

9 Java Enterprise Edition Programming Page 9 of 9Configuring Servlets Servlet Configuration  When a servlet is initialized, a common task is to load any external resources into memory.  The Web container uses a configuration object to pass the initialization parameters to the servlet at runtime.

10 Java Enterprise Edition Programming Page 10 of 9Configuring Servlets ServletConfig API

11 Java Enterprise Edition Programming Page 11 of 9Configuring Servlets Initialization Parameters  Servlet initialization parameters are name-value pairs that are declared in the deployment descriptor. The names and values are arbitrary strings.  Example: MsgHello1 adprog1.web.HelloServlet msgText Hello msgText Hello

12 Java Enterprise Edition Programming Page 12 of 9Configuring Servlets ServletContext Object  A Web application is a self-contained collection of static and dynamic resources: HTML pages, media files, data and resource files, servlets (and JSP pages), and other auxiliary Java technology classes and objects.  A ServletContext object is the runtime representation of the Web application.

13 Java Enterprise Edition Programming Page 13 of 9Configuring Servlets ServletContext API  Read-only access to application scoped initialization parameters  Read-only access to application-level file resources  Read-write access to application scoped attributes  Logging functionality

14 Java Enterprise Edition Programming Page 14 of 9Configuring Servlets ServletContext API

15 Java Enterprise Edition Programming Page 15 of 9Configuring Servlets Context Initialization Parameters  A Web application may have one or more initialization parameters.  Context initialization parameters are accessed using the getInitParameter method on the ServletContext object.  The context-param element tag is used to configure the parameters in the deployment descriptor.

16 Java Enterprise Edition Programming Page 16 of 9Configuring Servlets Example catalogFileName /WEB- INF/catalog.txt</param-value

17 Java Enterprise Edition Programming Page 17 of 9Configuring Servlets Retrieving Context Parameters  To retrieving a context initialization parameter, invoke the getInitParameter() method from the ServletContext instance. ServletContext context = sce.getServletContext() String catalogFileName = context.getInitParameter(“catal ogFileName”);

18 Java Enterprise Edition Programming Page 18 of 9Configuring Servlets Access to File Resources  The ServletContext object provides read- only access to file resources through the getResourceAsStream() method that returns a raw InputStream object.  Example ServletContext context = sce.getServletContext(); String catalogFileName = context.getInitParameter(“catal ogFileName”); InputStream is = null; BufferedReader catReader = null; try{ is = context.getResourceAsStream(cat alogFileName); catReader = new BufferedReader(new InputStreamReader(is));

19 Java Enterprise Edition Programming Page 19 of 9Configuring Servlets Writing to the Web Application Log File  The ServletContext object provides write- only access to log file:  The log(String) method writes a message to the log file.  The log(String, Throwable) method writes a message and the stack trace of the exception or error to the log file.  Example context.log(“The ProductList has been initialized.”);

20 Java Enterprise Edition Programming Page 20 of 9Configuring Servlets Accessing Shared Runtime Attributes  The ServletContext object provides read- write access to attributes shared across all servlets through the getAttribute and setAttribute methods.  Storing an Application Scoped Attribute context.setAttribute(“catalog”,ca talog);  Retrieving an Application Scoped Attribute ServletContext context = (ProductList) context.getAttribute(“catalog”) ;

21 Java Enterprise Edition Programming Page 21 of 9Configuring Servlets Web Application Life Cycle  When the Web container is started, each Web application is initialized. When the Web container is shut down, each Web application is destroyed.

22 Java Enterprise Edition Programming Page 22 of 9Configuring Servlets ServletContextListener API


Download ppt "Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name  In multiple web applications, a “context name” is used."

Similar presentations


Ads by Google