wood burning stoves*
The moose likes Servlets and the fly likes I am doing something wrong, WHAT??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "I am doing something wrong, WHAT???" Watch "I am doing something wrong, WHAT???" New topic
Author

I am doing something wrong, WHAT???

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
Hey,
I have just returned to the world of Java and am starting with JSP. Following is supposedly a very simple piece of code, but I am getting hard time running it successfully. My environment is Windows 2000, Apache 1.3.20, Tomcat Version 3.<something>. The logic is quite simple: Their are 3 files involved:
NameHandler.java/.class --- The Bean class file
FeedBack.jsp --- A .jsp file which is calling the bean class and also has the html form. Further, if a condition is satisfied, it is sending a response back. The html form has two text fields and submit & reset buttons. On Submitting the data, the values of the text fields are returned to the user in another jsp page(response.jsp)
response.jsp --- The .jsp file which sends some reply to the FeedBack.jsp file.

The codes and Errors are as follows:

********************************* NameHandler.java STARTS *********************************
package hello;
import java.beans.*;
class NameHandler
{
private String userName;
private String carName;
public NameHandler()
{
userName=null;
carName=null;
}
public void setUserName(String name)
{
userName=name;
}
public String getUserName()
{
return userName;
}

public void setCarName(String car)
{
carName=car;
}
public String getCarName()
{
return carName;
}
}
********************************* NameHandler.java ENDS *********************************
********************************* FeedBack.jsp STARTS *********************************
<%@ page import="hello.*" %>
<%@ page info="The Main JSP File which is called by the Client Browser" %>

<jsp:useBean id="NameBean" scope="page" class="hello.NameHandler" />
<jsp:setProperty name="NameBeam" property="UserName" param="username" />
<jsp:setProperty name="NameBeam" property="CarName" param="carname" />
</jsp:useBean>
<html>
<head><title>FeedBack is Important!</title></head>
<body>
<%-- <%@ include file="form.html" %> --%>
<form method="get">
<table border=1 width=300 align=center>

<tr>
<td><b><font face="verdana" size=2 align="center">User Name : </font></b></td>
<td><input type=text name="username" size=30></td>
</tr>

<tr>
<td><b><font face="verdana" size=2 align="center">Car Name : </font></b></td>
<td><input type=text name="carname" size=30></td>
</tr>

<tr>
<td><input type=submit value="Send Data"></td>
<td><input type=reset value="Clear Form"></td>
</tr>
</table>
</form>

<%
if(request.getParameter("username") !=null)
{
%>
<%@ include file="response.jsp" %>
<%
}
%>
</body>
</html>
********************************* FeedBack.jsp ENDS *********************************
********************************* response.jsp STARTS *******************************
<table border=1 width=300 align=center>
<tr>
<td align=center><font face="verdana" size=2>Your Name is <jsp:getProperty name="NameBean" property="UserName" />
</font></td>
</tr>
<tr>
<td align=center><font face="verdana" size=2>Your Car's Brand is <jsp:getProperty name="NameBean" property="CarName" />
</font></td>
</tr>
</table>
********************************* response.jsp ENDS *******************************
********************************* ERROR MESSAGE RECIEVED STARTS ******************
Error: 500
Location: /mydev/hello/FeedBack.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to load class hello.NameHandler
at org.apache.jasper.compiler.BeanRepository.getBeanType(BeanRepository.java:183)
at org.apache.jasper.compiler.GetPropertyGenerator.generate(GetPropertyGenerator.java:99)
at org.apache.jasper.compiler.JspParseEventListener$GeneratorWrapper.generate(JspParseEventListener.java:773)
at org.apache.jasper.compiler.JspParseEventListener.generateAll(JspParseEventListener.java:220)
at org.apache.jasper.compiler.JspParseEventListener.endPageProcessing(JspParseEventListener.java:175)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:183)
at org.apache.jasper.servlet.JspServlet.doLoadJSP(JspServlet.java:462)
at org.apache.jasper.servlet.JasperLoader12.loadJSP(JasperLoader12.java:146)
at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:433)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:152)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:164)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
Root cause:
java.lang.ClassNotFoundException: Unable to load class hello.NameHandler
at org.apache.jasper.servlet.JasperLoader.findClass(JasperLoader.java:223)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:147)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at org.apache.jasper.compiler.BeanRepository.getBeanType(BeanRepository.java:181)
at org.apache.jasper.compiler.GetPropertyGenerator.generate(GetPropertyGenerator.java:99)
at org.apache.jasper.compiler.JspParseEventListener$GeneratorWrapper.generate(JspParseEventListener.java:773)
at org.apache.jasper.compiler.JspParseEventListener.generateAll(JspParseEventListener.java:220)
at org.apache.jasper.compiler.JspParseEventListener.endPageProcessing(JspParseEventListener.java:175)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:183)
at org.apache.jasper.servlet.JspServlet.doLoadJSP(JspServlet.java:462)
at org.apache.jasper.servlet.JasperLoader12.loadJSP(JasperLoader12.java:146)
at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:433)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:152)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:164)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
********************************* ERROR MESSAGE RECIEVED ENDS ******************

Eagerly waiting for your help.
Thanks & Bye,
Tualha Khan
(Long Lost SCJP)

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Based on this:
Root cause:
java.lang.ClassNotFoundException: Unable to load class hello.NameHandler
I would say your compiled NameHandler.class file is not in the right place. It has to be in the
appRoot\WEB-INF\classes\hello\
directory where appRoot is whatever "web application" the JSP belongs to.
Bill
------------------
author of:
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Fine,
Thanks, will try to rectify and see what comes up.
Bye,
Tualha Khan
Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236
Hi Tualha,
Couple of things i noticed in your code...

1) In feedback.jsp you have written the following..
<b>
<jsp:useBean id="NameBean" scope="page" class="hello.NameHandler" />
<jsp:setProperty name="NameBeam" property="UserName" param="username" />
<jsp:setProperty name="NameBeam" property="CarName" param="carname" />
</jsp:useBean>

</b>
There is a mismatch in the id and the name attribute..
i,e you have given id as "NameBean" and name as "NameBeam".They should be the same for obvious reasons.(was it a typo!!?)
I think the JSP engine is throwing the exception because of this
mismatch..
2)Declare your bean class with the public modifier.This might be
the other reason for the code not working
Hope this helps,
Manjunath
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
Hey,
Okay, I was able to get the initial form submition screen. But when I feed the data and press the submit button. I get the following error.
I have done the rectifications as specified by Manjunath and William.
Now where am I wrong?
Thanks & Bye,
Tualha Khan

******************** ERROR STARTS ********************
Error: 500
Location: /mydev/FeedBack.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Attempted a bean operation on a null object.
at org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty(JspRuntimeLibrary.java:424)
at _0002fFeedBack_0002ejspFeedBack_jsp_0._jspService(_0002fFeedBack_0002ejspFeedBack_jsp_0.java:84)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
******************** ERROR ENDS ********************
Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236

Hi Tualha,

Observe that,in the reponse.jsp file, You are using the <jsp:getProperty> tag without using the <jsp:useBean> tag.
This is not wrong, but you can do this only if the instance of the bean class which you have created in the feedback.jsp is visible in response.jsp.Because you have set the scope of the bean instance as "page" in feedback.jsp,it is visible(or accessible)only in feedback.jsp and not in response.jsp.
so using jsp:getProperty here is like calling a method on a null reference and thus the error message,
"Attempted a bean operation on a null object".
Probably you can try two things to rectify this..
1)Make the scope of the bean instance as "request"
or "application" or even "session".
2)Or use jsp:getProperty in conjuction with <jsp:useBean> in
response.jsp.Here you would be creating a fresh instance of
the bean class.
Hope this helps,
Manjunath
Dave Soto
Ranch Hand

Joined: Sep 15, 2001
Posts: 55
Could you post the new code, now that you've fixed it some?
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
Hey,
I have done everything as suggested by manjunath, but still I am recieving the same error of null operation.
Some thing else is still missing, please help.
Thanks & Bye,
Tualha Khan
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
According to your last exception stack trace, your error is at _0002fFeedBack_0002ejspFeedBack_jsp_0.java line 84.
To properly debug this, you should try to find this file. This is the java code that tomcat converts your jsp page to, before compiling it into a servlet.
The file is probably located in the directory <tomcat-home>/work/localhost_8080%2F<webapp-name>. Or just do a Windows search for the file. If you can't figure out what's wrong at line 84 (maybe it's a mispelling of a classname or variable), just post everything up to line 84 for us to review.
Cheers
------------------
Miftah Khan
- Sun Certified Programmer for the Java� 2 Platform
- Sun Certified Web Component Developer for the Java� Platform, Enterprise Edition
Jayanthimeena
Greenhorn

Joined: Jul 24, 2001
Posts: 17
*** response.jsp STARTS *******************************
<jsp:useBean id="NameBean1" class="hello1.NameHandler" scope="session"></jsp:useBean>
<table border=1 width=300 align=center>
<tr>
<td align=center><font face="verdana" size=2>
Your Name is
<% String ls =NameBean1.getUserName() ;
out.println(ls);
%>
</font></td>
</tr>
<tr>
<td align=center><font face="verdana" size=2>
Your Car's Brand is <%
out.println(NameBean1.getCarName() );%></font></td>
</tr>
</table>
***************** response.jsp Ends **********
***************** Feedback.jsp Starts **********
<%@ page info="The Main JSP File which is called by the Client Browser" %>
<html>
<head><title>FeedBack is Important!</title></head>
<body>
<% //-- <%@ include file="form.html" %>
%>
<jsp:useBean id="NameBean1" class="hello1.NameHandler" scope="session"></jsp:useBean>
<form method="get">
<%
if(request.getParameter("username") !=null)
{
NameBean1.setUserName(request.getParameter("username")) ;
NameBean1.setCarName(request.getParameter("carname")) ;
response.sendRedirect("response.jsp");
}
%>
<table border=1 width=300 align=center>
<tr>
<td><b><font face="verdana" size=2 align="center">User Name : </font></b></td>
<td><input type=text name="username" size=30></td>
</tr>

<tr>
<td><b><font face="verdana" size=2 align="center">Car Name : </font></b></td>
<td><input type=text name="carname" size=30></td>
</tr>

<tr>
<td><input type=submit value="Send Data"></td>
<td><input type=reset value="Clear Form"></td>
</tr>
</table>
</form>
</body>
</html>
***************** Feedback.jsp Ends **********
***************** NameHandler.java starts **********
package hello1;
import java.beans.*;
public class NameHandler
{
private String userName;
private String carName;
public NameHandler()
{
userName="";
carName="";
}
public void setUserName(String name)
{
userName=name;
}
public String getUserName()
{
return userName;
}
public void setCarName(String car)
{
carName=car;
}
public String getCarName()
{
return carName;
}
}
***************** NameHandler.java ends **********
hai this might help u a lot.I chenaged a lot. I put the jsp in tomcat/webapps/examples folder and java and class in web-inf/classes folder.
noe compile java file and check it. It will work fine.
bye
Jayanthi
Matthias Bayer
Greenhorn

Joined: Dec 21, 2000
Posts: 4
Hi,
beans property: Username != userName!!!
<jsp:useBean id="NameBean" scope="page" class="hello.NameHandler" />
<jsp:setProperty name="NameBean" property="userName" param="username" />
<jsp:setProperty name="NameBean" property="carName" param="carname" />
</jsp:useBean>
And Bean class must be public!
Good luck!
Matthias
[This message has been edited by Matthias Bayer (edited September 24, 2001).]
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
Hey,
I was able to get the code working, but when I added one more field to it, it started giving me the following error. Any logical reasons for this behaviour??
*************** New ERROR STARTS ****************
Error: 500
Location: /mydev/FeedBack1.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Cannot find any information on property 'Phone' in a bean of type 'hello.NameHandler'
at org.apache.jasper.runtime.JspRuntimeLibrary.getReadMethod(JspRuntimeLibrary.java:616)
at org.apache.jasper.compiler.GetPropertyGenerator.generate(GetPropertyGenerator.java:101)
at org.apache.jasper.compiler.JspParseEventListener$GeneratorWrapper.generate(JspParseEventListener.java:771)
at org.apache.jasper.compiler.JspParseEventListener.generateAll(JspParseEventListener.java:220)
at org.apache.jasper.compiler.JspParseEventListener.endPageProcessing(JspParseEventListener.java:175)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:210)
at org.apache.jasper.servlet.JspServlet.doLoadJSP(JspServlet.java:612)
at org.apache.jasper.servlet.JasperLoader12.loadJSP(JasperLoader12.java:146)
at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:542)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:258)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:268)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)

*************** New ERROR ENDS ****************

Thanks & Bye,
Tualha Khan
I can post the working code, if need be!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I am doing something wrong, WHAT???
 
Similar Threads
Error: Unable to load class
Browser back button
Array Index Out Of Bounds
I M NOT ABLE TO DEFINE VARIABLES IN JSP.
datas storing twice from servlet to mysql database