• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JSP and JavaBeans

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic