• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JSP and JavaBeans

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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?
Regards,
Kunal Jaggi
SCJP2
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which server do you use?
[ February 22, 2003: Message edited by: Ruff Young ]
 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am Using Tomcat 4.1
 
Ruff Young
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you have the directory structure like "install_dir/webapps/ROOT", then web application name is called "ROOT".
It means that hello3.jsp is accessed via "http://localhost:8080/ROOT/hello3.jsp".
Otherwise, move hello3.jsp under "webapps" directly. Then you can access the file using "http://localhost:8080/hello3.jsp";
Your source files doesn't have any problems.
Please let me know anything wrong with it.
 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ruff,
Well as far as the placement of JSP file goes I am pretty confident that in Tomcat it is placed in install_dir/webapps/ROOT directory. I have tried an example :
<HTML>
<HEAD><TITLE>Hello</TITLE></HEAD>
<BODY>
<H1>
<%
if (request.getParameter("name") == null) {
out.println("Hello World");
}
else {
out.println("Hello, " + request.getParameter("name"));
}
%>
</H1>
</BODY></HTML>
I have placed this file in install_dir/webapps/ROOT directory and access it using http://localhost:8080/hello1.jsp?name=Kunal this works fine. Now when I try to access the same using the URL http://localhost:8080/ROOT/hello1.jsp I get the default Tomcat 404 error page.
Also, when I place the hello1.jsp file under install_dir/webapps dir.and try to access it using http://localhost:8080/hello1.jsp?name=Kunal I get the error page.
I have just started programming Servlets and JSP and whatever little knowledge I have gathered so far I can say that install_dir/webapps/ROOT is the default web application where the context path is an empty string.
I believe there there is something wrong with the placement of HelloBean.class file.
 
Ruff Young
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jaggi,
Tomcat directory structure looks like
C:\tomcat\bin\..
C:\tomcat\classes\..
C:\tomcat\webapps\..
All the web applications are under C:\tomcat\webapps\. In your example, the webapps directory contains "ROOT" web application which means the document root for the "ROOT" web application. So the request for http://localhost:8080/ROOT/hello1.jsp?name=Kunal will refer the hello1.jsp file in the "ROOT" directory.
I tried your exmple on my local machine. It works fine using the URL http://localhost:8080/ROOT/hello1.jsp.
I put the your Bean class under \webapps\ROOT\WEB-INF\classes\.
I wonder that when you install tomcat, did you add like "ROOT" directory? If you did, it's not web application name anymore. So you can acess using http://localhost:8080/hello1.jsp?name=Kunal. Definitely, it doesn't work using http://localhost:8080/ROOT/hello1.jsp?name=Kunal.
[ February 23, 2003: Message edited by: Ruff Young ]
 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ruff,
Thanks for your sincere efforts, but I can’t still run the basic JavaBean example.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes the import statements fails if the classes are not packaged.
Try to package your class.
Then import it using :
<%@ page import="packageName.HelloBean" %>
HTH
 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Juanjo,
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.
 
Juanjo Bazan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you set your import statement correctly?
It should be:
<%@ page import="MyBean.HelloBean" %>
If it still doesn't work, let us the code of your jsp page.(you can post code inside [ code ] tags)
HTH.
 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the source code of hello3.jsp file :<%-- hello3.jsp --%>
<%@ page import="MyBean.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>
[ February 25, 2003: Message edited by: Jaggi Kunal ]
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic