BEA Logo BEA WebLogic Server Release 6.1

  Corporate Info  |  News  |  Solutions  |  Products  |  Partners  |  Services  |  Events  |  Download  |  How To Buy

   Developing WebLogic Server Applications:   Previous topic   |   Next topic   |   Contents   

 

Writing Web Application Deployment Descriptors

 

The following sections describe how to writeWeb Application deployment descriptors:

Overview of Web Application Deployment Descriptors

Deploying Web Applications requires you to create two deployment descriptors for each Web Application. These deployment descriptors define components and operating parameters for a Web Application. Deployment descriptors are standard text files, formatted using XML notation and are packaged within the Web Application. For more information on Web Applications, see Deploying and Configuring Web Applications.

The first deployment descriptor, web.xml is defined by the Servlet 2.2 specification from Sun Microsystems. This deployment descriptor can be used to deploy a Web Application on any J2EE-compliant application server.

The second deployment descriptor, weblogic.xml, defines deployment properties that are specific to a Web Application running on WebLogic Server.

Writing the web.xml Deployment Descriptor

This section describes the steps to create the web.xml deployment descriptor. Depending on the components in your Web Application, you may not need to include all of the elements listed here to configure and deploy your Web Application.

The elements in the web.xml file must be entered in the order they are presented in this document.

Main Steps to Create the web.xml File

Step 1: Create a deployment descriptor file
Step 2: Create the header
Step 3: Create the main body of the web.xml file
Step 4: Define deployment-time attributes
Step 5: Define context parameters
Step 6: Deploy servlets
Step 7: Map a servlet to a URL
Step 8: Define the session timeout value
Step 9: Define welcome pages
Step 10: Define error pages
Step 11: Define MIME mapping
Step 12: Define a JSP tag library descriptor
Step 13: Reference external resources
Step 14: Set up security constraints
Step 15: Set up login authentication
Step 16: Define security roles
Step 17: Set environment entries
Step 18: Reference Enterprise JavaBean (EJB) resources

If you have installed the WebLogic Server samples and examples, you can look at the web.xml and weblogic.xml files in the Pet Store sample to see a working example of Web Application deployment descriptors. These files are located in the /samples/PetStore/source/dd/war/WEB-INF directory of your WebLogic Server distribution.

Detailed Steps to Create the web.xml File

Step 1: Create a deployment descriptor file

Name the file web.xml and place it under the WEB-INF directory of the Web Application. Use any text editor.

Step 2: Create the header

This text must be the first line of the file:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

The header refers to the location and version of the Document Type Descriptor (DTD) file for the deployment descriptor. Although this header references an external URL at java.sun.com, WebLogic Server contains its own copy of the DTD file, so your host server need not have access to the Internet. However, you must still include this <!DOCTYPE...> element in your web.xml file, and have it reference the external URL because the version of the DTD contained in this element is used to identify the version of this deployment descriptor.

Step 3: Create the main body of the web.xml file

Wrap all of your entries within a pair of opening and closing <web-app> tags.
  

<web-app>

All elements describing this Web Application go within the <web-app> element.

</web-app>

This tag should be the final tag in the web.xml file

In XML, properties are defined by surrounding a property name or value with opening and closing tags as shown above. The opening tag, the body (the property name or value), and the closing tag are collectively called an element. Some elements do not use the surrounding tags, but instead use a single tag that contains attributes called an empty-tag. Elements contained within other elements are indented in this text for clarity. Indenting is not necessary in an XML file.

The body of the <web-app> element itself contains additional elements that determine how the Web Application will run on WebLogic Server. The order of the tag elements within the file must follow the order reflected in this document. This ordering is defined in the Document Type Descriptor (DTD) file. For more information, refer to the DTD, available on the Sun Microsystems Web site at http://java.sun.com/j2ee/dtds/web-app_2_2.dtd.

Step 4: Define deployment-time attributes

These tags provide information for the deployment tools or the application server resource management tools. These values are not used by WebLogic Server in this release.
  

<small-icon>
   iconfile.gif(jpg)
</small-icon>

(Optional)

<large-icon>
   iconfile.gif(jpg)
</large-icon>

(Optional)

<display-name>
   application-name
</display-name>

(Optional)

<description>
   descriptive-text
</description>

(Optional)

<distributable>

(Optional)

Step 5: Define context parameters

The context-param element declares a Web Application's servlet context initialization parameters. These can be parameters that you define that will be available throughout your Web Application. You set each context-param within a single context-param element, using <param-name> and <param-value> elements. You can access these parameters in your code using the javax.servlet.ServletContext.getInitParameter() and javax.servlet.ServletContext.getInitParameterNames() methods.

Precompiling JSPs
You can use the context-param element to specify that WebLogic Server precompile JSPs on start up. For more information, see Precompiling JSPs.


Step 6: Deploy servlets

In this step, you give the servlet a name, specify the class file or JSP used to implement its behavior, and set other servlet-specific properties. List each of the servlets in your Web Application within separate <servlet>...</servlet> elements. After you create entries for all of your servlets, you must include elements that map the servlet to a URL pattern. These mapping elements are described in Step 7: Map a servlet to a URL.

Use the following elements to declare a servlet:
  

<servlet>

For more information, see servlet Element

     <servlet-name>
        name
     </servlet-name>

(Required)

     <servlet-class>
        package.name.MyClass
     </servlet-class>
     -or-
     <jsp-file>
        /foo/bar/myFile.jsp
     </jsp-file>

(Required)

     <init-param>

For more information, see init-param Element

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

(Required)

          <param-value>
             value
          </param-value>

(Required)

          <description>
             ...text...
          </description>

     </init-param>

(Optional)

     <load-on-startup>
        loadOrder
     </load-on-startup>

(Optional)

     <security-role-ref>

(Optional).

For more information, see security-role-ref Element

          <description>
             ...text...
          </description>

(Optional)

          <role-name>
             rolename
          </role-name>

(Required)

          <role-link>
             rolelink
          </role-link>

(Required)

     </security-role-ref>


     <small-icon>
        iconfile
     </small-icon>

Not Used.(Optional)

     <large-icon>
        iconfile
     </large-icon>

(Optional)

     <display-name>
        Servlet Name
     </display-name>

(Optional)

     <description>
        ...text...
     </description>

(Optional)

</servlet>

Here is an example of a servlet element that includes an initialization parameter.

<servlet>
   <init-param>
   <param-name>feedbackEmail</param-name>
   <param-value>feedback123@beasys.com</param-value>
   <description>
   The email for web-site feedback.
   </description>
     </init-param>
   </servlet>


Step 7: Map a servlet to a URL

Once you declare your servlet or JSP using a <servlet> element, map it to one or more URL patterns to make it a public HTTP resource. For each mapping, use a <servlet-mapping> element.
  

<servlet-mapping>

For more information, see servlet-mapping Element

     <servlet-name>
        name
     </servlet-name>

(Required)

     <url-pattern>
        pattern
     </url-pattern>

(Required)


</servlet-mapping>

Here is an example of a <servlet-mapping> for the <servlet> declaration example used earlier:

<servlet-mapping>
<servlet-name>LoginServlet
</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>


Step 8: Define the session timeout value
  

<session-config>

(Optional)

     <session-timeout>
        
minutes
     </session-timeout>

For more information, see session-config Element

</session-config>


Step 9: Define welcome pages
  

<welcome-file-list>

(Welcome pages are Optional.)
For more information, see welcome-file-list Element

     <welcome-file>
        myWelcomeFile.jsp
     </welcome-file>

     <welcome-file>
        myWelcomeFile.html
     </welcome-file>

See also Welcome Pages
And How WebLogic Server Resolves HTTP Requests

</welcome-file-list>

Step 10: Define error pages
  

<error-page>

(Optional) Define a customized page to respond to errors


For more information, see error-page Element

And How WebLogic Server Resolves HTTP Requests


     <error-code>
        HTTP error code
     </error-code>


     -or-


     <exception-type>
        Java exception class
     </exception-type>

     <location>URL</location>

</error-page>

Step 11: Define MIME mapping
  

<mime-mapping>

(Optional)

Define MIME types

For more information, see mime-mapping Element

     <extension>
        ext

     </extension>

     <mime-type>
        mime type
     </mime-type>

</mime-mapping>

Step 12: Define a JSP tag library descriptor
  

<taglib>

(Optional) Identify JSP tag libraries

For more information, see taglib Element

     <taglib-uri>
        string_pattern
     </taglib-uri>

(Required)

     <taglib-location>
        filename
     </taglib-location>

(Required)

</taglib>


The following is an example of a taglib directive used in a JSP:

<%@ taglib uri="string_pattern" prefix="taglib" %>

For more details, see the Programming WebLogic JSP Tag Extensions.

Step 13: Reference external resources
  

<resource-ref>

(Optional)
For more information, see resource-ref Element.

     <res-ref-name>
        name
     </res-ref-name>

(Required)

     <res-type>
        Java class
     </res-type>

(Required)

     <res-auth>
        CONTAINER | SERVLET
     </res-auth>

(Required)

</resource-ref>

Step 14: Set up security constraints

A Web Application that uses security requires the user to log in in order to access its resources. The user's credentials are verified against a security realm, and once authorized, the user will have access only to specified resources within the Web Application.

Security in a Web Application is configured using three elements:

  

<security-constraint>

(Optional) For more information, see security-constraint Element

     <web-resource-collection>

(Required) For more information, see web-resource-collection Element

             <web-resource-name>
                name
              </web-resource-name>

(Required)

              <description>
             ...text...
              </description>

(Optional)

              <url-pattern>
                  pattern
              </url-pattern>

(Optional)

              <http-method>
                   GET | POST
              </http-method>

(Optional)

     </web-resource-collection>


     <auth-constraint>

(Optional)

For more information, see auth-constraint Element

          <role-name>
             group | principal
          </role-name>

(Optional)

     </auth-constraint>

     <user-data-constraint>

(Optional)

For more information, see user-data-constraint Element

          <description>...text...</description>

(Optional)

          <transport-guarantee>

            NONE
            INTEGRAL
            or
             CONFIDENTIAL

          </transport-guarantee>

(Required)

     </user-data-constraint>

</security-constraint>

Step 15: Set up login authentication

 
  

<login-config>

(Optional)

For more information, see login-config Element

     <auth-method>
        BASIC,FORM, or
        CLIENT-CERT
     </auth-method>

(Optional) Specifies the method used to authenticate the user

     <realm-name>
        realmname
     
</realm-name>

(Optional) For more information, see Specifying a Security Realm.

     <form-login-config>

(Optional)

For more information, see form-login-config Element

Use this element if you configure the <auth-method> to FORM

     <form-login-page>
        URI
     </form-login-page>

(Required)

     <form-error-page>
        URI
      </form-error-page>

     </form-login-config>

(Required)

</login-config>

Step 16: Define security roles
  

<security-role>

(Optional)

For more information, see security-role Element

     <description>
        ...text...
     </description>

(Optional)

     <role-name>
        
rolename
     </role-name>

(Required)

</security-role>


Step 17: Set environment entries
  

<env-entry>

(Optional)

For more information, see env-entry Element

     <description>
        
...text...
     </description>

(Optional)

     <env-entry-name>
        
name
     </env-entry-name>

(Required)

     <env-entry-value>
        
value
     </env-entry-value>

(Required)

     <env-entry-type>
        
type
     </env-entry-type>

(Required)

</env-entry>

Step 18: Reference Enterprise JavaBean (EJB) resources
  

<ejb-ref>

Optional)

For more information, see ejb-ref Element

     <description>
        
...text...
     </description>

(Optional)

     <ejb-ref-name>
        name
     </ejb-ref-name>

(Required)

     <ejb-ref-type>
        
Java type
     </ejb-ref-type>

(Required)

     <home>
        mycom.ejb.AccountHome
     </home>

(Required)

     <remote>
        
mycom.ejb.Account
     </remote>

(Required)

     <ejb-link>
        ejb.name
     </ejb-link>

(Optional)

</ejb-ref>


Listing 5-1 Sample web.xml with Servlet Mapping, Welcome file, and Error Page


<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//
DTD Web Application 1.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

<!- The following servlet element defines a servlet called servletA.
The Java class of this servlet is servlets.servletA ->
  <servlet>
    <servlet-name>servletA</servlet-name>
    <servlet-class>servlets.servletA</servlet-class>
  </servlet>

<!- The following servlet element defines another servlet called
servletB. The Java class of this servlet is servlets.servletB ->
  <servlet>
    <servlet-name>servletB</servlet-name>
    <servlet-class>servlets.servletB</servlet-class>
  </servlet>

<!- The following servlet-mapping maps the servlet called servletA
(see the servlet element) to a url-pattern of "blue".
The url-pattern is used when requesting this servlet, for example:
http://host:port/myWebApp/blue. ->
  <servlet-mapping>
    <servlet-name>servletA</servlet-name>
    <url-pattern>blue</url-pattern>
  </servlet-mapping>

<!- The following servlet-mapping maps the servlet called servletB
(see the servlet element) to a url-pattern of "yellow".
The url-pattern is used when requesting this servlet, for example:
http://host:port/myWebApp/yellow. ->
  <servlet-mapping>
    <servlet-name>servletB</servlet-name>
    <url-pattern>yellow</url-pattern>
  </servlet-mapping>

<!-The following welcome-file-list specifies a welcome-file.
Welcome files are discussed elsewhere in this document->
  <welcome-file-list>
    <welcome-file>hello.html</welcome-file>
  </welcome-file-list>

<!-The following error-page element specifies a page that is served
in place of the standard HTTP error response pages, in this case
HTTP error 404.->
  <error-page>
    <error-code>404</error-code>
    <location>/error.jsp</location>
  </error-page>

</web-app>

Writing the WebLogic-Specific Deployment Descriptor (weblogic.xml)

The weblogic.xml file contains WebLogic-specific attributes for a Web Application. You define the following attributes in this file: HTTP session parameters, HTTP cookie parameters, JSP parameters, resource references, and security role assignments.

If you define external resources such as DataSources, EJBs, or a Security realm in the web.xml deployment descriptor, you can use any descriptive name to define the resource. To access the resource, you then map this resource name to the actual name of the resource in the JNDI tree using a file called weblogic.xml. Place this file in the WEB-INF directory of your Web Application.

If you have installed the WebLogic Server samples and examples, you can look at the web.xml and weblogic.xml files in the Pet Store sample to see a working example of Web application deployment descriptors. These files are located in the /samples/PetStore/source/dd/war/WEB-INF directory of your WebLogic Server distribution.

The ordering of the tag elements within the weblogic.xml file must follow the ordering specified in this document.

Main Steps to Create the weblogic.xml File

Step 1: Begin the weblogic.xml file with a DOCTYPE header
Step 2: Map security role names to a security realm
Step 3 Reference resources
Step 4: Define session parameters
Step 5: Define JSP parameter

Detailed Steps to Create the weblogic.xml File

Step 1: Begin the weblogic.xml file with a DOCTYPE header

This header refers to the location and version of the DTD file for the deployment descriptor. Although this header references an external URL at www.beasys.com, WebLogic Server has its own copy of the DTD file, so your host server need not have access to the Internet. However, you must still include this DOCTYPE element in your weblogic.xml file, and have it reference the external URL since the version of the DTD is used to identify the version of this deployment descriptor.
  

<!DOCTYPE weblogic-web-app PUBLIC "-//BEA
Systems, Inc.//DTD Web Application 6.0//EN"
"http://www.bea.com/servers/wls600/dtd/
weblogic-web-jar.dtd">

<weblogic-web-app>


     <description>
        Text description of the Web App
     </description>


     <weblogic-version>
        
     </weblogic-version>

This element is not used by WebLogic Server

Step 2: Map security role names to a security realm
  

<security-role-assignment>

         <role-name>
            name
         </role-name>

(Required)

For more information, see security-role-assignment Element

         <principal-name>
             name
         </principal-name>

(Required)

</security-role-assignment>

If you need to define multiple roles, define each additional pair of <role-name> and <principal-name> tags within separate <security-role-assignment> elements.

Step 3 Reference resources

In this step you map resources used in your Web Application to the JNDI tree. When you define an <ejb-ref-name> or a <res-ref-name> in the web.xml deployment descriptor, you also reference those names in weblogic.xml and map them to an actual JNDI name that is available in WebLogic Server. In the following example, a Data Source is referenced in a servlet with the name myDataSource. myDataSource is then referenced in web.xml and its data type defined. Finally, in the weblogic.xml file, myDataSource is mapped to the JNDI name accountDataSource, which is available in the JNDI tree. The JNDI name must match the name of an object bound in the JNDI tree. Objects can be bound to the JNDI tree programatically or by configuring them in the Administration Console. For more information, see Programming WebLogic JNDI.

Servlet code:

javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup
("
myDataSource");

web.xml entries:

<resource-ref>
. . .
   <res-ref-name>
myDataSource</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>CONTAINER</res-auth>
. . .
</resource-ref>

weblogic.xml entries:

<resource-description>
   <res-ref-name>
myDataSource</res-ref-name>
   <jndi-name>accountDataSource</jndi-name>
</security-role-ref>

A similar pattern is used to map EJBs to the JNDI tree, but uses the <ejb-ref-name> element of the <ejb-reference-description> element in place of the <res-ref-name> element of the <resource-description> element.
  

<reference-descriptor>

For more information, see reference-descriptor Element

     <resource-description>

For more information, see resource-description Element

         <res-ref-name>
            name
          </res-ref-name>

(Required)

         <jndi-name>
             JNDI name of resource
          </jndi-name>

(Required)

     </resource-description>


     <ejb-reference-description>


         <ejb-ref-name>
            name
          </ejb-ref-name>

(Required) For more information, see ejb-reference-description Element

         <jndi-name>
             JNDI name of EJB
          </jndi-name>

(Required)

     </ejb-reference-description>

</reference-descriptor>

Step 4: Define session parameters

You define HTTP session parameters for this Web Application inside of <session-param> tags, which are nested in side <session-descriptor> tags. For each <session-param> you need to supply a <param-name>...</param-name> element that names the parameter being defined and a <param-value>...</param-value> element that provides the value of the parameter. For a list of HTTP session parameters and details on setting them, see session-descriptor Element
  

<session-descriptor>

For more information, see session-descriptor Element

     <session-param>


         <param-name>
            session param name
         </param-name>


         <param-value>
             my value
         </param-value>

     </session-param>

</session-descriptor>

.

Step 5: Define JSP parameter

You define JSP configuration parameters for this Web Application inside of <jsp-param> tags, which are nested in side <jsp-descriptor> tags. For each <jsp-param> you need to supply a <param-name>...</param-name> element that names the parameter being defined and a <param-value>...</param-value> element that provides the value of the parameter. For a list of JSP parameters and details on setting them, see jsp-descriptor Element
  

<jsp-descriptor>

For more information, see jsp-descriptor Element

     <jsp-param>

          <param-name>
            jsp param name
          </param-name>

          <param-value>
             my value
          </param-value>

     </jsp-param>

</jsp-descriptor>

.

 

Back to Top