HelloWorld Servlet Sample

This document contains the following topics : 

Overview of HelloWorld Servlet Sample

This Sample describes how to create a basic servlet application.The Servlet application creates a simple HTML page and displays "Hello Oracle World" string in it.

Steps in the Sample
 
Step 1. After invoking the application, it will display a simple HTML pagedisplaying the "Hello Oracle World" String.

Required Software

  • Oracle9iAS Containers for J2EE (OC4J) downloadable from OTN
  • JDK Version 1.2 or higher.
  • 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 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 oc4j.jar, its 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 a jar file HelloWorld.jar which contains the following files required for the sample : 
 
File Name Description
HelloWorld.java The source file which displays the message "Hello Oracle World!"
web.xml This file is used to define the Web application deployment parameters and is included in the WAR(Web Archive) file. Only required for WAR deployment.
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 HelloWorld.jar file using
jar xvf HelloWorld.jar
After extracting the .jar file, it creates 'HelloWorld' directory and all required files in it. 
Step 2. Under the HelloWorld directory created in Step 1, compile the *.java file to create the class file by using the command below: 
 javac *.java
This will create HelloWorld.class
Step 3. Move  HelloWorld.class file to <J2EE_HOME>\default-web-app\Web-inf\classes directory. 
Step 4. Now start OC4J by giving the command from the 
<J2EE_HOME>  directory. 
java -jar oc4j.jar 
Now the sample is ready to be run
WAR Deployment to OC4J using Standard J2EE way:
 
Step 1. Extract the HelloWorld.jar file using following command: 
jar xvf HelloWorld.jar 
After extracting the .jar file, it creates 'HelloWorld' directory and all required files of the sample in it.
Step 2. Under the HelloWorld directory created in Step 1, compile the *.java files to create the class files by using the command below: 
 javac *.java
This will create HelloWorld.class
Step 3. Create the following directories say, in d:\. 
d:\HelloWorld 
d:\HelloWorld\src 
d:\HelloWorld\WEB-INF 
d:\HelloWorld\WEB-INF\classes 
Step 4. Move Readme.html file to d:\HelloWorld\  directory. 
Step 5. Move web.xml file to d:\HelloWorld\WEB-INF. 
Step 6. Move the HelloWorld.java to d:\HelloWorld\src
Step 7. Move the HelloWorld.class to d:\HelloWorld\WEB-INF\classes
Step 8. Create the war file, HelloWorld.war 
       cd d:\HelloWorld 
       jar -cvfM HelloWorld.war .
Step 9. Copy the file HelloWorld.war to <J2EE_HOME>\applications.
Step 10. Open the file application.xml present in <J2EE_HOME>\config directory and after 
<web-module id="defaultWebApp" path="../default-web-app" /> line add:
<web-module id="HelloWorld" path="../applications/HelloWorld.war" /> 
Step 11. Open the http-web-site.xml file present in <J2EE_HOME>\config directory and 
after <default-web-app application="default" name="defaultWebApp" /> line add:
<web-app application="default" name="HelloWorld" root="/technology/HelloWorld" />
Step 12. Now start 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 HelloWorld.jar file. 
 jar xvf HelloWorld.jar 
 After extracting the .jar file, it creates 'HelloWorld' directory and all required files of the sample in it.
Step 2. Click File -> New -> Projects -> Workspace. Name the workspace as HelloWorld.jws and create it in the directory <Base>\HelloWorld directory, where  <Base> is the directory where you have extracted the sample. 'Create Empty Project'
checkbox is checked by default, this start project creation wizard.
Step 3. Let the Project directory be <Base>\Helloworld directory and the name of the project be HelloWorld.jpr.  Right click on HelloWorld.jpr and select Project Settings -> Development -> Libraries and include the following libraries in the "Available" list. (JDeveloper Runtime, Servlet Runtime). Also right click on HelloWorld.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 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 HelloWorld.jpr and Click on "+" button. Now add the source file HelloWorld.java to HelloWorld.jpr. It is in <Base>\HelloWorld directory.
Step 6. Make the project by right clicking on the Helloworld.jpr and selecting the make option from it.
Step 7. 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 HelloWorld.deploy Click Save.  Save the WAR file and EAR file in <Base>\HelloWorld directory with the name HelloWorld.war and HelloWorld.ear. Click OK (Here <Base> directory is the directory where you have extracted the sample) 
Step 8. The file web.xml gets automatically created while creating the deployment profile. This file needs to be edited to include servlet class. You can simply delete the content of the file and copy the following
<?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>HelloWorldDisplayName </display-name>
                <servlet>
                   <servlet-name>HelloWorldServletName</servlet-name>
                   <servlet-class>HelloWorld</servlet-class>
                </servlet>
              <servlet-mapping>
                 <servlet-name>HelloWorldServletName</servlet-name>
                 <url-pattern>/</url-pattern>
             </servlet-mapping>
          </web-app>
Step 9. Restart the OC4J server.
Step 10. Right click on HelloWorld 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 HelloWorld servlet deployment, now the sample is ready to run .

Running the Sample on OC4J :

After deploying the sample, access the it by using URL like: 
http://<IP-address>:<Port>/<path-to-servlet>/<sample directory>/<main-servlet-file> where 
 

<IP-address>   is the IP-address of the machine on which OC4J is setup
<Port>   is the port on which OC4J server is running. 
(refer to file <J2EE_HOME>\config\default-web-site.xml).   Default is: 8888
<path-to-servlet>  is the default directory where all servlets classes are placed. 
 i.e  <J2EE_HOME>\default-web-app\BasicServlets. Refer to the Step1 in the above Installing and preparing the sample for simple deployment. This will not be there for WAR   deployment.
<sample directory>  is the directory where the source files for the samples are placed.
<main-servlet-file>  is the main servlet file of the sample that is called from the browser.

For example, the URLs to the sample can be like: 

Simple Deployment:
http://152.69.168.208:8888/servlet/HelloWorld

WAR Deployment (when deployed using Standard J2EE way)
http://152.69.168.208:8888/HelloWorld/HelloWorld

JDeveloper Deployment (when deployed using Oracle9i JDeveloper)
http://152.69.168.208:8888/HelloWorld-HelloWorld-context-root/HelloWorld   

This application is just a single call to HelloWorld servlet; it will display a simple HTML page displaying 
the message "Hello Oracle World!". 



Please enter your comments about this sample in the OTN Sample Code Discussion Forum. 
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