*
The moose likes JSP and the fly likes JSP and JavaBeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP and JavaBeans " Watch "JSP and JavaBeans " New topic
Author

JSP and JavaBeans

Jaggi Kunal
Ranch Hand

Joined: Jan 21, 2003
Posts: 45
I am finding problems using a simple Bean in a JSP Page.
The bean code is given below:
public class HelloBean {
private String name = "World";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
I have placed the source code and the .class file in install_dir/webapps/ROOT/WEB-INF/classes directory
The JSP code is as under:
<%-- hello3.jsp --%>
<%@ page import="HelloBean" %>
<jsp:useBean id="hello" class="HelloBean">
<jsp:setProperty name="hello" property="*" />
</jsp:useBean>
<HTML>
<HEAD><TITLE>Hello</TITLE></HEAD>
<BODY>
<H1>
Hello, <jsp:getProperty name="hello" property="name" />
</H1>
</BODY>
</HTML>
I have placed the hello3.jsp file in install_dir/webapps/ROOT directory
Now when I try to run the JSP file using http://localhost:8080/hello3.jsp , there is a compiler error indicating that the import statement is wrong. Where the Bean class file should be placed?
I am Using Tomcat 4.1.12
Regards,
Kunal Jaggi
SCJP2
Jaggi Kunal
Ranch Hand

Joined: Jan 21, 2003
Posts: 45
I tried to put the HelloBean.class file inside a package.
package MyBean;
public class HelloBean {
private String name = "World";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Now the class file is placed in install_dir/webapps/ROOT/WEB-INF/classes/MyBean directory and the hello3.jsp file is placed in install_dir/webapps/ROOT directory. When I try to access the JSP page using http://localhost:8080/hello3.jsp then this time I don’t get a compilation error instead java.lang.ClassNotFoundException is thrown. The web page is cluttered with a big stack trace.
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
Try moving your package to WEB-INF/lib
See http://www.onjava.com/pub/a/onjava/2001/03/15/tomcat.html for more info regarding web application folder structure
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP and JavaBeans
 
Similar Threads
Problem Using Bean in JSP
JSP and JavaBeans
Unable to compile class for JSP
jsp + javabean path
Code not working