This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Beans

 
harikumar devandla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following .jsp file.

MyUseBean.jsp

<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean"/>
<jsp:setProperty name="myBean" property="myProperty" value="<%=request.getParameter("sentProperty")%>"/>
<jsp:getProperty name="myBean" property="myProperty"/>
MyBean.java

Package com.mypackage;

Public class MyBean
{
private String myProperty;

public MyBean()
{
this("initialValue");
}

public MyBean(String myProperty)
{
this.myProperty= myProperty;}

public void setMyProperty(String myProperty)
{
this. MyProperty= myProperty;
}

public String getMyProperty()
{
return myProperty;
}
}

Which of the following is the result produced when MyUseBean.jsp is accessed with the URL
MyUseBean.jsp?sentProperty=
(With out any value provided to sentProperty)

Can you please tell me what is the answer?
I guess the answer'd be compilation error. because this("initial value")
should not be used with the constructor for the bean class.
As it a public no args constructor.

kindly waiting for the reply.
Thanks
 
Gabriel Forro
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the Bean's code is all right. There is a no arg constructor, so it should be compiled without any error (You can call another constructor by the this(...) expression from the very first line of the constructor)

The final value of myProperty should be an ampty string. The reason is, that You have used the <jsp:setProperty.../>'s value form, where value is an empty string.
 
Nitish Bahadur
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't we need a fully qualified class name:
<jsp:useBean id="myBean" class="com.mypackage.MyBean"/>

Ignoring all compilation errors, I agree with Forro.
 
Sharon
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am beginner of JSP. but just wonder << language="java" >> is legal or not. becuase I never see it from the book. Please advise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic