LarryAndro
asked on
JSP Call to Bean Getter Malfunction
I am not understanding something about calling bean methods from JSP. All calls to the bean "getter" return nothing! Here's my jsp...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="test" class="myBeans.SimpleBean" />
<jsp:setProperty name="test" property="message" value="Hello WWW" />
Message 1: <jsp:getProperty name="test" property="message" />
<br>
<% test.setMessage("New Message"); %>
Message 2: <jsp:getProperty name="test" property="message" />
<br>
Message 3: <% test.getMessage(); %>
<br>
<jsp:setProperty name="test" property="message" value="Final Message" />
Message 4-1: <% test.getMessage(); %>
<br>
Message 4-2: <jsp:getProperty name="test" property="message" />
</BODY>
</HTML>
Here's my bean class...
package myBeans;
public class SimpleBean
{
private String message = "No message specified";
public String getMessage()
{
return(message);
}
public void setMessage(String message)
{
this.message = message;
}
}
(By the way, I created a test class with a main method and successfully tested calls to setMessage and getMessage.)
Here's my output...
Message 1: Hello WWW
Message 2: New Message <-- proof that test.setMessage call worked...
Message 3: <-- call to test.getMessage() failure...
Message 4-1: <-- call to test.getMessage() failure...
Message 4-2: Final Message
Nothing prints for Message #3 and #4-1 above. Why? The commonality is that both call test.getMessage().
Recently, I asked a question about using beans. My problem ended up being path related. This question is different than my previous question, but you can refer to https://www.experts-exchange.com/questions/21106099/useBean-Problem.html if I haven't provided enough background info above.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="test" class="myBeans.SimpleBean"
<jsp:setProperty name="test" property="message" value="Hello WWW" />
Message 1: <jsp:getProperty name="test" property="message" />
<br>
<% test.setMessage("New Message"); %>
Message 2: <jsp:getProperty name="test" property="message" />
<br>
Message 3: <% test.getMessage(); %>
<br>
<jsp:setProperty name="test" property="message" value="Final Message" />
Message 4-1: <% test.getMessage(); %>
<br>
Message 4-2: <jsp:getProperty name="test" property="message" />
</BODY>
</HTML>
Here's my bean class...
package myBeans;
public class SimpleBean
{
private String message = "No message specified";
public String getMessage()
{
return(message);
}
public void setMessage(String message)
{
this.message = message;
}
}
(By the way, I created a test class with a main method and successfully tested calls to setMessage and getMessage.)
Here's my output...
Message 1: Hello WWW
Message 2: New Message <-- proof that test.setMessage call worked...
Message 3: <-- call to test.getMessage() failure...
Message 4-1: <-- call to test.getMessage() failure...
Message 4-2: Final Message
Nothing prints for Message #3 and #4-1 above. Why? The commonality is that both call test.getMessage().
Recently, I asked a question about using beans. My problem ended up being path related. This question is different than my previous question, but you can refer to https://www.experts-exchange.com/questions/21106099/useBean-Problem.html if I haven't provided enough background info above.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry ramazanyich I didn't see your post.
ASKER
I initially tried <%=...%> as you both suggested and it didn't work. So, I tried <%...%>, as you saw in my first message. When I changed back to <%=...%> as suggested, it still didn't work. Here's the error I got...
C:\Tomcat_JWSDP_1.4.1.0\wo rk\Catalin a\localhos t\SCS_Prot otype\org\ apache\jsp \SimpleBea n_jsp.java :71: ')' expected out.print(test.getMessage( ););
But, I noticed you didn't append a semicolon to <%=test.getMessage();%> like I did. So, I removed my semicolon to make it exactly match your suggestion(s), and it worked.
Here's the working code...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="test" class="myBeans.SimpleBean" />
<jsp:setProperty name="test" property="message" value="Hello WWW" />
Message 1: <jsp:getProperty name="test" property="message" />
<br>
<% test.setMessage("New Message"); %>
Message 2: <jsp:getProperty name="test" property="message" />
<br>
Message 3: <%=test.getMessage()%>
<br>
<jsp:setProperty name="test" property="message" value="Final Message" />
Message 4-1: <%= test.getMessage() %>
<br>
Message 4-2: <jsp:getProperty name="test" property="message" />
</BODY>
</HTML>
But, now I'm mystified about the use or non-use of semicolons! There are two locations in the above code I am referring two:
(1) <% test.setMessage("New Message"); %>
(2) <%= test.getMessage() %>
If I remove the semicolon from #1 I get an "expected semicolon" error. If I add a semicolon to #2, I get an error!
What are the rules? (Add semicolon if code is being executed, and don't if output is being returned?)
C:\Tomcat_JWSDP_1.4.1.0\wo
But, I noticed you didn't append a semicolon to <%=test.getMessage();%> like I did. So, I removed my semicolon to make it exactly match your suggestion(s), and it worked.
Here's the working code...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="test" class="myBeans.SimpleBean"
<jsp:setProperty name="test" property="message" value="Hello WWW" />
Message 1: <jsp:getProperty name="test" property="message" />
<br>
<% test.setMessage("New Message"); %>
Message 2: <jsp:getProperty name="test" property="message" />
<br>
Message 3: <%=test.getMessage()%>
<br>
<jsp:setProperty name="test" property="message" value="Final Message" />
Message 4-1: <%= test.getMessage() %>
<br>
Message 4-2: <jsp:getProperty name="test" property="message" />
</BODY>
</HTML>
But, now I'm mystified about the use or non-use of semicolons! There are two locations in the above code I am referring two:
(1) <% test.setMessage("New Message"); %>
(2) <%= test.getMessage() %>
If I remove the semicolon from #1 I get an "expected semicolon" error. If I add a semicolon to #2, I get an error!
What are the rules? (Add semicolon if code is being executed, and don't if output is being returned?)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<% use them
<%= don't (and only put one expression)
<%= don't (and only put one expression)
sorry.. ramazanyich... I didn't refresh before I posted...
Please ignore my comment LarryAndro
:-)
Tim
Please ignore my comment LarryAndro
:-)
Tim
Please look in Tomcat's work folder you will find the translated file that Tomcat produced from your JSP file. You can see that
<%= test.getMessage() %> ( this is a JSP scripting element called an JSP expression)
is translated to
out.print(test.getMessage( ));
><% test.setMessage("New Message"); %> ( this is called a scriptlet )
This is just put into the code as is, so it needs the ; rrz
<%= test.getMessage() %> ( this is a JSP scripting element called an JSP expression)
is translated to
out.print(test.getMessage(
><% test.setMessage("New Message"); %> ( this is called a scriptlet )
This is just put into the code as is, so it needs the ; rrz
Thanks for the points Larry.
Be sure to start looking at the translated file(in work folder) for debugging clues. rrz
Be sure to start looking at the translated file(in work folder) for debugging clues. rrz