JSP/JavaBean - Newbie help

D

Dave

I have set-up Apache Tomcat for the first time and I'm trying to configure a
test JSP website on it. I'm attempting to use a simple JavaBean with my JSP
page and I keep getting errors relating to the JavaBean.

index.jsp is in the $CATALINA_HOME\webapps\JSPTest\WebContent

SimpleBean.java is in $CATALINA_HOME\webapps\JSPTest\src\mybeans

SimpleBean.class is in
$CATALINA_HOME\webapps\JSPTest\WebContent\WEB-INF\classes\mybeans

web.xml is in $CATALINA_HOME\webapps\JSPTest\WebContent\WEB-INF\


If I change the "type" attribute of the jsp:useBean tag then to "class" then
I get a different exception (shown below).

I'm using Tomcat 5.5 for Windows, JRE/JDK 1.5. I have tried uninstalling and
re-installing Tomcat, and also tried running it as both a service and a
standalone application.

Any idea what the problem is? I presume it's something fundamental I'm
missing.

Thanks in advance, code and errors are below.

********
index.jsp
********

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="mybeans.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using SimpleBean</title>
</head>
<body>
<jsp:useBean id="msg" type="SimpleBean" />
Test Content
</body>
</html>


*************
SimpleBean.java
*************

package mybeans;
public class SimpleBean
{
private String message;
public SimpleBean()
{
}
public void setMessage(String input)
{
message = input;
}
public String getMessage()
{
return message;
}
}



*******
web.xml
*******

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
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">
<display-name>
JSPTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>SimpleBean</servlet-name>
<servlet-class>SimpleBean</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleBean</servlet-name>
<url-pattern>/servlet/SimpleBean</url-pattern>
</servlet-mapping>
</web-app>



*********
Exception 1
*********

description The server encountered an internal error () that prevented it
from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 13 in the jsp file: /WebContent/index.jsp
Generated servlet error:
SimpleBean cannot be resolved to a type

An error occurred at line: 13 in the jsp file: /WebContent/index.jsp
Generated servlet error:
SimpleBean cannot be resolved to a type


org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) 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)root causeorg.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 13 in the jsp file: /WebContent/index.jspGenerated servlet error:SimpleBean cannot be resolved to a typeAn error occurred at line: 13 in the jsp file: /WebContent/index.jspGenerated servlet error:SimpleBean 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:414) org.apache.jasper.compiler.Compiler.compile(Compiler.java:297) org.apache.jasper.compiler.Compiler.compile(Compiler.java:276) org.apache.jasper.compiler.Compiler.compile(Compiler.java:264) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305) 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)note The full stack trace of the root cause is available in the ApacheTomcat/5.5.20 logs.********Exception 2*********description The server encountered an internal error () that prevented itfrom fulfilling this request.exceptionorg.apache.jasper.JasperException: Unable to load class for JSP org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) 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)root causeorg.apache.jasper.JasperException: Unable to load class for JSP org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:598) org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:147) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317) 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)root causejava.lang.ClassNotFoundException: org.apache.jsp.WebContent.index_jsp java.net.URLClassLoader$1.run(Unknown Source) java.security.AccessController.doPrivileged(Native Method) java.net.URLClassLoader.findClass(Unknown Source) org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:133) org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:65) org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:596) org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:147) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317) 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)note The full stack trace of the root cause is available in the ApacheTomcat/5.5.20 logs.***************************Exception 3 (using "class" instead of "type" attribute)***************************description The server encountered an internal error () that prevented itfrom fulfilling this request.exceptionorg.apache.jasper.JasperException: /WebContent/index.jsp(13,0) The value forthe useBean class attribute SimpleBean is invalid. org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) 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)root causeorg.apache.jasper.JasperException: /WebContent/index.jsp(13,0) The value forthe useBean class attribute SimpleBean 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:1174) 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:3320) org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198) org.apache.jasper.compiler.Compiler.compile(Compiler.java:295) org.apache.jasper.compiler.Compiler.compile(Compiler.java:276) org.apache.jasper.compiler.Compiler.compile(Compiler.java:264) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305) 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)note The full stack trace of the root cause is available in the ApacheTomcat/5.5.20 logs.
 
L

Lew

Dave said:
********
index.jsp
********

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="mybeans.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

It's better to use the lower-case "html"; this will prepare you better for XHTML.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using SimpleBean</title>
</head>
<body>
<jsp:useBean id="msg" type="SimpleBean" />
Test Content
</body>
</html>

<jsp:useBean id="msg" class="mybeans.SimpleBean" />

I don't think the <%@ page import="mybeans.*" %> works to prevent the need to
fully qualify the class in a <jsp:useBean ... /> tag. I easily could be
mistaken here, but the Sun docs refer to
<jsp:useBean id="beanName" class="fully_qualified_classname" scope="scope" />

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
*************
SimpleBean.java
*************

package mybeans;
public class SimpleBean
{
...
}
*******
web.xml
*******

<servlet>
<servlet-name>SimpleBean</servlet-name>
<servlet-class>SimpleBean</servlet-class>
</servlet>

SimpleBean is not a servlet.
> SimpleBean.class is in
> $CATALINA_HOME\webapps\JSPTest\WebContent\WEB-INF\classes\mybeans
>
> web.xml is in $CATALINA_HOME\webapps\JSPTest\WebContent\WEB-INF\

You should install the app in the webapps/ directory directly, not one level
down in WebContent/, i.e.,

$CATALINA_HOME/webapps/JSPTest/index.jsp
$CATALINA_HOME/webapps/JSPTest/WEB-INF/
$CATALINA_HOME/webapps/JSPTest/WEB-INF/classes/...

If WEB-INF/ is buried one level down then the container cannot find the
classes/ subdirectory.

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/WebApp3.html#wp225219
http://tomcat.apache.org/tomcat-5.5-doc/appdev/deployment.html

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top