HTTP Tunneling using
Servlets Sample
This document contains the following topics
:
Overview
of HTTP Tunneling using Servlets Sample
This Sample illustrates HTTP Tunneling
using Servlets. The HTTP tunneling can be thought as a way to use an existing
road of communication (HTTP) and create a subprotocol within it to perform
specific tasks.
The subprotocol will contain
all the information necessary to create an object on the Web Server, invoke
the method on that object and return results back to the client. This
Sample implements below mentioned subprotocol.
Each request consists of two
arguments. The first argument is an Integer which specifies what method
to invoke on the subsequent Object Argument.
|
Value of First Argument |
Task to be done by
the Servlet |
| 0 |
Initialize the database
Connection |
| 1 |
Get the subsequent Object
which represents Country Name String. Find out all the cities
present in the given country.
Send back concatenated city
Names String to the client. |
| 2 |
Close the Database Connection |
This simple subprotocol is
implemented in this Sample.
Steps in the Sample :
| Step 1. |
After the
user invokes the application, the user is prompted to select any country
name from the combo box. |
| Step 2. |
After user
selects the country name and presses the "FIND CITIES" button, a request
is sent to the tunnelServlet. The Servlet using the rules specified
above, invokes the proper method and sends back the city names associated
with the given country. |
| Step 3. |
Upon receiving
the request, all the city Names in the selected country are displayed
in a cities table. |
| Step 4. |
After pressing
"EXIT" button, the application exists. |
Required
Software
- Oracle9iAS
Containers for J2EE (OC4J) downloadable from OTN
.
- JDK Version 1.2 or higher.
- Oracle Database Server 8.1.7 or higher
downloadable from here.
- Oracle9i
JDeveloper downloadable from OTN
.
Install the above required software and
continue with the following instructions on the system where OC4J
is installed.
Pre-Requisite
Setup
| OC4J(Oracle9iAS
Container for J2EE) Setup |
Refer to the OC4J documentation
for basic installation, testing the default configurations, starting
and stopping OC4J server. Also refer the section for Servlets
in particular. This should basically give general instructions to
deploy and run the web applications. |
| CLASSPATH |
Add JDBC 2.0 related jar
file : classes12.jar or classes12dms.jar, present under <OC4J_HOME>\jdbc\lib.
This jar is basically for compiling JDBC related classes in .java
files. Also add oc4j.jar, present
under <J2EE_HOME>.
Add servlet.jar which is present
at <J2EE_HOME>\lib |
| JAVA_HOME |
Set the JAVA_HOME
system variable where JDK is installed (Example: d:\jdk1.2.1 ) |
Note: If <OC4J_HOME> is where
your OC4J Home is installed, <J2EE_HOME> is <OC4J_HOME>\j2ee\home
. For example: If oc4j is installed at d:\oc4j then d:\oc4j\j2ee\home
will be refered as <J2EE_HOME>
Description
of files in the Sample
The sample is provided as two JAR files.
a) tunnelServer.jar file contains
all the files required for the Servlet and Server Side source files for
implementing the sub-protocol which contains the following files required
for the sample :
| File Name |
Description |
| tunnelServlet.java |
The source
file for main Servlet accepting input requests & sending responses |
| RemoteServer.java |
The Source
File for Remote Server helping to invoke proper methods |
| QueryObject.java |
The Source file for Query Object which
implements query methods |
| queryInterface.java |
The Source file for defining sub-protocol
methods as interface |
| cityRecord.java |
The Source file for the Result of
the Query |
| web.xml |
This file is used to define
the Web application deployment parameters and is included in the WAR
file. Only required for WAR deployment. |
| Readme.html |
This file. |
b) tunnelClient.jar file contains
all the files required for the Client side application which sends the
requests to the Servlet and receives the response.
| File Name |
Description |
| mainClient.java |
The Source
File for main Client application |
| mainClientFrame.java |
The Source File for the
Client application user interface |
| tunnelClient.java |
The Source file for the Client side
class which helps in marshaling and demarshaling request data. |
| cityRecord.java |
The Source file for the result of
the request |
| genTableModel.java |
The source file for the genTableModel
class, which handles the JTable data. |
| GridBagConstraints2.java |
The source file for the
GridBagConstraints2 class which simplifies the use of java.awt.GridBagconstraints |
| Readme.html |
This file. |
Installing
and Preparing the Sample
Note: Linux Users should use / instead
of \ in the path variables.
Simple
Deployment:
| Step 1. |
Extract the tunnelServer.jar file
jar xvf tunnelServer.jar
After extracting the .jar file, it creates
'tunnelServer' directory and all required files in it. |
| Step 2. |
Open ConnectionParams.java file
present under the tunnelServer folder created using Step 1. Change
the details in the file to that of the database instance you are connecting
to. |
| Step 3. |
Compile the *.java file to create the
class files by using the command below:
javac *.java |
| Step 4. |
Move the class files
to the directory: <J2EE_HOME>\default-web-app\Web-inf\classes |
| Step 5. |
Register Database Connection
parameters:
To include your application
specific connection parameters of your database, edit the file data-sources.xml
under <J2EE_HOME>\config
directory. Add the following lines to create a data source. Change
the hostname, port, username/password, database name, driver type
to suit your application. Make changes for those which are in RED
below. Save the file. This will register the data-source which can
be used across your application.
<data-source
class="oracle.jdbc.pool.OracleConnectionPoolDataSource"
name="ifso817DS"
location="jdbc/Loneifso817DS"
xa-location="jdbc/xa/ifso817XADS"
ejb-location="jdbc/Pooledifso817DS"
url="jdbc:oracle:thin:@insn104a.idc.oracle.com:1521:ifso817"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="travel"
password="travel"
inactivity-timeout="30"
/>
|
| Step 6. |
Restart OC4J after you
modify the data-sources.xml file. |
Now the sample is ready to be
run .
WAR
Deployment to OC4J using Standard J2EE way:
| Step 1. |
Extract the tunnelServer.jar file.
jar xvf tunnelServer.jar
After extracting the .jar file, it creates
'tunnelServer' directory and all required files of the
sample in it. |
| Step 2. |
Open ConnectionParams.java file
present under the tunnelServer folder created using Step 1. Change
the details in the file to that of the database instance you are connecting
to. |
| Step 3. |
Compile the *.java files to create
the class files by using the command below:
javac *.java |
| Step 4. |
Create the
following directories say, in d:\.
d:\tunnelServer
d:\tunnelServer\src
d:\tunnelServer\WEB-INF
d:\tunnelServer\WEB-INF\classes |
| Step 5. |
Move Readme.html
file to d:\tunnelServer\ directory. |
| Step 6. |
Move web.xml
file to d:\tunnelServer\WEB-INF. |
| Step 7. |
Move the java
source files to d:\tunnelServer\src |
| Step 8. |
Move the class files to d:\runnelServer\WEB-INF\classes |
| Step 9. |
Make sure that you add
the data source to access the database. Refer to Step 5 of simple
deployment to register Database Connection parameters |
| Step 10. |
Create the war file, tunnelServer.war
cd d:\tunnelServer
jar -cvfM tunnelServer.war . |
| Step 11. |
Copy the file tunnelServer.war
to <J2EE_HOME>\applications. |
| Step 12. |
Open the file
application.xml present in <J2EE_HOME>\config
directory and
add the following after the <web-module
id="defaultWebApp" path="../default-web-app" /> line:
<web-module id="tunnelServer"
path="../applications/tunnelServer.war" /> |
| Step 13. |
Open the
http-web-site.xml file present in <J2EE_HOME>\config
directory and
add the following after the <default-web-app
application="default" name="defaultWebApp" /> line:
<web-app application="default"
name="tunnelServer" root="/technology/tunnelServer" /> |
| Step 14. |
Now start
the OC4J by giving the command from the <J2EE_HOME>
directory.
java -jar oc4j.jar |
Now the sample is ready to be
run .
Deploying
the servlet to OC4J using Oracle9i
JDeveloper (automated creation and deployment of .war file)
Following are the steps that are to be followed to deploy and run the
servlet from Oracle9i
JDeveloper
| Step 1. |
Extract the tunnelServer.jar
file.
jar xvf tunnelServer.jar
After extracting the .jar file, it creates
'tunnelServer' directory and all required files of the sample in it. |
| Step 2. |
Click File -> New ->
Projects -> Workspace. Name the workspace as tunnelServer.jws and
create it in the directory <Base>\tunnelServer directory, where
<Base> is the directory where you have extracted the sample.
Check 'Create Empty Project' checkbox , this start project creation
wizard. |
| Step 3. |
Let the Project directory
be <Base>\tunnelServer
directory and the name of the project be tunnelServer.jpr. Right
click on tunnelServer.jpr and select Project Settings -> Development
-> Libraries and include the following libraries in the "Available"
list. (JDeveloper Runtime, Servlet Runtime, Oracle JDBC). Also
right click on tunnelServer.jpr
and select Project Settings - > Common - > Input paths ->Java
Source Path and make sure that it points to the directory where you
have extracted this sample. |
| Step 4. |
Create an Application
Server Connection. This can be done by clicking on the Connections
Node. Right click on Application Servers and select "New Connection".
Provide the appropriate connection parameters of the Application Server
to which you want to deploy the servlets. |
| Step 5. |
Select tunnelServer.jpr
and Click on "+" button.Now add the java source files. It is in <Base>\tunnelServer
directory. |
| Step 6. |
Open ConnectionParams.java
file present under the tunnelServer folder created using Step 1. Change
the details in the file to that of the database instance you are connecting
to. |
| Step 7. |
Make the project
by right clicking on the tunnelServer.jpr and selecting make
option from it. |
| Step 8. |
Select File ->
New -> Deployment Profiles -> J2EE Web Module (WAR File), save
the Deployment Profile, this brings a File Dialog, where you can mention
the name of the deployment profile and the directory to which you
want to save the profile. Accept the default values for the
directory and mention the file name as tunnelServer.deploy
Click Save. Save the WAR file and EAR file in <Base>\tunnelServer
directory with the name tunnelServer.war and tunnelServer.ear.
Click OK (Here <Base> directory is the directory where you have
extracted the sample) |
| Step 9. |
The file web.xml
gets automatically created while creating the deployment profile.
This file needs to be edited to include servlet class and servlet
mapping
<?xml version = '1.0' encoding
= 'UTF-8'?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>tunnelServerDisplayName </display-name>
<servlet>
<servlet-name>tunnelServerName</servlet-name>
<servlet-class>tunnelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tunnelServerName</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app> |
| Step 10. |
Register Database Connection
parameters:
To include your application specific
connection parameters of your database, edit the file
data-sources.xml under <J2EE_HOME>\config
directory. Add the following lines to create a data source. Change
the hostname, port, username/password, database name, driver type
to suit your application. Make changes for those which are in
RED below. Save the file. This will register the data-source which
can be used across your application.
<data-source
class="oracle.jdbc.pool.OracleConnectionPoolDataSource"
name="ifso817DS"
location="jdbc/Loneifso817DS"
xa-location="jdbc/xa/ifso817XADS"
ejb-location="jdbc/Pooledifso817DS"
url="jdbc:oracle:thin:@insn104a.idc.oracle.com:1521:ifso817"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="travel"
password="travel"
inactivity-timeout="30"
/> |
| Step 11. |
Restart the OC4J
server |
| Step 12. |
Right click on tunnelServer
deployment profile, and choose "Deploy to". Choose the server to which
you want to deploy your servlet .Check for the successful deployment
of the servlet by looking at the deployment
console. |
This completes the tunnelServer deployment,
now the sample is ready to
run .
Running the Sample :
| Step 1. |
Extract the tunnelClient.jar
file using following command:
jar xvf tunnelClient .jar
After extracting the .jar file, it creates
'tunnelClient' directory and all required files in it. |
| Step 2. |
Edit the mainClient.java
file.
Substitute proper URL value of the tunnelServer
in the String s_webServerURL.
For example, the URLs to the sample
can be like:
Simple Deployment:
http://152.69.168.208:8888/servlet/tunnelServlet
WAR Deployment: (when deployed
using Standard J2EE way)
http://152.69.168.209:8888/tunnelServer/tunnelServlet
JDeveloper Deployment (when
deployed using Oracle9i
JDeveloper)
http://152.69.168.208:8888/tunnelServer-tunnelServer-context-root/TravelServlet
|
| Step 3. |
Compile the *.java files in the tunnelClient
directory to create the class files by using the command below:
javac *.java |
| Step 4 |
Run the mainClient using command
java mainClient |
Please enter your comments about this sample
in the
OTN Sample Code Discussion Forum. |