Tuesday, August 01, 2006


Running TomCat 5.5 as Linux Daemon



Perform standard Tomat 5.5.X server install by account "tomcat".
Compile as officially advised in [1] $CATALINA_HOME/bin/jsvc binary :-



cd $CATALINA_HOME/bin
tar xvfz jsvc.tar.gz
cd jsvc-src
autoconf
./configure
make
cp jsvc ..
cd ..




To be succeed starting up TomCat's jsvc daemon with JDK 1.5
create as root script "/etc/init.d/TomCatJsvc",
compare it with daemon startup script suggested in [1]:-



JAVA_HOME='/home/tomcat/jdk1.5.0_06'
CATALINA_HOME='/home/tomcat/apache-tomcat-5.5.16'
CLASSPATH=$CATALINA_HOME/bin/bootstrap.jar:$CATALINA_HOME/bin/commons-daemon.jar:$JAVA_HOME/lib/tools.jar
TOMCAT_USER=tomcat
TMPDIR=/var/tmp


RC=0

case "$1" in

start)

$CATALINA_HOME/bin/jsvc -user $TOMCAT_USER -home $JAVA_HOME -Dcatalina.home=$CATALINA_HOME -Djava.io.tmpdir=$TMPDIR -Djava.awt.headless=true -outfile $CATALINA_HOME/logs/catalina.out -errfile $CATALINA_HOME/logs/catalina.err -cp $CLASSPATH org.apache.catalina.startup.Bootstrap

RC=$?

[ $RC = 0 ] && touch /var/lock/subsys/tomcat
;;

stop)

PID=`cat /var/run/jsvc.pid`
kill $PID

RC=$?

[ $RC = 0 ] && rm -f /var/lock/subsys/tomcat /var/run/jsvc.pid
;;

*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit $RC




Then run as root:-



# /etc/init.d/TomCatJsvc start




If "ps -ef|grep tomcat" output looks like:-




and $CATALINA_HOME/logs/catalina.out:-





then you should be fine with launching browser to http://localhost:8080.

For automatic startup/shutdown TomCat's daemon create 3 symbolic links:-



# ln -s /etc/init.d/TomCatJsvc /etc/rc5.d/S99TomCatJsvc
# ln -s /etc/init.d/TomCatJsvc /etc/rc0.d/K09TomCatJsvc
# ln -s /etc/init.d/TomCatJsvc /etc/rc6.d/K09TomCatJsvc




References

1.http://tomcat.apache.org/tomcat-5.5-doc/setup.html


Sunday, July 30, 2006


Advanced Configuration - Multiple Tomcat 5.5 Instances on Linux



Create account tomuser as in [1] with .bash_profile following bellow.

Export new environment variable CATALINA_BASE pointing for example to ~tomuser.



# su - tomuser
$pwd
/home/tomuser
$cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
export CATALINA_BASE=~tomuser
export CATALINA_HOME=/home/tomcat/apache-tomcat-5.5.16
export JAVA_HOME=/home/tomcat/jdk1.5.0_06
export ANT_HOME=/home/tomcat/apache-ant-1.6.5
PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
export PATH
export CLASSPATH=.:..:../..:$CATALINA_HOME/common/lib/servlet-api.jar:$CATALINA_HOME/common/lib/jsp-api.jar:
$CATALINA_HOME/common/lib/naming-factory-dbcp.jar
export DISPLAY=:0.0
unset USERNAME




Then:-



$cp -R $CATALINA_HOME/conf .
$cp -R $CATALINA_HOME/webapps .
$cp -R $CATALINA_HOME/shared .
$cp -R $CATALINA_HOME/work .
$cp -R $CATALINA_HOME/temp .
$cp -R $CATALINA_HOME/logs .
$cd /home/tomuser/conf/Catalina/localhost




Modify 3 files admin.xml,host-manager.xml,manager.xml.

Context should contain new value for docBase.

In particular manager.xml should look like:-



<Context docBase="/home/tomuser/server/webapps/manager"
privileged="true" antiResourceLocking="false" antiJARLocking="false">

<!-- Link to the user database we will get roles from -->
<ResourceLink name="users" global="UserDatabase"
type="org.apache.catalina.UserDatabase"/>
</Context>




Next:-



$cp -R $CATALINA_HOME/server .
$cd conf




Modify server.xml. Three ports values should be changed:-



<Server port="8015" shutdown="SHUTDOWN">

.......

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8090" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />

.......

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8019"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />




To start new TomCat instance:-



$ $CATALINA_HOME/bin/startup.sh -Dcatalina.base=$CATALINA_BASE




Screenshot of $CATALINA_BASE/logs/catalina.out:-





Screenshot of "admin" report:-




To perform deployment with "ant" to new instance of TomCat :-



$ cd apache-tomcat-5.5.16-deployer




Make one change to build.xml,replacing old port value for TomCat by new one.



<!-- Configure properties to access the Manager application -->
<property name="url" value="http://localhost:8090/manager"/>
<property name="username" value="tomcat"/>
<property name="password" value="tomcat"/>\>




References



1.http://bderzhavets.blogspot.com/2006/07/installation-tomcat5.html


Tuesday, July 25, 2006


Installation TomCat(5.5.17) Client Deployer on Linux(CentOS 4.2)



First :-



# chmod g+r ~tomcat
# chmod g+x ~tomcat




In case when TCD is supposed to be installed on the same host
with server have "ant" installed by tomcat,where tomcat is account used for server install,otherwhise (after creation account tomuser):



1.Perform installation of JDK 1.5 and "ant" by tomuser.
2.Just for example, make assignments:-
ANT_HOME=~tomuser/apache-ant-1.6.5
JAVA_HOME=~tomuser/jdk1.5.0_06
Then modify PATH as follows:
export PATH=$ANT_HOME/bin:$JAVA_HOME/bin:$PATH




Create account tomuser :



# adduser tomuser -g users




with .bash_profile for local deployment



# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

export JAVA_HOME=~tomcat/jdk1.5.0_06
export ANT_HOME=~tomcat/apache-ant-1.6.5
PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
export PATH
export DISPLAY=:0.0
unset USERNAME




or with .bash_profile for remote deployment



# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

export JAVA_HOME=~tomuser/jdk1.5.0_06
export ANT_HOME=~tomuser/apache-ant-1.6.5
PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
export PATH
export DISPLAY=:0.0
unset USERNAME




Login as tomuser and untar deployer package.

Then change directory to ~tomuser/apache-tomcat-5.5.17-deployer.

Create file deployer.properties under /home/tomuser/apache-tomcat-5.5.17-deployer

with content:-



name="compile.debug" value="true"
name="compile.deprecation" value="false"
name="compile.optimize" value="true"




Modify two rows in build.xml:-



<property name="webapp" value="bunk-support"/>
<property name="path" value="/bank-support"/>




In case when TomCat Server is running on the remote host with
IP address: IP-Address and name: Host_Name ,entry
"http://localhost:8080/manager" in build.xml
should be replaced by "http://IP-Address:8080/manager" or by "http://Host_Name:8080/manager" .

Create bank-support directory and download source for chapter 15
of Marty Hall's "Core Servlets and JavaServer Pages" book (second edition,v 1). Create file ./WEB-INF/web.xml:-


Then compile with "javac" Bean Class BankCustomer.java under WEB-INF/classes before calling "ant".
Servlet ShowBalance.java would be better
to compile during "ant" build phase.

Jasper Compiler will be also invoked to compile all JSPs.


Snapshot of source directory:-


Snapshot of target directory:-


Thursday, July 06, 2006


Refreshing Master-Detail JSF page after inserting
or deleting row from Detail table (JDeveloper 10.1.3)



Pick up as a sample jpauser schema from [1].
Add one more view to jpauser schema :-



create view OrderView as select * from order_table;




Add method findOrderView(Double) to JPRSFacade Session EJB based
on same TopLink POJOS as in [1] plus Orderview.java




Create Master-Detail page step by step:-










Add code generated by "findOrderView" to code generated by"removeEntity" button.

Save new code for"removeEntity" button into JSF managed bean
for current page.




Now we are done with refresh after delete.


Create page for data entry and bind "persistEntity" at this page.




Then implement refreshing of main page after inserting new record into detail table:

1.Open the browse page and right click in the visual editor. Choose Go To Page Definition.

2.In the Structure window, expand the highest level node. Right click the executables node and choose

Insert inside executables -> invokeAction

3.In the Common Properties tab, specify "tableRefresh" as the Id for the action and choose your detail-table query method name : findOrderView in the Binds dropdown list.

4.Click the Advanced Properties tab

Choose ifNeeded as the Refresh property and to ensure the action is called each time the page is rendered.

Enter ${!adfFacesContext.postback} as the RefreshCondition and click OK.





Project has been deployed to standalone OC4J instance

and behaved as expected

References



1.http://bderzhavets.blogspot.com/2006/05/toplink-jpa-inside-ejb-3.html


Monday, June 12, 2006


Automatic Startup Shutdown Oracle 10g R2 (10.2.0.1)
database utilizing ASM on CentOS 4.3 (RHEL AS 4 U 3)



In general we follow Jeff Hunter [1].However, scripts starting ASM databases are not just like in [1] due to different ORACLE_HOME's directories for ASM instance and database utilizing ASM in case of 10g R2 version of server. In 10.2.0.1 case script /etc/init.d/dbora doesn't export any ORACLE_HOME value.
It just runs script in ~oracle directory responsible for export right ORACLE_HOME value at right time
1. Follow [1] and make changes to /etc/inittab



Original /etc/inittab file:



(...)
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
(...)
h1:35:respawn:/etc/init.d/init.cssd run >/dev/null 2>&1 </dev/null




Modified /etc/inittab file:



(...)
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
h1:35:respawn:/etc/init.d/init.cssd run >/dev/null 2>&1 </dev/null
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
(...)




2.Create file /etc/init.d/dbora:



SLEEP_TIME=180
ORACLE_OWNER=oracle

case "$1" in
start)
sleep $SLEEP_TIME
su - $ORACLE_OWNER -c "/home/oracle/dbStart"
touch /var/lock/subsys/dbora
;;

stop)
su - $ORACLE_OWNER -c "/home/oracle/dbStop"
rm -f /var/lock/subsys/dbora
;;

*)
echo $"Usage: $prog {start|stop}"
exit 1
esac
exit




3.Script ~oracle/dbStart:



export ORACLE_HOME=/u01/app/oracle/product/10.2.0/ASM
lsnrctl start
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_2
lsnrctl start
$ORACLE_HOME/bin/dbstart
$ORACLE_HOME/bin/isqlplusctl start
$ORACLE_HOME/bin/emctl start dbconsole




4.Script ~oracle/dbStop:



export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_2
$ORACLE_HOME/bin/isqlplusctl stop
$ORACLE_HOME/bin/emctl stop dbconsole
$ORACLE_HOME/bin/dbshut
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_2
lsnrctl stop
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/ASM
lsnrctl stop




5.Creating symbolic links for /etc/init.d/dbora:



# ln -s /etc/init.d/dbora /etc/rc5.d/S99dbora
# ln -s /etc/init.d/dbora /etc/rc0.d/K10dbora
# ln -s /etc/init.d/dbora /etc/rc6.d/K10dbora




6. System restart.


References.


"Installing Oracle10g Release 1 (10.1.0) on
Linux-(RHEL 4)" at http://www.idevelopment.info
by Jeff Hunter

Saturday, June 03, 2006


One more independent opinion regarding discussed issue.


At mean time on OTN front page you may view:-








Friday, June 02, 2006


"Searching for Timm Hall's Quote"


Searching for "ASM and the database can share a single Oracle home"




Searching for just "single"




Searching for quote mentioned by myself


Tuesday, May 30, 2006


TopLink JPA inside EJB 3.0 Container (JDeveloper 10.1.3)



Application designed satisfies same business requirements
as [1] and doesn't contain any hands written code line
for creating new orders and editing existing.
Deleting orders from Master-Detail page requieres modifying of
default method embedded into JSF managed Bean.


Import jpauser schema created in [2] in JDeveloper 10.1.3
environment. Tune Jdeveloper 10.1.3 environment to complete the task and generate Session EJB 3.0 for TopLink POJOs imported from jpauser schema.





Create all JSF pages (as *.jspx files backing up UI to Managed Beans),invoke method persistEntity(Object entity) for "Creation order" page (for particular item) , invoke method mergeEntity(Object entity) for "Editing order" page (for particular item)

Create table form has been used for data input.
Method persistEntity() refreshes target screen automatically.



Drag and drop persistEntity(Object) from Data Controls into
JSP area covering button "Submit".


Next step - edit action binding for the "persistEntity" button:




Same action should be performed for "order editing"
page regarding mergeEntity(Object entity) method and editing action binding for corresponding button.
Drag and drop "Delete" button of OrderTable from Data Controls:





Action invoked by "Delete" button (manual intervention):





Code details:


public String commandButton9_action() {

// Delete row from table

BindingContainer bindings1 = getBindings();
OperationBinding operationBinding1 =
bindings1.getOperationBinding("removeEntity");
Object result1 = operationBinding1.execute();
if (!operationBinding1.getErrors().isEmpty()) {
return null;
}

// Refresh detail table screen view

OperationBinding operationBinding =
bindings.getOperationBinding("Delete");
Object result = operationBinding.execute();
if (!operationBinding.getErrors().isEmpty()) {
return null;
}
return null;
}



As of June 21 2006 I found out that described method works only in
Jdeveloper 10.1.3 environment, project stops working properly even been deployed to standalone OC4J instance shipped with Jdeveloper
The reason of mistake is restarting of embedded OC4J instance with apps every time when apps restarts. Otherwise Master – Detail page won’t be properly refreshed after joint invocation removeEntity() & Delete.

View workaround for mentioned issue:-

Refreshing Master-Detail JSF page after inserting or deleting row from Detail table

Runtime screenshots:

Main page:


Orders for particular item:




Creating new order:




References.
1.http://www.oracle.com/technology/products/ias/toplink/jpa/tutorials/jsf-jpa-tutorial.html
2.http://bderzhavets.blogspot.com/2006/05/build-web-application-jsf-using-jpa.html

Friday, May 19, 2006


Build a Web Application (JSF) Using JPA (OTN's Tutorial)



Performing steps advised by :

JPA tutorial at OTN

I was experiencing the only one issue with command

$ ant -f build.xml generate-tables

and had to create schema through SQLPLUS


CREATE TABLE INVENTORY (COST NUMBER(19,4) NULL, PRICE NUMBER(19,4) NULL,
QUANTITY NUMBER(10) NULL, VERSION NUMBER(10) NULL,
ITEM_SKU NUMBER(19) NOT NULL, PRIMARY KEY (ITEM_SKU));

CREATE TABLE ORDER_TABLE (ORDERID NUMBER(19) NOT NULL,
CURRENTLOCATION VARCHAR2(255) NULL, QUANTITY NUMBER(10) NULL,
ARRIVALDATE DATE NULL, VERSION NUMBER(10) NULL,
ORDERINITIATED DATE NULL, ITEM_SKU NUMBER(19) NULL, PRIMARY KEY (ORDERID));

CREATE TABLE ITEM (SKU NUMBER(19) NOT NULL, NAME VARCHAR2(255) NULL,
CATEGORY VARCHAR2(255) NULL, VERSION NUMBER(10) NULL,
DESCRIPTION VARCHAR2(255) NULL, PRIMARY KEY (SKU));

ALTER TABLE INVENTORY ADD CONSTRAINT FK_INVENTORY_ITEM_SKU FOREIGN KEY (ITEM_SKU)
REFERENCES ITEM (SKU);

ALTER TABLE ORDER_TABLE ADD CONSTRAINT FK_ORDER_TABLE_ITEM_SKU FOREIGN KEY (ITEM_SKU)
REFERENCES ITEM (SKU);

CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR2(50) NOT NULL,
SEQ_COUNT NUMBER(38) NULL, PRIMARY KEY (SEQ_NAME));

INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0);
COMMIT;






As was advised by Gordon Yorke at OTN Toplink Forum (05/25/06)



glassfish-persistence-installer-v2-b01.jar was replaced by glassfish-persistence-installer-v2-b03.jar

The replacement resolves the issue.

It also runs fine with the same oracle database credentials simply recreating the schema.

Actually , sequence of builds:-



ant -f build.xml generate-tables
ant -f build.xml populate-data
ant -f build.xml package.webapp




may run recreating schema, repopulating data and regenerating war-file at any time.

The JSF application using JPA was successfully deployed to TomCat 5.5.16 on CentOS 4.2 box
hosting Oracle 10gR2 database.

Deployment Web Application using TomCat's Application Manager :-


Completed OK.



The manual mentioned above shows the basic steps of developing, packaging, and

deploying a Web application using the EJB 3.0 Java Persistence API (JPA).

In this application, a Java Server Faces (JSF) presentation layer will make

use of JPA for persistence outside of the EJB 3.0 container.

Runtime screenshots follows bellow:-








Sunday, April 30, 2006


Usage Datasource(JNDI API) for Oracle 10gR2 in TomCat 5.5.17 (on Linux).



Place into ./WEB-INF/lib/ file "ojdbc14.jar"

Create following directory structure and tune:-

files (./WEB-INF/web.xml & ./META-INF/context.xml) as follows

[tomcat@ServerRHL Jdevdb]$ ls -CR



.:
OraDataSrc.war index.html META-INF WEB-INF

./META-INF:
context.xml

./WEB-INF:
classes src web.xml

./WEB-INF/classes:
coreservlets

./WEB-INF/classes/coreservlets:
DbServlet.class

./WEB-INF/src:
DbServlet.java




*******************************
File ./META-INF/context.xml
*******************************



<Context debug="0" docBase="OraDataSrc" path="/OraDataSrc" reloadable="true">
<Resource name="jdbc/oracle10g"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@ServerRHL.informatics.dstu.net:1521:jdvs"
username="hr"
password="hr"
maxActive="100"
maxIdle="10"/>
</Context>




*************************
File ./WEB-INF/web.xml
*************************



<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/oracle10g</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>DbServlet</servlet-name>
<servlet-class>coreservlets.DbServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DbServlet</servlet-name>
<url-pattern>/servlet/coreservlets.DbServlet</url-pattern>
</servlet-mapping>
</web-app>




***********************************
File ./WEB-INF/src/DbServlet.java
***********************************


View [1] chapter 21.3


Then compile:-



[tomcat@ServerRHL src]$ javac -d /home/tomcat/Jdevdb/WEB-INF/classes DbServlet.java



Create OneOut.html and have index.jsp forward to this form:-



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">
<HTML>
<HEAD><TITLE>Servlet Connection Pooling: A Test</TITLE></HEAD>
<FRAMESET ROWS="*" BORDER=0 FRAMEBORDER=0 FRAMESPACING=0>
<FRAMESET COLS="*">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
</FRAMESET>
</HTML>




Then:-



[tomcat@ServerRHL Jdevdb]$ ls -l

total 32

-rw-rw-r-- 1 tomcat tomcat 351 Apr 29 13:45 index.jsp

-rw-rw-r-- 1 tomcat tomcat 351 Apr 29 13:45 OneOut.html

drwxrwxr-x 2 tomcat tomcat 4096 Apr 29 13:23 META-INF

drwxr-xr-x 4 tomcat tomcat 4096 Apr 29 13:31 WEB-INF



[tomcat@ServerRHL Jdevdb]$ jar cvf OraDataSrc.war .



Deploy OraDataSrc.war and run.



Have index.jsp forward to MultipleOut.html form:-



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">
<HTML>
<HEAD><TITLE>Servlet Connection Pooling A Test</TITLE></HEAD>
<!-- Causes 25 near simultaneous requests for same servlet. -->
<FRAMESET ROWS="*,*,*,*,*" BORDER=0 FRAMEBORDER=0 FRAMESPACING=0>
<FRAMESET COLS="*,*,*,*,*">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
</FRAMESET>
<FRAMESET COLS="*,*,*,*,*">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
</FRAMESET>
<FRAMESET COLS="*,*,*,*,*">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
</FRAMESET>
<FRAMESET COLS="*,*,*,*,*">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
</FRAMESET>
<FRAMESET COLS="*,*,*,*,*">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
<FRAME SRC="/OraDataSrc/servlet/coreservlets.DbServlet">
</FRAMESET>
</FRAMESET>
</HTML>




Redeploy application and run






1. One More Servlet,creating input form by itself, to test :-



package coreservlets;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DatabaseServlet extends HttpServlet {
DataSource datasrc;
public void init() throws ServletException {
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
datasrc = (DataSource) envCtx.lookup ("jdbc/oracle10g");
if (datasrc == null)
throw new ServletException ("'Oracle10g' is unknown DataSource");
} catch (NamingException ne) { throw new ServletException(ne.getMessage());
} //try
}


public void doGet (HttpServletRequest request,HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
response.setContentType ("text/html");
java.io.PrintWriter out = response.getWriter();

out.println ("<html><head>");
out.println ("<title>Help page</title></head><body>");
out.println ("<h2>Please submit SQL Query </h2>");
out.println ("<form method=\"post\" action=\"" + request.getContextPath() +
"/servlet/coreservlets.DatabaseServlet\">");
out.println ("<table border=\"1\"><tr> <td valign=\"top\">");
out.println ("</td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"sqlstring\" size=\"60\">");
out.println("</td></tr>");
out.println("</table><form>");
out.println("<p>");
out.println("<input type=\"submit\" value=\"Submit Query\">");
out.println("</body></html>");
} // doGet

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

String sql = request.getParameter("sqlstring");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ResultSetMetaData rsm = null;
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head><title> Servlet Database Access </title></head><body>");
out.println("<h4> Database Info </h4>");
out.println("<table border = \"1\"> <tr>");
try {
conn = datasrc.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
rsm = rs.getMetaData();
int colCount = rsm.getColumnCount();
for (int i = 1; i <= colCount; ++i) {
out.println("<th>" + rsm.getColumnName(i) + "</th>");
}
out.println("</tr>");
while (rs.next()) {
for (int i = 1; i <= colCount; ++i)
out.println("<td>" + rs.getString(i) + "</td>");
out.println("</tr>");
}
} catch (Exception e) {
throw new ServletException(e.getMessage());
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {}
} //finally
out.println("</table><br><br>");
out.println("</body>");
out.println("</html>");
} //doPost
}




HTML form to invoke DatabaseServlet:-



<HTML>
<BODY>
<a href="servlet/DatabaseServlet" >Run Database Servlet </a>
</BODY>
</HTML>




File ./WEB-INF/web.xml should also have "DbServlet" replaced by "DatabaseServlet".



2. JSP example - OraData.jsp :-



<html>
<head>
<%@ page errorPage="errorpg.jsp"
import="java.sql.*,
javax.naming.Context,
javax.naming.InitialContext,
javax.naming.NamingException,
javax.sql.*,
javax.servlet.*,
javax.servlet.http.*" %>
</head>
<body>
<%!
DataSource datasrc ;
public void _jspInit() {
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
datasrc = (DataSource) envCtx.lookup ("jdbc/oracle10g");
} catch (NamingException ne) {ne.getMessage();}
}
%>

<%
Connection conn = datasrc.getConnection();
Statement stmt = conn.createStatement();
String sqlquery = request.getParameter("sqlquery");
ResultSet rs = stmt.executeQuery(sqlquery);
ResultSetMetaData rsm = rs.getMetaData();
int colCount = rsm.getColumnCount();
%>
<table border = \"1\"> <tr>
<% for (int i = 1; i <= colCount; ++i) { %>
<th> <%= rsm.getColumnName(i) %> </th>
<% } %>
</tr>
<tr>
<% while (rs.next()) {
for (int j = 1; j <= colCount; ++j) { %>
<td> <%= rs.getString(j) %> </td>
<% } %>
</tr>
<% }
stmt.close();
conn.close();
%>
</table>
</body>
</html>




HTML form to run OraData.jsp:-



<html>
<head>
<title>Help page</title>
</head>
<body>
<h2>Please submit SQL Query </h2>
<form method="get" action="/OraDataSrc/OraData.jsp">
<table border="1">
<tr> <td valign="top">
<input type="text" name="sqlquery" size="60">
</td></tr>
</table></form>
<p>
<input type="submit" value="Submit Query">
</body>
</html>




$ ant -f build.xml


Servlet OraData_jsp.java generated by "ant":-



package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public final class OrclData_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {


DataSource datasrc ;

public void _jspInit() {
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
datasrc = (DataSource) envCtx.lookup ("jdbc/oracle10g");
} catch (NamingException ne) {ne.getMessage();
}
}

private static java.util.List _jspx_dependants;

public Object getDependants() {
return _jspx_dependants;
}

public void _jspService (HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;


try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
"errorpg.jsp", true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write("<html>\n<head>\n\n</head>\n<body>\n");
out.write(' ');
out.write('\n');

Connection conn = datasrc.getConnection();
Statement stmt = conn.createStatement();
String sqlquery = request.getParameter("sqlquery");
ResultSet rs = stmt.executeQuery(sqlquery);
ResultSetMetaData rsm = rs.getMetaData();
int colCount = rsm.getColumnCount();

out.write("\n<table border = \\\"1\\\"> <tr>\n");
for (int i = 1; i <= colCount; ++i) {
out.write("\n<th> ");
out.print( rsm.getColumnName(i) );
out.write(" </th>\n");
}
out.write("\n</tr>\n<tr>\n");
while (rs.next()) {
for (int j = 1; j <= colCount; ++j) {
out.write("\n<td> ");
out.print( rs.getString(j) );
out.write(" </td>\n");
}
out.write("\n</tr>\n");
}
stmt.close();
conn.close();

out.write("\n</table>\n</body>\n</html>\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}




Web application been compiled by "ant" files layout:-






$ ant -f build.xml deploy


Then launch browser to "https://localhost:8443"
and run application from Manager Screen.







References.

1.Java Servlet and JSP Cookbook. Bruce W. Perry, O'REILLY 2004

2.Core Servlets and Java Server Pages,Marty Hall,Sun Press 2001