Using Servlet Features in Oracle Containers for Java EE 11 Technology Preview

Using Servlet Features in Oracle Containers for Java EE 11 Technology Preview

First Publication: 01-Nov-04
Last Update: 20-Mar-07
Author: Frances Zhao

Introduction

This demo illustrates some of the Servlet 2.4 features supported by OC4J:

  • Servlet as welcome file
  • New listener interfaces like ServletRequestListener
  • New methods in ServletRequest

What are the Prerequisites?

What You Should Know

  • You should be familiar with these technologies: Servlet, JavaServer Pages (JSP)

What are the Software Requirements?

This demo requires that the following sofware components are installed and configured correctly:

What Variables Need to be Set?

  • %ORACLE_HOME% - The directory where you installed OC4J.
  • %JAVA_HOME% - The directory where your JDK is installed
  • %HOWTO_HOME% - The directory where this demo is unzipped

How to Build the Application?

Setting a Servlet as Welcome File

With Servlet 2.4, you can now have a servlet as a "welcome" file, which is useful for applications that use servlets as "Front Controllers".

First you must define the servlet in the Web module descriptor, web.xml.

 <servlet>
  <servlet-name>DemoServlet</servlet-name>
  <servlet-class>web.DemoServlet</servlet-class>
 </servlet>
 <!-- optional -->
 <servlet-mapping>
  <servlet-name>DemoServlet</servlet-name>
  <url-pattern>/demoservlet</url-pattern>
 </servlet-mapping>

Next, you must add a <welcome-file> element in web.xml that specifies the name of the servlet defined above.

<welcome-file-list>
  <welcome-file>DemoServlet</welcome-file>
</welcome-file-list>

As shown in the %HOWTO_HOME%/etc/web.xml, you can also define an alternative welcome file at the same time, which will be used if the servlet can not be found in the web application.

Using New Listener Interfaces

Servlet 2.4 provides two new listener interfaces that you can use for HTTP requests: javax.servlet.ServletRequestListener and javax.servlet.ServletRequestAttributeListener. This demo application only implements the former and would print out log messages to the OC4J console whenever the configured servlet DemoServlet is activated.

For the implemented listener, each time the web application receives a new request, the listener is notified and its requestInitialized() method is called. This method's parameter is a javax.servlet.ServletRequestEvent type. Calling this object's getServletRequest() method gives the developer access to the new request, a javax.servlet.ServletRequest type (to do whatever they want with the new request). See %HOWTO_HOME%/src/web/java/ServletReqLsnr.java for details.

The implemented listener must also have a constructor with no arguments. You define your implemented listener in web.xml, like:

<listener>
  <listener-class>web.ServletReqLsnr</listener-class>
</listener>

Using New Methods in ServletRequest

Servlet 2.4 adds some new methods to the ServletRequest interface:

  • getRemotePort(): Returns the IP source port of the client or last proxy that sent the request
  • getLocalName(): Returns the host name of the IP interface on which the request was received
  • getLocalAddr(): Returns the IP address of the interface on which the request was received
  • getLocalPort(): Returns the IP port number of the interface on which the request was received

These methods provide a mechanism to query the low-level IP connection details and understand how the connection routed. This How-to application also calls these methods as illustration, as in %HOWTO_HOME%/src/web/java/DemoServlet.java.

How to Build the Application?

The following instructions are for running this demonstration on a standalone instance of Oracle Containers for Java EE 11 Technology Preview.

Examining the How to Distribution

  • build - temporary directory created during the build
  • log - temporary directory holding build/deploy logs
  • etc - all necessary files to package the application
  • lib - holds the application archives that could be deployed (e.g., ear, war, rar, jar files)
  • doc - the How-to document and Javadoc's
    • javadoc - the javadoc of the different source files
    • how-to-web-servlet-intro..html - this How-to page
  • src - the source of the demo
    • web - contains HTML pages and Servlet code

Setting Up the Application

Environment Configuration

Please check to make sure that the following properties are configured correctly in the ant-oracle.properties file located in the root of the sample's distribution (NOTE:  Some of these properties will default to the values of corresponding environment variables as noted below.  If you have these variables setup in your environment you may not have to alter the values in the file).  If necessary, modify these variable to the proper values for you environment:

  • oracle.home - the root directory of oracle installation.  Defaults to ORACLE_HOME environment variable.
  • java.home -  the root directory of JDK installation.  Defaults to JAVA_HOME environment variable.
  • oracleas.host - the hostname of the platform on which the OC4J instance is running.  Defaults to localhost.
  • oracleas.http.port - the port on which the OC4J HTTP listener is listening.  Defaults to 8888.
  • oracleas.admin.port  - the port on which the OC4J administration processor is listening.  Defaults to 23791.
  • oracleas.admin.user - the name of the OC4J administrator.  Defaults to "oc4jadmin".
  • oracleas.admin.password - the password for the OC4J administrator.  Defaults to "welcome".
  • oracleas.binding.module - the name of the HTTP web site to which the deployed application is bound.  Defaults to "default-web-site".
In addition, please make sure that the ant command associated with the OC4J ant distribution is in your execution path (%ORACLE_HOME%/ant/bin).

Starting the OC4J Instance

Start your Oracle Containers for Java EE 11 Technology Preview instance as follows:

  • %ORACLE_HOME%/bin/oc4j start

Note that the oc4j command expects the JAVA_HOME environment variable to point to a full JDK installation.

Generating, Compiling and Deploying the Application

Build the Application

Make sure that the ant command associated with the OC4J ant distribution is in your execution path (%ORACLE_HOME%/ant/bin).

In the top-level %HOWTO_HOME% directory, type the command:

  • ant

You should now have newly created servlet_demo.ear and servlet_demo-web.war in your %HOWTO_HOME%/lib directory.

This command would also attempt to deploy the application if the build is successful. It will first test whether OC4J is running.

Deploy and Bind the Application

You can also deploy and bind the application separately by using the following command. Make sure the %ORACLE_HOME% environment variable is defined.

In the top-level %HOWTO_HOME% directory, type the command:

  • ant bind-web-app

Running the Application

In a browser window, browse to:

If the website hostname or port number are different, specify those values instead.

Alternatively, you can use this link to access the demo servlet. Reload the page several times. Check the result page, but also watch the demo listener updating the log in the OC4J console.

Summary

In this document, you should have:

  • Learned how to use some of the Servlet features provided by Oracle Containers for Java EE 11 Technology Preview

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy