Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name In...

22
Java Enterprise Edition Programming Page 1 of 9 Configuring 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/ file

Transcript of Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name In...

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

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/file

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

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.web.ValidateUserServlet

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

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.

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

Java Enterprise Edition Programming

Page 4 of 9Configuring Servlets

Example of Deployment Descriptor

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD

Web Application 2.3//EN" "http://java.sun.com/dtd/web-

app_2_3.dtd"><web-app> <display-name>ADPROG1 Web Application

Example</display-name> <description> This Web Application

demonstrates a simple deployment descriptor.

It also demonstrates a servlet definition and servlet mapping.

</description <servlet> <servlet-name>Hello</servlet-

name> <servlet-class>adprog1.web.

HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-

name> <url-pattern>/greeting</url-

pattern> </servlet-mapping></web-app>

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

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.

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

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.

<<interface>>Servlet

initservicedestroy

Readyinit

service

destroy

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

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.

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

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.

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

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.

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

Java Enterprise Edition Programming

Page 10 of 9Configuring Servlets

ServletConfig API

HelloServlet

greetingText: String

init( )doPost(request,response)

HttpServlet{abstract}

init(config:ServletConfig)service(request, response)destroy( )

<<interface>>Servlet

GenericServlet

{abstract}

init(config:ServletConfig)init( )service(request,response)destroy( )getInitParameter(name:String) : StringgetInitParameterNames( ) :EnumerationgetServletName( ) : String

getInitParameter(name:String) : StringgetInitParameterNames( ) : EnumerationgetServletName( ) : String

<<interface>>Servlet

getInitParameter(name:String) : StringgetInitParameterNames( ) : EnumerationgetServletName( ) : String

VenderServletConfigImpl

delegate

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

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:

<servlet><servlet-name>MsgHello1</servlet-name><servlet-class>adprog1.web.HelloServlet</servlet-class><init-param>

<param-name>msgText</param-name>

<param-value>Hello</param-value></init-param><init-param>

<param-name>msgText</param-name>

<param-value>Hello</param-value></init-param>

</servlet>

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

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.

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

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

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

Java Enterprise Edition Programming

Page 14 of 9Configuring Servlets

ServletContext API

<<abstract>>GenericServlet

getServletContext() : ServletContextlog(message: String)log(message:String, Throwable)

getInitParameter(name:String) : StringgetInitParameterNames( ) : EnumerationgetAttribute(name: String) : ObjectsetAttribute(name: String, value:Object)getAttributeNames() : EnumerationgetResource(path) : URLgetResourceAsStream(path) : InputStreamlog(message: String)log(message: String. Throwable: excp

<<interface>>ServletContext

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

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.

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

Java Enterprise Edition Programming

Page 16 of 9Configuring Servlets

Example

<web-app><display-name></display-name><description></description><context-param>

<param-name>catalogFileName</param-name><param-value>/WEB-INF/catalog.txt</param-value

</context-param></web-app>

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

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(“catalogFileName”);

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

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(“catalogFileName”);

InputStream is = null;BufferedReader catReader = null;

try{is =

context.getResourceAsStream(catalogFileName);

catReader = new BufferedReader(new InputStreamReader(is));

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

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.”);

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

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”,catalog);

Retrieving an Application Scoped Attribute

ServletContext context = (ProductList)

context.getAttribute(“catalog”);

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

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.

Ready

New

initialize destroy

Destroyed

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

Java Enterprise Edition Programming

Page 22 of 9Configuring Servlets

ServletContextListener API

<<interface>>ServletContextListener

contextInitialized(event)contextDestroyed(event)

getServletContext() : ServletContext

ServletContextEvent