Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

error using JSP, Javabeans, and Tomcat

Thread view: 
Olinga K. Abbott - 12 Jan 2006 21:01 GMT
I'm having difficult getting JSP to work with Javabeans inside Tomcat.
I've been working on this for awhile, and I have seen some examples,
but all the examples I found (like "sample" included with Tomcat)
explain JSP and servlets but not Javabeans. I have not been able to get
a complete picture of the proper configuration, even after reading the
Tomcat docs.

My JSP looks like this:
===================================================================
<%!
    String pageName = "progsoftware.com";
%>

<html>
<head>
    <title><%= pageName %></title>
    <jsp:useBean class="dbConnect.dbConnect" id="bean0" scope="page" />
</head>
<body>
<h1>Welcome to <%= pageName %></h1>

</body>
</html>
===================================================================
Depending on what I place in the "class" attribute I get different
error messages:

1) "dbConnect.dbConnect":
=====================================================
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 8 in the jsp file: /learn.jsp
Generated servlet error:
dbConnect cannot be resolved to a type

An error occurred at line: 8 in the jsp file: /learn.jsp
Generated servlet error:
dbConnect cannot be resolved

An error occurred at line: 8 in the jsp file: /learn.jsp
Generated servlet error:
dbConnect cannot be resolved to a type

    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:409)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
=====================================================================
2) "dbConnect":
====================================================================
javax.servlet.ServletException: dbConnect (wrong name:
dbConnect/dbConnect)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:272)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NoClassDefFoundError: dbConnect (wrong name:
dbConnect/dbConnect)
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClass(Unknown Source)
    java.security.SecureClassLoader.defineClass(Unknown Source)
    java.net.URLClassLoader.defineClass(Unknown Source)
    java.net.URLClassLoader.access$100(Unknown Source)
    java.net.URLClassLoader$1.run(Unknown Source)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(Unknown Source)
    java.lang.ClassLoader.loadClass(Unknown Source)
    java.lang.ClassLoader.loadClass(Unknown Source)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1325)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1187)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1210)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3284)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:189)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
======================================================================
3) "asdf" (random string):
======================================================================
org.apache.jasper.JasperException: /learn.jsp(8,1) The value for the
useBean class attribute asdf is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:146)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1223)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3284)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:189)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
=======================================================================
Interestingly, sometimes when I do a reload in my browsers (Firefox and
IE) one of the previous error messages are returned. This appears at
random, and the error messages will interleave merely by refreshing. It
appears that Tomcat inappropriately caches.

My directory structure is
=======================================================================
Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware

01/12/2006  01:49 PM    <DIR>          .
01/12/2006  01:49 PM    <DIR>          ..
01/12/2006  11:43 AM               317 .classpath
01/12/2006  06:29 AM               388 .project
01/12/2006  01:42 PM    <DIR>          .settings
01/12/2006  02:11 PM               228 learn.jsp
01/12/2006  01:42 PM    <DIR>          source
01/12/2006  01:42 PM    <DIR>          WEB-INF
01/12/2006  10:51 AM               287 web.xml
              4 File(s)          1,220 bytes

Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware\.settings

01/12/2006  01:42 PM    <DIR>          .
01/12/2006  01:42 PM    <DIR>          ..
01/12/2006  06:29 AM               330 org.eclipse.jdt.core.prefs
              1 File(s)            330 bytes

Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware\source

01/12/2006  01:42 PM    <DIR>          .
01/12/2006  01:42 PM    <DIR>          ..
01/12/2006  01:42 PM    <DIR>          dbConnect
              0 File(s)              0 bytes

Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware\source\dbConnect

01/12/2006  01:42 PM    <DIR>          .
01/12/2006  01:42 PM    <DIR>          ..
01/12/2006  12:45 PM             1,247 dbConnect.java
              1 File(s)          1,247 bytes

Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware\WEB-INF

01/12/2006  01:42 PM    <DIR>          .
01/12/2006  01:42 PM    <DIR>          ..
01/12/2006  01:42 PM    <DIR>          classes
01/12/2006  01:42 PM    <DIR>          lib
              0 File(s)              0 bytes

Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware\WEB-INF\classes

01/12/2006  01:42 PM    <DIR>          .
01/12/2006  01:42 PM    <DIR>          ..
01/12/2006  01:42 PM    <DIR>          dbConnect
              0 File(s)              0 bytes

Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware\WEB-INF\classes\dbConnect

01/12/2006  01:42 PM    <DIR>          .
01/12/2006  01:42 PM    <DIR>          ..
01/12/2006  12:45 PM             1,918 dbConnect.class
              1 File(s)          1,918 bytes

Directory of C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware\WEB-INF\lib

01/12/2006  01:42 PM    <DIR>          .
01/12/2006  01:42 PM    <DIR>          ..
              0 File(s)              0 bytes
===================================================================
Following are the contents of some relevant files
dbConnect.java (the class file is in the webapps\progsoftware folder,
and the java file is in a "workspace" folder outside the Tomcat
hierarchy.)
======================================================================
package dbConnect;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.lang.ClassNotFoundException;
public class dbConnect
{
    private Connection con;
    private ResultSet rs;
    public dbConnect(){} //added after reading that beans require default
constructors
    public void DBConnect(String dbName, String username, String password)
    throws ClassNotFoundException,SQLException
    {
        Class.forName("org.postgresql.Driver");
        con =
DriverManager.getConnection("jdbc:postgresql:postgres",username,password);
    }
    public void execStmt(String queryString)
    throws SQLException
    {
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(queryString);
    }
    public void closeConnection()
    throws SQLException
    {
        con.close();
    }
    public dbConnect(String dbName, String username, String password,
String queryString)
    throws ClassNotFoundException,SQLException
    {
        DBConnect(dbName,username,password);
        execStmt(queryString);
        closeConnection();
    }
    public dbConnect(String dbName, String username, String password)
    throws ClassNotFoundException,SQLException
    {
        DBConnect(dbName,username,password);
    }
    public ResultSet getResultSet()
    {
        return rs;
    }
}
===================================================================
web.xml (I'm under the impression that it only needs to be filled when
running servlets, which I'm not)
=======================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
</web-app>
=====================================================================
Because of its length, I'm not posting the complete contents of
conf\server.xml, but I only added one line which I read was required:
<Context path="C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\progsoftware" docBase="progsoftware" debug="0"
reloadable="true" />
This is just before </host>. The source I was reading
(http://www.onjava.com/pub/a/onjava/2001/04/19/tomcat.html?page=2) said
to add the line but did not state where to place it.
I'm wondering though if this is only required when using servelets.

I don't think I have changed any other files.

Software versions:
Tomcat 5.5.12
Eclipse: 3.1.1
OS: WinXP sp 2
JDK: 1.5.0_06
rkm - 13 Jan 2006 13:45 GMT
You say you aren't using servlets, yet you got an error message saying:
Generated servlet error:

so you apparently _are_ using servlets, so you would need an entry in
web.xml that instructs the server to instantiate your dbConnect class on
demand (or at startup, as you prefer). My experience is with JSF, so I
do those things in the JSF.xml file.  Your class="dbConnect.dbConnect"
value is the correct one.

Rick
Andrea Desole - 13 Jan 2006 14:56 GMT
> I'm having difficult getting JSP to work with Javabeans inside Tomcat.
> I've been working on this for awhile, and I have seen some examples,
[quoted text clipped - 19 lines]
> </body>
> </html>

looks correct

> ===================================================================
> web.xml (I'm under the impression that it only needs to be filled when
> running servlets, which I'm not)

not really, but it doesn't matter

> Because of its length, I'm not posting the complete contents of
> conf\server.xml, but I only added one line which I read was required:
> <Context path="C:\Program Files\Apache Software Foundation\Tomcat
> 5.5\webapps\progsoftware" docBase="progsoftware" debug="0"
> reloadable="true" />

this is probably not correct. Also, if the application is in the webapps
directory it's not necessary. Read the Tomcat documentation for more
information
I'm not sure this is the cause of the problem, though

> This is just before </host>. The source I was reading
> (http://www.onjava.com/pub/a/onjava/2001/04/19/tomcat.html?page=2) said
> to add the line but did not state where to place it.
> I'm wondering though if this is only required when using servelets.

no. This has some specific information for Tomcat to handle your web
application; for example, the location of the application itself or if
classes should be loaded as soon as they are put into the web application

What you did looks fine. Try to reproduce the problem with a new,clean
application. Or try to reinstall Tomcat
Olinga K. Abbott - 14 Jan 2006 14:17 GMT
Rick, Andrea, thanks for responding. I reinstalled Tomcat and
everything works now.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.