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!

Another JSP Bean assistance request 2

Status
Not open for further replies.

oaklandar

Technical User
Feb 12, 2004
246
US
I am trying to get this JSP to work in the JSP Bean. Please advise what I am doing wrong?


Here is my Java file called myTherm.java which compiles with no problems:
Code:
package colors;

public class myTherm
{
private int temp;
private int maxTemp;
private int minTemp;


public myTherm()
{
this(34);
}

public myTherm(int temp)
{
this.temp = temp;
init();
}

public void setTemp(int temp)
{
this.temp = temp;
init();
}

public int getTemp()
{
return temp;
}

private void init()
{
maxTemp = this.temp + 10;
minTemp = this.temp = 20;
if(maxTemp > 150)
System.out.println("above");
else
System.out.println("below");
}
}

My JSP with the Bean call here:
Code:
<jsp:useBean id="myStat" class="colors.myTherm">

</jsp:useBean>
<html>
<body>

<% myTherm t = new myTherm(34); %>
The data is now <%= myStat.getTemp() %> data.

</body>
</html>
Error messages here:


Generated servlet error:
[javac] Compiling 1 source file

C:\jakarta-tomcat-4.1.27\work\Standalone\localhost\examples\jsp\colors\myTherm_jsp.java:55: cannot resolve symbol
symbol : class myTherm
location: class org.apache.jsp.myTherm_jsp
myTherm t = new myTherm(34);
^



An error occurred at line: 2 in the jsp file: /jsp/colors/myTherm.jsp

Generated servlet error:
C:\jakarta-tomcat-4.1.27\work\Standalone\localhost\examples\jsp\colors\myTherm_jsp.java:55: cannot resolve symbol
symbol : class myTherm
location: class org.apache.jsp.myTherm_jsp
myTherm t = new myTherm(34);
^
2 errors



 
Add this line at the top of your JSP :

<%@ page import="colors.*" %>

or change this line :

<% myTherm t = new myTherm(34); %>

to :

<% colors.myTherm t = new colors.myTherm(34); %>

Couple of things though -

why are you using the <jsp:useBean ...> tag and then creating a new "myTherm"" bean in the scriptlet ?

Java classes should be called naming conventions like "MyTherm" rather than "myTherm" - its not necessary but its good Java programming practice.
 

why are you using the <jsp:useBean ...> tag and then creating a new "myTherm"" bean in the scriptlet ?


I am still learning the bean part and not sure I assume there is a better way to do this?
 
You should be able to remove this line :

<% myTherm t = new myTherm(34); %>

because once the JSP is written into a servlet on before compilation, the <jsp:useBean ...> code will in effect become :

myTherm t = new myTherm(34);

Look in TOMCAT_HOME/work/* to find how your JSPs look once they are made into servlets and compiled - if you jsp is called "mypage.jsp" the servlet will be called something like "mypage_jsp.java".
 
Yes, that is exactly how it looks. Thanks for the explanations.
 
It helps to understand what goes on under the sheets with these things, even though JSP is supposed to "insulate" you from it. The code:

Code:
<jsp:useBean id="myStat" class="colors.myTherm"/>

is basically equivalent to

Code:
<%
if (pageContext.getAttribute("myStat")==null) {
    myTherm foo = new myTherm();
    pageContext.setAttribute("myStat", foo);
}
%>

similarly, the code

Code:
<jsp:getProperty name="myStat" property="temp"/>

is essentially equivalent to

Code:
<%
out.print(((myTherm) pageContext.findAttribute("myStat")).getTemp());
%>

This hopefully shows how JSP beans work so you can use the tags more effectively in the future, and avoid typing some code.

News and views of some obscure guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top