• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock exam question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic