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:
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
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:
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