Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting my first bean to work 2

Status
Not open for further replies.

oaklandar

Technical User
Feb 12, 2004
246
US
I have tried for several days to get my first bean working but it is not working.

Here is my jsp page called mypage.jsp located in

C:\jakarta-tomcat-4.1.27\webapps\examples\jsp\mypage.jsp

Code:
<html>
<body>

<%@ page import="HelperBean" %><%-- tried it with and without this line but still not working --%>
<jsp:useBean id="myHelperBean" class="HelperBean">
</jsp:useBean>

<% 
	myHelperBean.setIntProperty(34);
	myHelperBean.setStringProperty("itworks");
%>

Value of mydata is
	<%= myHelperBean.getIntProperty() %><BR>
Value of string prop:
	<%= myHelperBean.getStringProperty() %>
</body>
</html>

The Java Bean called HelperBean.java located in
C:\jakarta-tomcat-4.1.27\webapps\examples\WEB-INFO\classes

where I compiled it in DOS such as:
javac HelperBean.java
and it worked.

Code:
public class HelperBean
{
	private int intProperty = -1;
	private String stringProperty = null;
	
	public HelperBean() {}
	
	public void setIntProperty(int value)
	{
		intProperty = value;
	}
	
	public int getIntProperty()
	{
		return intProperty;
	}
	
	public void setStringProperty(String value)
	{
		stringProperty = value;
	}
	
	public String getStringProperty()
	{
		return stringProperty;
	}
}

When I pull up the mypage.jsp web page I get the following errors:

Code:
  [javac] Compiling 1 source file

C:\jakarta-tomcat-4.1.27\work\Standalone\localhost\examples\jsp\mypage_jsp.java:42: cannot resolve symbol
symbol  : class HelperBean 
location: class org.apache.jsp.mypage_jsp
      HelperBean myHelperBean = null;
      ^

An error occurred at line: 1 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
C:\jakarta-tomcat-4.1.27\work\Standalone\localhost\examples\jsp\mypage_jsp.java:44: cannot resolve symbol
symbol  : class HelperBean 
location: class org.apache.jsp.mypage_jsp
        myHelperBean = (HelperBean) pageContext.getAttribute("myHelperBean", PageContext.PAGE_SCOPE);
                        ^

An error occurred at line: 1 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
C:\jakarta-tomcat-4.1.27\work\Standalone\localhost\examples\jsp\mypage_jsp.java:47: cannot resolve symbol
symbol  : class HelperBean 
location: class org.apache.jsp.mypage_jsp
            myHelperBean = (HelperBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "HelperBean");
                            ^
3 errors

Please advise what I am doing wrong?
 
Hi,

The Error say's it not able to find the HelperBean class.

I copied your code to my webapplication at it is working fine. Make sure you have the HelperBean.class file under WEB-INF/classes.

It is always a better Idea to define a java file under a package.

Cheers
Venu
 
I thought I do have my HelperBean.class file under the WEB-INF/classes?

My current setup with file (HelperBean.java) is in here:
C:\jakarta-tomcat-4.1.27\webapps\examples\WEB-INFO\classes


And how would I define it under a package if that is what is necessary to get this to work so I can learn JSP.
 
It should be WEB-INF not WEB-INFO

If you want to package it, add the line in your bean (for example) :

package com.acme.test;

and then put your class in :

WEB-INF/classes/com/acme/test

and then in your JSP be sure to import your class (on 1.4 + you can only import packaged beans)

<@ page import="com.acme.test.* %>
 
Hi,

Create a folder under WEB-INF\classes and copy the java file into the newly created folder and compile the java file using javac.

Ex:
WEB-INF\classes\<NEW FOLDER>
NEW FOLDER >> NAME >> oaklandar

Copy the Java File HelperBean.java into the oaklander folder

add the below line into the HelperBean.java (Should be first Line)

package oaklandar;


from the command prompt cd to classes and use
C:\jakarta-tomcat-4.1.27\webapps\examples\WEB-INF\classes>
javac oaklander\HelperBean.java

When you open the folder oaklandar you should be able to see HelperBean.java and HelperBean.class

And in the JSP file you need to use it as

<%@ page import="oaklandar.HelperBean"%>
<jsp:useBean id="myHelperBean" class="oaklandar.HelperBean">
</jsp:useBean>

Are you sure its
C:\jakarta-tomcat-4.1.27\webapps\examples\WEB-INF\classes
NOT
C:\jakarta-tomcat-4.1.27\webapps\examples\WEB-INFO\classes

if so rename it to WEB-INF

Hope this will help you...

Cheers,
Venu
 
venur :

Is this not exactly what I said earlier ??!!
 
Sedj,

Yes, the same.. It's not my mistake. We both given the reply at the same time. I have seen your reply after clicking the submit post..

:)

Cheers
Venu
 
Thanks to both of you it now works. I had a typo error..it was WEB-INFO not WEB-INF.

Also, I have to compile my Java file (javac HelperBean.java) each time I make a change to it?? If so I am doing this locally so I go into DOS to do my javac every time? Is there a quicker way?

 
A tool that many Java devs use is Ant - search google for "Jakarta Ant" - its a *make* tool for java that build your project for you, using a build.xml file where youo specify what to build. There are many examples on the website - its a little bit of hassle to set up at first, but once you're there, its well worth it. I'd give you the link but I'm on dial-up and its too painful to open another browser !!!
 
Maybe a little bit more information would help !
 
Code I have written:

First.jsp


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>MediTrack - Create Account</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 { font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: x-large;
}
.style2 {font-size: medium}
.style3 {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #000099;
font-size: large;
font-weight: bold;
}
.style6 {color: #000099; font-size: x-small; font-family: Verdana, Arial, Helvetica, sans-serif;}
.style7 {font-size: xx-small}
-->
</style>
</head>

<body>
<FORM METHOD=POST ACTION="First3.jsp">


<p align="center" class="style1 style2">An Online Health Tracking System &reg; </p>
<p align="center" class="style1 style7">&nbsp;</p>
<center>
<table width="352" height="171" border="0">
<tr valign="bottom">
<td colspan="2"><p class="style3">Create a new account </p> </td>
</tr>
<tr valign="bottom">

</tr>
<tr>
<td width="144" valign="top"><div align="left">
<p>First Name:</p>
</div></td>
<td width="198"><p>
<input name="fname" type="text" onFocus="if(this.value=='Your message')this.value='';" value="">
</p> </td>
</tr>

<tr>
<td width="144" valign="top"><div align="left">
<p>User Name:</p>
</div></td>
<td width="198"><p>
<input name="uname" type="text" onFocus="if(this.value=='Your message')this.value='';" value="">
</p> </td>
</tr>

<tr>
<td colspan="2" valign="top"><p align="right"><span class="style1">
<input name="Reset" type="reset" value="Clear Form">
</span><span class="style1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input name="Submit2" type="submit" onClick="parent.location=''"; value="Next -&gt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></p></td>
</tr>
</table>
</center>
<p align="center" class="style1">&nbsp;</p>

</FORM>

</body>
</html>

FirstBean.java

package Second;

public class FirstBean
{
String fname;
String uname;

public void setUserName( String val)
{ uname = val; }

public void setFirstName( String val2)
{ fname = val2; }

public String getFirstName( )
{ return fname; }

public String getUserName( )
{ return uname; }


};


First3.jsp

<%@page import = "Second.FirstBean"%>
<jsp:useBean id="MyFirstBean" class="Second.FirstBean" scope="session"/>
<!--<jsp:setProperty name="MyFirstBean" property="*"/> -->
<HTML>
<BODY>
You entered<BR>
First Name: <%= MyFirstBean.getFirstName() %><BR>
User Name: <%= MyFirstBean.getUserName() %><BR>

</BODY>
</HTML>


The result I am getting is null for both the parameters.
 
You try to use this :

<jsp:setProperty name="MyFirstBean" property="*"/>

to set all the properties in the bean - but the documentation says this mechanism works like this :

By passing all of the values in the user's request (stored as parameters in the request object) to matching properties in the Bean

So you request parameter is called "uname", but there is no setter method named "setUname" in the bean.

Your bean should look like :

Code:
package Second;

public class FirstBean
{
    String firstName;
    String userName;

    public void setUserName( String userName)
    {   this.userName = userName; }

    public void setFirstName( String firstName)
    {   this.firstName = firstName; }

    public String getFirstName( )
    {  return firstName; }
    
    public String getUserName( )
    {  return userName; }


};

and you should change your text fields in the first page to be :

<input name="firstName" ...
...
<input name="userName" ...


Try looking at this link too :

 
Hey,

Its not working for me. I tried changing few things. Still no result. Can anybody help ASAP.

Thanks in advance.
 
>>> I tried changing few things.

Did you do ALL the changes I suggested ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top