Exzilla.net
Contact
Home -> Articles -> OC4J -> HOWTO: Assembling and deploying a simple Web application with a simple WAR file.
 
Features
Forums
Files
Blogs
HOWTO: Assembling and deploying a simple Web application with a simple WAR file.
Sept 21, 2001
Fuju
Author's Bio | E-Mail
Copyright (C) 2001 exzilla.net

 

How to Assemble and deploy a simple Web application with a simple WAR file.

Prerequistires:

Assumption:

ได้มีการติดตั้ง OC4J ที่ D:\oc4j ไว้แล้ว

Purpose:

แสดงการสร้าง WAR file เพื่อใช้ในการนำไปใช้ (deployment) กับ web application ที่พัฒนาอยู่บนพื้นฐานของ J2EE โดยในตัวอย่างนี้ได้ ทำการทดสอบ Deploy ไปยัง OC4J

Step-by-Step Example:

How to Assemble and deploy a simple Web application with a simple WAR file.

1. Create WAR directory structure

ก่อนที่จะเริ่มพัฒนา Application ในตัวอย่างนี้เราต้องทำการสร้าง Directory ตามโครงสร้างที่ได้กำหนดไว้ตาม โครงสร้างของ War file

E:\workShop>mkdir simplewar
E:\workShop>cd simplewar
E:\workShop\simplewar>mkdir src
E:\workShop\simplewar>mkdir WEB-INF
E:\workShop\simplewar>mkdir WEB-INF\classes
E:\workShop\simplewar>mkdir WEB-INF\lib
E:\workShop\simplewar>

 

2.Developing your application

พัฒนา Application ตัวอย่างซึ่งประกอบด้วย file ประเภทต่าง ๆ ดังนี้

2.1 Create Jsp file and bean

Create an E:\workShop\simplewar\src\wbNew.java file (bean in package)

package package1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class wbNew {
int i=0;
public int plus_one_Method() {

try
{
i=i+1;
return i;
} catch(Exception ex)
{
throw new RuntimeException(ex.getMessage());
}
}
}

 

Create an E:\workShop\simplewar\first.jsp file

<%@ page contentType="text/html;charset=WINDOWS-1252" session="true" %>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=WINDOWS-1252">

<TITLE>
Hello jsp and simple bean
</TITLE>
</HEAD>
<BODY>

<H3>The following output is from JSP code and simple bean :</H3>

<%!
int myReturn = 0;
%>

<jsp:useBean class="package1.wbNew" id="myID" scope="request" >

<%
myReturn = myID.plus_one_Method();
%>

</jsp:useBean>

<%

out.println("Hello simple Bean : " + myReturn + "<br>");
myReturn = myID.plus_one_Method();
out.println("Hello simple Bean : " + myReturn + "<br>");
myReturn = myID.plus_one_Method();
out.println("Hello simple Bean : " + myReturn + "<br>");

%>

<H3> The session ID is </H3>
<%= session.getId() %>


</BODY>
</HTML>

 

2.2 Create Servlet program

Create an E:\workShop\simplewar\src\ SimpleServlet.java file

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleServlet extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException

{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
String title = "Simple Servlet";
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<body>");
out.println("<h1>" + title + "</h1>");
out.println("</body>");
out.println("</html>");

}
}

 

Create an E:\workShop\simplewar\src\ package1\Servlet1.java file (Servlet in Package)

package package1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Servlet1 extends HttpServlet {

/**
* Initialize global variables
*/

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

/**
* Process the HTTP Get request
*/

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html; charset=WINDOWS-1252");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet1</title>");
out.println("<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=WINDOWS-1252\">");

out.println("</head>");
out.println("<body>Hello From Servlet1 doGet()");
out.println("</body></html>");
out.close();

}

/**
* Process the HTTP Post request
*/

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html; charset=WINDOWS-1252");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet1</title>");
out.println("<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=WINDOWS-1252\">");
out.println("</head>");
out.println("<body>");
out.println("</body></html>");
out.close();

}

/**
* Get Servlet information
* @return java.lang.String
*/

public String getServletInfo() {
return "package1.Servlet1 Information";
}

}

 

2.3 Create default index page for your Application

Create an E:\workShop\simplewar\Welcome.html file

<html>
<head>
<title>Assembling and deploying a simple Web application with a simple WAR file.</title>
</head>
<body>
<h3>A simple WAR file example.</h3>
<p><br><a href="servlet/SimpleServlet">Link to SimpleServlet</a>
<p><br><a href="servlet/Servlet1">Link to Package1.Servlet1</a>
<p><a href="first.jsp">Link to first.jsp file</a>
</body>
</html>

 

3. Complying all java source files

D:\oc4j\j2ee>set CLASSPATH=D:\oc4j\j2ee\home\ejb.jar;.;D:\oc4j\j2ee\home\orion.jar

D:\oc4j\j2ee>java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

D:\oc4j\j2ee>
E:\workShop\simplewar\src>"D:\Program Files\Oracle\JDeveloper 3.2.3\java1.2\bin\javac" -d ..\WEB-INF\classes\ *.java package1\*.java

E:\workShop\simplewar\src>

 

4.Create web.xml file for your WAR

Create E:\workShop\simplewar\WEB-INF\web.xml file

<?xml version="1.0" encoding="Cp1252"?>

<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_2.dtd'>

<web-app>
<display-name>A simple Web Application</display-name>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>SimpleServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>package1.Servlet1</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>Welcome.html</welcome-file>
</welcome-file-list>
</web-app>

 

ข้อมูลเพิ่มเติ่มของ file web.xml ได้จาก " http://www.orionserver.com/docs/web.xml.html "

 

5. Create WAR file for your application

E:\workShop\simplewar>jar cvf simplewar.war .
adding: src/ (in=0) (out=0) (stored 0%)
adding: src/SimpleServlet.java (in=822) (out=304) (deflated 63%)
adding: src/package1/ (in=0) (out=0) (stored 0%)
adding: src/package1/wbNew.java (in=332) (out=190) (deflated 42%)
adding: src/package1/Servlet1.java (in=1696) (out=486) (deflated 71%)
adding: WEB-INF/ (in=0) (out=0) (stored 0%)
adding: WEB-INF/classes/ (in=0) (out=0) (stored 0%)
adding: WEB-INF/classes/SimpleServlet.class (in=1106) (out=617) (deflated 44%)
adding: WEB-INF/classes/package1/ (in=0) (out=0) (stored 0%)
adding: WEB-INF/classes/package1/Servlet1.class (in=1402) (out=738) (deflated 47%)
adding: WEB-INF/classes/package1/wbNew.class (in=508) (out=346) (deflated 31%)
adding: WEB-INF/lib/ (in=0) (out=0) (stored 0%)
adding: WEB-INF/web.bak (in=602) (out=275) (deflated 54%)
adding: WEB-INF/web.xml (in=597) (out=278) (deflated 53%)
adding: first.bak (in=171) (out=117) (deflated 31%)
adding: first.jsp (in=811) (out=388) (deflated 52%)
adding: Welcome.bak (in=327) (out=163) (deflated 50%)
adding: Welcome.html (in=318) (out=161) (deflated 49%)

E:\workShop\simplewar>

 	  		

 

6.Deploying WAR file

E:\workShop\simplewar>copy simplewar.war d:\oc4j\j2ee\home\applications
1 file(s) copied.

 

7.Modify applicaton.xml

เพื่อให้ Containers รู้จัก web-module ของเรา จึงต้องทำการ เพิ่ม tag web-module ภายใน file d:\oc4j\j2ee\home\config\application.xml โดยระบุ web-module ชื่อ simpleware ไปยัง war file ที่อยู่ที่ d:\oc4j\j2ee\home\applications\simplewar.war

<web-module id="defaultWebApp" path="../default-web-app" />
<!-- start -->
<web-module id="simplewar" path="../applications/simplewar.war" />
<!-- end -->

 

ข้อมูลเพิ่มเติ่มของ file application.xml ได้จาก "http://www.orionserver.com/docs/application.xml.html "

 

8.Modify default-web-site.xml

เพื่อให้สามารถติดต่อ web-app ที่เราพัฒนาขึ้นโดยผ่าน default web site สิ่งที่เราต้องทำก็คือ modify file d:\oc4j\j2ee\home\config\default-web-site.xml โดยการเพิ่ม tag web-app ดังนี้

<web-app application="default" name="simplewar" root="/simplewar" />

 

ข้อมูลเพิ่มเติมของ file *web-site.xml ได้จาก "http://www.orionserver.com/docs/web-site.xml.html "

9.Start the Oracle9iAS Containers

D:\j2ee\home>java -jar orion.jar
Auto-unpacking D:\j2ee\home\applications\simplewar.war... done.
Oracle9iAS (1.0.2.2) Containers for J2EE initialized

 

10.Test the application with the following urls

10.1 Default homepage for your application

http://localhost:8888/simplewar

10.2 Jsp testing

http://localhost:8888/simplewar/first.jsp

10.3 Servlet testing

http://localhost:8888/simplewar/servlet/SimpleServlet
and
http://localhost:8888/simplewar/servlet/Servlet1

 

Note:

Complete Sample code:

References:

More Information:

Keywords:

OC4J orion J2EE Oracle Servlet JSP

 

 
 
{Exzilla.net -- e-development QuickStart --}



Copyright (c) 2001-2005 - Exzilla.net -  All Rights Reserved.
Contact Us | Privacy Policy | Terms & Conditions