GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Mock exam question" Watch "Mock exam question" New topic
Author

Mock exam question

T Madhavi
Greenhorn

Joined: Apr 19, 2007
Posts: 10
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= (Without any value provided to sentProperty)


A.Runtime Error

B.Prints "null"

C.Prints "initialValue"

D.None Of the Above



This is a question from Mock Exams.

My Ans is: B
But the correct Ans is :C

Can anyone let me know the explanation?

Thanks
Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
Hi Madhavi,

I think the answer should be D i.e. None of the above choices. According to me it should show a blank page to you. The reason I am telling you is because of this:
1) If we run the jsp with url, MyUseBean.jsp?sentProperty= (Without any value provided to sentProperty) i.e. after sentProperty we dont put any thing then in such case you will get output as blank. Because it will take sentPropery value as blank.
2) we get output as null under the condition that we run MyUseBean.jsp with no sendProperty appended to it i.e. MyUseBean.jsp
As per the code of jsp given, in no case it will display output as initialValue.
I have tested this code and it works in the above explained manner.


Thanks,<br />-Rancy
Arun cog
Greenhorn

Joined: May 06, 2007
Posts: 2
Hi,

The answer will be C. At first the code will invoke the constructor with the argument. and then it will implicitly call the super so it will invoke the constructor without the argument. so it will print C.intialvalue.

Kindly Correct me if i am wrong any where.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Rancy Chadha:
As per the code of jsp given, in no case it will display output as initialValue.
I have tested this code and it works in the above explained manner.


Are you sure? Accrodinlgy to secton JSP.5.2 of the spec:


param:
The name of the request parameter whose value is given to a bean property. The name of the request parameter usually comes from a web form.
If param is omitted, the request parameter name is assumed to be the same as the bean property name.
If the param is not set in the Request object, or if it has the value of ““, the jsp:setProperty action has no effect (a noop). An action may not have both param and value attributes.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Sergio,
please note that the "param" attribute is not used in the setProperty tag here. So I think that your comment does not apply here. (request.getParameter is being called instead)


[My Blog]
All roads lead to JavaRanch
Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
Hi Sergio,

I think you misunderstood my statement. What you have written as per specs is absolutely correct and it works in the same way if instead of <jsp:setProperty name="myBean" property="myProperty" value="<%=request.getParameter("sentProperty")%>"/>, I write something like <jsp:setProperty name="myBean" property="myProperty"/> or <jsp:setProperty name="myBean" property="myProperty" param="sentProperty"/> and in the url I either omitt sentProperty parameter or write MyUseBean.jsp?sentProperty= then in this scenario it will display 'initialValue' in the output but what i said was
As per the code of jsp given, in no case it will display output as initialValue.
The code that Madhavi has pasted which has a value attribute (value="<%=request.getParameter("sentProperty")%>"/) specified, in this case it will not display 'initialValue' in the output.
I hope I am making sense here. But thank you for the specs portion you mentioned I had missed the last statement of param attribut now I got it. Thank you!
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Sorry, my mistake.

Thank you Satou and Rancy. Of course, both of you are right.

[Offtopic] My greatest problem when I take mock exams is that sometimes I don't 'read correctly' what's being asked. I'd better work on that before the real exam.[/Offtopic]
T Madhavi
Greenhorn

Joined: Apr 19, 2007
Posts: 10
Thank you guys.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Mock exam question