Developing Web Applications for WebLogic Server
You define servlets as a part of a Web application in several entries in the Web Application deployment descriptor. The first entry, under the
<servlet> element, defines a name for the servlet and specifies the compiled class that executes the servlet. (Or, instead of specifying a servlet class, you can specify a JSP.) This element also contains definitions for initialization attributes and security roles for the servlet. The second entry, under the
<servlet-mapping> element, defines the URL pattern that calls this servlet.
Servlet mapping controls how you access a servlet. The following examples demonstrate how you can use servlet mapping in your Web application. In the examples, a set of servlet configurations and mappings (from the
web.xml deployment descriptor) is followed by a table (see url-patterns and Servlet Invocation) showing the URLs used to invoke these servlets.
For more information on servlet mappings, such as general servlet mapping rules and conventions, refer to Section 11 of the Servlet 2.3 specification at: http://www.jcp.org/aboutJava/communityprocess/final/jsr053/
ServletServlet can be used to create a default mappings for servlets. For example, to create a default mapping to map all servlets to /myservlet/*, so the servlets can be called using http://host:port/web-app-name/myservlet/com/foo/FooServlet, add the following to your web.xml file:
You define initialization attributes for servlets in the Web Application deployment descriptor, web.xml, in the
<init-param> element of the
<servlet> element, using
<param-value> tags. For example:
For more information on editing the Web Application deployment descriptor, see Deploying Web Applications.
In order to deploy Java Server Pages (JSP) files, you must place them in the root (or in a subdirectory below the root) of a Web application. You define JSP configuration attributes in the
<jsp-descriptor> element of the WebLogic-specific deployment descriptor,
weblogic.xml. These attributes define the following functionality:
For a complete description of these attributes, see JSP Attribute Names and Values.
You can register a JSP as a servlet using the
<servlet> element. A servlet container maintains a map of the servlets known to it. This map is used to resolve requests that are made to the container. Adding entries into this map is known as "registering" a servlet. You add entries to this map by referencing a
<servlet> element in web.xml through the
A JSP is a type of servlet; registering a JSP is a special case of registering a servlet. Normally, JSPs are implicitly registered the first time you invoke them, based on the name of the JSP file. Therefore, the
myJSPfile.jsp file would be registered as
myJSPfile.jsp in the mapping table. You can implicitly register JSPs, as illustrated in the following example. In this example, you request the JSP with the name
/main instead of the implicit name
Weblogic Server lets you create and use custom JSP tags. Custom JSP tags are Java classes you can call from within a JSP page. To create custom JSP tags, you place them in a tag library and define their behavior in a tag library descriptor (TLD) file. You make this TLD available to the Web application containing the JSP by defining it in the Web Application deployment descriptor. It is a good idea to place the TLD file in the
WEB-INF directory of your Web application, because that directory is never available publicly.
In the Web Application deployment descriptor, you define a URI pattern for the tag library. This URI pattern must match the value in the taglib directive in your JSP pages. You also define the location of the TLD. For example, if the taglib directive in the JSP page is:
For more information on creating custom JSP tag libraries, see Programming JSP Tag Extensions.
WebLogic Server also includes several custom JSP tags that you can use in your applications. These tags perform caching, facilitate query attribute-based flow control, and facilitate iterations over sets of objects. For more information, see:
WebLogic Server allows you to set a page that is served by default if the requested URL is a directory. This feature can make your site easier to use, because the user can type a URL without giving a specific filename.
To define Welcome pages, you edit the Web application deployment descriptor,
web.xml. If you do not define Welcome Pages, WebLogic Server looks for the following files in the following order and serves the first one it finds:
Each Web application has a default servlet. This default servlet can be a servlet that you specify, or, if you do not specify a default servlet, WebLogic Server uses an internal servlet called the
FileServlet as the default servlet.
Setting up a default servlet replaces the
FileServlet and should be done carefully because the
FileServlet is used to serve most files, such as text files, HTML file, image files, and more. If you expect your default servlet to serve such files, you will need to write that functionality into your default servlet.
FileServlet includes the SERVLET_PATH when determining the source filename if docHome is not specified. As a result, it is possible to explicitly serve only files from specific directories by mapping the
You can configure WebLogic Server to respond with your own custom Web pages or other HTTP resources when particular HTTP errors or Java exceptions occur, instead of responding with the standard WebLogic Server error response pages.
You define custom error pages in the
<error-page> element of the Web application deployment descriptor (
web.xml). For more information on error pages, see error-page.
WebLogic Server supports all CGI scripts through an internal WebLogic servlet called the
CGIServlet. To use CGI, register the
CGIServlet in the Web application deployment descriptor. For more information, see Configuring Servlets.
CGIServletin your Web application by using the
<servlet-mapping>elements. The class name for the
weblogic.servlet.CGIServlet. You do not need to package this class in your Web application.
If you need to serve classes or other resources from the system
CLASSPATH, or from the
WEB-INF/classes directory of a Web application, you can use a special servlet called the
ClasspathServlet is useful for applications that use applets or RMI clients and require access to server-side classes. The
ClasspathServlet is implicitly registered and available from any application.
The resources that you use in a Web application are generally deployed externally to the application. JDBC DataSources can optionally be deployed within the scope of the Web application as part of an EAR file.
Prior to WebLogic Server 7.0, JDBC DataSources were always deployed externally to the Web application. To use external resources in the Web application, you resolve the JNDI resource name that the application uses with the global JNDI resource name using the
weblogic.xml deployment descriptors. See Configuring External Resources for more information.
WebLogic Server versions 7.x and later enable you to deploy JDBC DataSources as part of the Web application EAR file by configuring those resources in the
weblogic-application.xml deployment descriptor. Resources deployed as part of the EAR file are referred to as application-scoped resources. These resources remain private to the Web application, and application components can access the resource names directly from the local JNDI tree at
When accessing external resources (resources not deployed with the application EAR file) such as a DataSource from a Web application via Java Naming and Directory Interface (JNDI), you can map the JNDI name you look up in your code to the actual JNDI name as bound in the global JNDI tree. This mapping is made using both the
weblogic.xml deployment descriptors and allows you to change these resources without changing your application code. You provide a name that is used in your Java code, the name of the resource as bound in the JNDI tree, and the Java type of the resource, and you indicate whether security for the resource is handled programmatically by the servlet or from the credentials associated with the HTTP request.
The following example illustrates how to use an external DataSource. It assumes that you have defined a data source called
accountDataSource. For more information, see JDBC Data Sources Online Help.
Web applications can access EJBs that are deployed as part of a different application (a different EAR file) by using an external reference. The EJB being referenced exports a name to the global JNDI tree in its
weblogic-ejb-jar.xml deployment descriptor. An EJB reference in the Web application module can be linked to this global JNDI name by adding an
<ejb-reference-description> element to its
weblogic.xml deployment descriptor.
This procedure provides a level of indirection between the Web application and an EJB and is useful if you are using third-party EJBs or Web applications and cannot modify the code to directly call an EJB. In most situations, you can call the EJB directly without using this indirection. For more information, see Programming WebLogic Enterprise JavaBeans.
<ejb-ref>element of the Web Application deployment descriptor.
<ejb-reference-description>element of the WebLogic-specific deployment descriptor,
weblogic.xmlto the JNDI name defined in the
The ejb-ref element in the web.xml deployment descriptor declares that either a servlet or JSP is going to be using a particular EJB. The ejb-reference-description element in the weblogic.xml deployment descriptor binds that reference to an EJB, which is advertised in the global JNDI tree.
The ejb-reference-descriptor element indicates which ejb-ref element it is resolving with the ejb-ref-name element. That is, the ejb-reference-descriptor and ejb-ref elements with the same ejb-ref-name element go together.
Within an application, WebLogic Server binds any EJBs referenced by other application components to the environments associated with those referencing components. These resources are accessed at runtime through a JNDI name lookup relative to java:comp/env.
The following is an example of an application deployment descriptor (application.xml) for an application containing an EJB and a Web application, also called an Enterprise Application. (For the sake of brevity, the XML header is not included in this example.)
To allow the code in the Web application to use an EJB in ejb1.jar, the Web application deployment descriptor (web.xml) must include an <ejb-ref> stanza that contains an <ejb-link> referencing the JAR file and the name of the EJB that is being called.
Note that since the JAR path is relative to the WAR file, it begins with "../". Also, if the ejbname is unique across the application, the JAR path may be dropped. As a result, your entry may look like the following:
the portion of the syntax to the left of the # is a relative path to the EJB module being referenced. The syntax to the right of # is the particular EJB being referenced in that module. In the above example, the EJB JAR and WAR files are at the same level.
The name referenced in the <ejb-link> (in this example, myejb) corresponds to the <ejb-name> element of the referenced EJB's descriptor. As a result, the deployment descriptor (ejb-jar.xml) of the EJB module that this <ejb-ref> is referencing should have an entry an entry similar to the following:
WebLogic Server needs to convert character data contained in an HTTP request from its native encoding to the Unicode encoding used by the Java servlet API. In order to perform this conversion, WebLogic Server needs to know which code set was used to encode the request.
<form>tag. For example, this form tag sets
SJISas the character set for the content:
<input-charset>element in the WebLogic-specific deployment descriptor,
<java-charset-name>element defines the encoding used to convert data when the URL of the request contains the path specified with the
The names assigned by the Internet Assigned Numbers Authority (IANA) to describe character sets are sometimes different from the names used by Java. Because all HTTP communication uses the IANA character set names and these names are not always the same, WebLogic Server internally maps IANA character set names to Java character set names and can usually determine the correct mapping. However, you can resolve any ambiguities by explicitly mapping an IANA character set to the name of a Java character set.
To map on IANA character set to a Java character, set the character set names in the
<charset-mapping> element of the WebLogic-specific deployment descriptor,
weblogic.xml. Define the IANA character set name in the <iana-charset-name> element and the Java character set name in the <java-charset-name> element. For example: