This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Reg useBean standard action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Reg useBean standard action" Watch "Reg useBean standard action" New topic
Author

Reg useBean standard action

Purna Chandra Rao
Greenhorn

Joined: Feb 06, 2006
Posts: 23
Hi Ranchers,
Please help me with the following questions:
1) What happens If we try to use a bean that does not have a default constructor?Is it Compile time error or run time error?
2)What happens if we try to set or get a property using <jsp:setProperty> and <jsp:getProperty> and that property does not exist.Is it Compile time error or run time error?
3)Assume that MyClass is a valid bean with a property called name and valid setter and getter methods.

<%{
<jsp:useBean id="myBean" class="MyClass"/>
}%>
<jsp:useBean id="myBean" class="MyAnotherClass"/> //1
<%=myBean.getName()%> //2

It was given in a mock exam that line 1 results in a translation error and the line 2 results in a compile time error.Is that correct?if correct please explain me because as far as i know usebean standard action is used to creates an object of the specified class with the name specified in the id if that bean does not exist.In the above code the first useBean action is used in the block which means it is accessed only in that block and thereby line2 gives a compile time error as myBean is not available outsied the block.In that case why does line 1 gives translation error as we are free to declare myBean again as it is in outide of the block.Please correct me..I am confused.

4)

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)
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
1 - It should be a translation error
2 - Same translation error
3 - You can't declare two beans with same id in a single JSP, because useBean creates the object and places it in page scope if no scope is given. so even if you have declared your bean in <%{%> <%}%>, it will place the bean in page scope and it will be available outside {} by retreiving it from pageContext.getAttribute(beanName).

4- You can try it out yourself by runing this code. It will print "" for <jsp:getProperty name="myBean" property="myProperty"/>

[ February 22, 2007: Message edited by: Ali Gohar ]

[ February 22, 2007: Message edited by: Ali Gohar ]
[ February 22, 2007: Message edited by: Ali Gohar ]
Purna Chandra Rao
Greenhorn

Joined: Feb 06, 2006
Posts: 23
Thanks Ali.
But how can the first two be translation errors?can you please expalin because i thought they should be compile time errors.Will the availability of default constructor and existance of a property for a bean checked at translation time itself??



Thanks and regards,
Purna Chandra Rao
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Sorry my mistake, yes these should be compile time errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg useBean standard action
 
Similar Threads
Mock exam question
JSP - UseBean
jsp:include
Java Beans
Unable to assign value to Bean property using jsp expression