GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
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?
Regards,
Kunal Jaggi
SCJP2
Ruff Young
Ranch Hand

Joined: Sep 26, 2002
Posts: 513
Which server do you use?
[ February 22, 2003: Message edited by: Ruff Young ]

SCJP2, SCWCD<br />IBM Certified WebSphere<br />IBM Certified Solution Developer for WSAD
Jaggi Kunal
Ranch Hand

Joined: Jan 21, 2003
Posts: 45
I am Using Tomcat 4.1
Ruff Young
Ranch Hand

Joined: Sep 26, 2002
Posts: 513
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

Joined: Jan 21, 2003
Posts: 45
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

Joined: Sep 26, 2002
Posts: 513
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

Joined: Jan 21, 2003
Posts: 45
Hi Ruff,
Thanks for your sincere efforts, but I can’t still run the basic JavaBean example.
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
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

Joined: Jan 21, 2003
Posts: 45
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

Joined: Feb 04, 2002
Posts: 231
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

Joined: Jan 21, 2003
Posts: 45
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP and JavaBeans