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