Helpful Information
 
 
Category: DB2 Development
JasperException trying to connect to DB2 via jsp

Hi,

i'm trying to run a jsp in order to test a connection with DB2
I'm using :
tomcat 4.1.24
jsdk1.4.1_01
DB2 8.1


here is the jsp :
<html>
<body>
<%try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
%>
Contexte JNDI : <%=ctx%><br>
<% javax.sql.DataSource ds =
(javax.sql.DataSource)ctx.lookup("java:comp/env/jdbc/masource"); %>
DataSource : <%=ds%><br>
<% java.sql.Connection connection = ds.getConnection(); %>
Connexion : <%=connection%><br>
<%
connection.close();
} catch (Exception e) {
%>
Error : <%=e%>
<%
}
%>
</body>
</html>
----end of the jsp

when i run the jsp, here is the Exception i get :

org.apache.jasper.JasperException NoAllocEnv
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.jav a:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:536)

-----end of the stack trace

with no "root cause"

I know this stack trace doesn't mean very much and that's why i don't understand what's wrong

i'm trying to run a jsp in order to test a connection with DB2I can't tell from the stack trace what the problem is either. Specifically, I can't tell if the problem is with DB2, Tomcat or your code. My first question would be have you tested your connection from a command prompt yet? In other words if you're running this on a *nix box, have you logged in as the instance owner and run the command:

db2 CONNECT TO database USER userid USING password

Do you connect okay? Once we determine if your connection / environment is setup correctly, then we can look at other things that might be causing this problem.

ALso, you may want to see what can throw an exception and put some try/catch blocks in there. For instance, the InitialContext.lookup() method throws a NamingException. If you do this you may be better able to track down which piece is causing your problem.

Thanks for your responses,
The database is fine, i can connect, create table etc... from db2 command line
Tomcat works fine too : i manage to deploy a war and view some simple jsp.

I tried to run a simple java binary :

============================
import java.sql.*;

class Test1 {

public static void main (String[] args) {
try {

// Step 1: Load the JDBC driver.
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

// Step 2: Establish the connection to the database.
String url = "jdbc:db2:foodmart";
Connection conn = DriverManager.getConnection(url,"user1","password");

} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
===========================

That code works fine but when i tried to run the jsp equivalent, i got the same exception than before (SQLAllocEnv)

I watch on the db2 documentation to know what is it (http://www-306.ibm.com/cgi-bin/db2www/data/db2/udb/winos2unix/support/document.d2w/report?fn=db2v7l0sqll1302.htm) but i really don't know how i can interfere with that or make something to correct the problem.

Also, my OS is windows2k.

the problem is not jndi so i didn't try to catch a NamingException

Thank you for your help

Well lets try looking at this a little different.
What version of DB2 are you running?
Have you tried to install the latest patches for that version yet?

The version of DB2 is 8.1.0 and there is no patch installed.

I didn't find any patch or update that could correct the problem, but maybe I didn't search well...

Regards

Hi,

i have tried another way to run the jsp under tomcat :
The product of my company have is own tomcat(4.1.18), which is well configured with db2. So, i tried to run my jsp under this tomcat and it's work. but when i tried to run another jsp to connect database using jndi, it failed (the source code is very basic and correct)

It's because the web.xml of my webapp don't use the same tags-set (those of servlet 2.3) than what is instaled in tomcat 4.1.18 (servlet 2.2). So those tags aren't recognized : <filter>, <filter-mapping>, <listener>

So, it's definitively a problem in the configuration of tomcat, but I can't understand what's wrong.


Maybe should i repost this question in another forum ?

It is either a tomcat configuration error or a jndi error. Catching the exceptions thrown like I suggested may have helped track this down.

Since you have narrowed it down to a java problem, I would suggest posting this question in the java forum.

Thank you for your response,

I have found what's wrong : i feel a little miserablous.... In fact, it was the driver db2java.zip which was corrupted.

I have replaced it and now, it works fine

Regards

Hi,

I also have the project on jsp with DB2. Could you please explain what are the softwares needed to connect to DB2 and also please send your JSP code for connection.

Now I have Apache Tomcat 5.5. Should I download db2jdbc driver ?

Thanks in advance,
Sreedhar

i have been trying to connect the netbeans 3.5.1 with DB2 version 8.1 by establihsing a connection through the db2 driver "COM.ibm.db2.jdbc.app.DB2Driver".
the connection is established, the code is compilng without errors, but on execution its not giving any output.

the jsp code i wrote is:-
[code]
<%
try
{
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Connection con=DriverManager.getConnection("jdbc:db2:db");
Statement stmt=con.createStatement();
ResultSet rs= stmt.executeQuery("select * from st");
while(rs.next())
{ out.println(rs.getInt(1));
}
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}

catch(SQLException ex)
{
System.out.println(ex);
}
catch(Exception e)
{ out.println(e);
}
%>
where "db" is the database name & "st" is table name

can someone plz help me wid my problem???










privacy (GDPR)