Oracle Application Server 10 Oracle Containers For J2EE - Servlet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Oracle Application Server 10g

Oracle Containers for J2EE – Servlet


Frequently Asked Questions
October 2006

This FAQ addresses frequently asked questions relating to Servlet Container aspects of Oracle
Containers for J2EE 10g (10.1.3).

Oracle 10.1.3 Container for J2EE is Standards Compliant with J2EE 1.4 including Servlets 2.4.

1. How do I automatically load a Web application during OC4J startup?

You have to set the load-on-startup attibute to "true" in your http-web-site.xml for the <web-app>
tag e.g.

<web-app application="default" name="lab02War" root="lab02" load-on-


startup="true"/>

In the deployment descriptor for your Web application i.e. web.xml you can specify the priority of
the servlets to be loaded e.g. <load-on-startup>priorityNumber</load-on-startup>

For values >= 0, pre-loading will occur in the specified order. Duplicate values will cause those
servlets to be loaded in declaration order.

For values < 0, pre-loading will NOT occur.

For an empty element, the behavior is as if the element had been specified with
Integer.MAX_VALUE. This ensures that these will be pre-loaded AFTER any with values >= 0.

2. How can I stop users from directly connecting to the OC4J and force them to come
through the Oracle HTTP server ?

You can set access mask for a web application by making changes in the orion-web.xml for your
web application to allow access to a particular host or IP address where your Oracle HTTP
Server is running. For example you want to allow access only from host named dpanda-us and
machine with IP address 192.168.1.100 you have to add the following in the orion-web.xml:

<access-mask default="deny">
<host-access domain="dpanda-us" mode="allow" />
<ip-access ip="192.168.1.100" mode="allow" />
</access-mask>

In OracleAS, OC4J and Oracle HTTP Server are tightly integrated with mod_oc4j and access
control can be specified and enforced at the Oracle HTTP Server level.

3. How do I define an environment variable that can be accessed in all servlets in a Web
application ?
A J2EE application may require one or more initialization parameters to be accessed by one or
more servlets in a web application. An initialization parameter may be for a context or for a
specific servlet. The context level parameters can be accessed by any servlets in the web
application.

a) To define a context parameter you have to add in the following in the web.xml as follows:

<context-param>
<param-name>filedir</param-name>
<param-value>/temp</param-value>
</context-param>

b)You have to use context.getInitParameter method to get the Context level


parameter as in the following code:

javax.servlet.ServletContext context = getServletContext();


String fileDir = context.getInitParameter("filedir");

4. How do I deploy an application?

For deploying a Web application like the default-web-app, please read Chapter 5 of the OC4J
Deployment Guide.

5. How do I configure OC4J to not to remove the generated servlet source code for JSPs
during execution?

You can set development="true" in the <orion-web-app> element in your orion-web.xml file and
that will save your generated code in the persistence directory.

If you want to save the generated code for all Web applications then you have to make this
change in the global-web-application.xml.

For Oracle9iAS Release 2 and higher, the JSP translator will automatically save all translated
JSP source text. Please see the JSP FAQ for details.

6. How do I setup the OC4J HTTP server to allow directory browsing?

The OC4J HTTP server can be configured to support directory browsing of deployed Web
applications. The following steps will enable directory browsing for a deployed Web application.

a) Open the orion-web.xml for your Web application


b) Locate the <orion-web-app> tag
c) Add the attribute: directory-browsing="allow"

You should now be able to browse any directories that do NOT have a welcome file present.
Welcome files are configured in your web.xml file, with the <welcome-file-list> attribute.

7. How do I set the session time out for a Web application?

Servlet sessions (and JSP sessions too since they're the same thing) have a timeout period
controlled by a node in web.xml, in the following format:
<session-config>
<session-timeout>60</session-timeout>
</session-config>

This represents the number of minutes since the session was last used, so each page that affects
or accesses the session will reset this timer.

8. How do I use a pre-configured DataSource from Servlet?

Let us assume the DataSource name is OracleDS and the location is jdbc/OracleDS. These are
defined in the j2ee/home/config/data-sources.xml file.

The following code demonstrates how to lookup the DataSource and obtain a connection.

try
{
InitialContext context = new InitialContext();
DataSource myDS = (DataSource) context.lookup("jdbc/OracleDS");
Connection conn = myDS.getConnection();
...
// Use connection as normal since it's a normal JDBC connection
}
catch(NamingException ne)
{
...
}

9. How do I set up a servlet chain for a specific mime type in a Web application in OC4J?

Assuming you have a servlet named ChainServlet that you want to establish as a chainer for
mime type text/html you have to edit the orion-web.xml for your Web application to have the
following entry:

<servlet-chaining mime-type="text/html" servlet-name="ChainServlet" />

Servlet Filters are recommended for servlet chaining.

10. How do I add a directory that doesn't physically reside below the document-root
among the web-exposed files?

This can be performed by specifying a virtual-directory which maps a physical directory outside of
your web application to a virtual path. Assuming all your images in a directory called e:\pictures
and you want to add an access point to this as /img then edit the orion-web.xml file for your Web
application and add a virtual-directory as follows:

<orion-web>
...
<virtual-directory virtual-path="/img" real-path="/e:/pictures/" />
...
</orion-web>

11. How do I add a JSP file in the welcome-file-list if the jsp is in a sub-directory?

Let's assume the name of your file as xx.jsp and this sits inside your web\jsp\one sub-directory.
Assuming you are deploying your Web application using the standard J2EE WAR format, you
may add the file to the welcome file list in the web deployment descriptor file, web.xml:

<welcome-file-list>
...
<welcome-file>web/jsp/one/xx.jsp</welcome-file>
...
</welcome-file-list>

The directory web/jsp/one is relative to your web module home.

12. How do I setup HTTP-tunneling in OC4J?

Remote Method Invocation (RMI) enables developers to build distributed applications in java.
However, RMI calls are normally blocked by firewalls. RMI HTTP tunneling is a RMI
implementation to provide a way for RMI to get through firewalls using HTTP.

You can use the RMI HTTP Tunneling functionality through an HTTP Proxy.

It is run as a servlet - the RMIHttpTunnelProxyServlet.

Your global-web-application.xml should have this entry:

<servlet>
<servlet-name>rmi</servlet-name>
<servlet-class>com.evermind.server.rmi.RMIHttpTunnelServlet</servlet-class>
</servlet>

Run it by prepending the RMI URL with "http:". For example in your client program you access an
RMI object as Naming.Lookup("ormi://localhost/theapp") thus, you have to modify that to
Naming.Lookup("http:ormi://localhost/theapp")

13. How do I setup OC4J to use Class-Path attribute in a WAR files Manifest to load class
libraries?

OC4J has a configurable option to load class libraries included in the Manifest Class-Path entry.
You have to include the following directive in the orion-web.xml to ask OC4J to load class
libraries from the Manifest Class-Path:

<web-app-class-loader include-war-manifest-class-path="true" />

14. How do I setup OC4J to load local classes packaged in my Web Application overriding
System classes or classes in the higher level?

OC4J has a configurable option to load local classes first, overriding classes at a higher level.
You have to include the following directive in the orion-web.xml for your web module to ask OC4J
to load local classes:

<web-app-class-loader search-local-classes-first="true" />

15. Which classloader should I use to load a resource file in Servlet/JSP packaged in my
WAR module?
You have to use the context classsloader to load properties or resource file.

For example you have a properties file called debu.properties in WEB-INF/classes of your web
module, you have to use the following code to load the properties file:

InputStream is
= Thread.currentThread().getContextClassLoader().getResourceAsStream("debu.prop
erties");

16. How do I configure OC4J to locate a URL resource by making a JNDI lookup?

a) You have to define the resource in the deployment descriptor (e.g. if you are trying to access
the URL resource from a web application then you have to have the following entry in the
web.xml).

<resource-ref>
<res-ref-name>url/MyURL</res-ref-name>
<res-type>java.net.URL</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

b) You have to map the resource name (e.g. url/MyURL should be mapped to a location in your
vendor specific deployment descriptor, e.g. orion-web.xml as follows):

<resource-ref-mapping name="url/MyURL" location="http://debu.us.oracle.com" />

c) Now you can lookup the URL resource and connect using the following code:

HttpURLConnection connection = null;


Context context = new InitialContext();
URL url = (URL) context.lookup ("java:comp/env/url/MyURL");
connection = (HttpURLConnection)url.openConnection();

17. How do I display the stack trace in the browser?

The following changes have been made to the error messages that are presented when an
exception occurs and there is no error page to handle it. The following error message appears for
security-sensitive exceptions:

Servlet error: An exception occurred. For security reasons, it may not be


included in this response. Please consult the application log for
details.

For other exceptions, the following error message appears:

Servlet error: An exception occurred. The current application deployment


descriptors does not allow for including it in this response. Please
consult the application log for details.

If you have tests that rely on the display of an exception or stack trace, you can cause the stack
trace to be displayed by running the application in developer mode. To run an application in
development mode, set the development attribute of the <orion-web-app> element to "true" in the
orion-web.xml file. Here is an example:

<?xml version="1.0"?>
<orion-web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/or
ion-web-10_0.xsd"
deployment-version="null"
deployment-time="1152223108413"
jsp-cache-directory="./persistence"
jsp-cache-tlds="standard"
temporary-directory="./temp"
context-root="/DevelopmentThrowException"
schema-major-version="10"
schema-minor-version="0"
development="true">
<!-- Uncomment this element to control web application class loader
behavior. <web-app-class-loader search-local-classes-first="true"
include-war-manifest-class-path="true" />
-->
<web-app>
</web-app>
</orion-web-app>

OC4J JSP Documentation

Oracle Application Server Production Documentation library


http://www.oracle.com/technology/documentation/appserver10131.html
Oracle Application Server 10g FAQ
October, 2006
Author: Dana Singleterry

Oracle Corporation
World Headquarters
500 Oracle Parkway
Redwood Shores, CA 94065
U.S.A.
Worldwide Inquiries:
+1.650.506.7000
Fax +1.650.506.7200
http://www.oracle.com/
Copyright © Oracle Corporation 2006
All Rights Reserved
This document is provided for informational purposes only,
and the information herein is subject to change
without notice. Please report any errors herein to
Oracle Corporation. Oracle Corporation does not provide
any warranties covering and specifically disclaims any
liability in connection with this document.
Oracle is a registered trademark of Oracle Corporation.
All other company and product names mentioned are used
for identification purposes only and may be trademarks of
their respective owners.

You might also like