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

Tag

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating a tag called SmartInputTag to replace the HTML <input> tag. If the request contains a value for a given parameter, this tag automatically puts that value in the input tag for that parameter. This tag is useful when the same form is redisplayed to the user if it fails validation. For example, if the user has already input 28 as the value of "age" parameter, your SmartInputTag will generate the following outout:

<input type="text" name="age" value="28" />

Which of the following is an appropriate tag handler method to achieve this?


(1) public void doStartTag() throws JspException {
PageContext pc = (PageContext) getJspContext();
JspWriter out=pc.getOut();
try {
String value = pc.getRequest().getParameter(this.name);
if(value == null) value = "";
out.write("<input type=\"text\" name=\""+this.name+"\" value=\""+value+"\"/>");
} catch (java.io.IOException ex) {
throw new JspException(ex.getMessage());
}
}

(2) public void doStartTag() throws JspException {
PageContext pc = (PageContext) getJspContext();
JspWriter out=pc.getOut();
try {
String value = pc.getRequest().getParameter(this.name);
if(value == null) value = "";
out.write("<input type=\"text\" name=\""+this.name+"\" value=\""+value+"\"/>");
} catch (java.io.IOException ex) {
throw new JspException(ex.getMessage());
}
return SKIP_BODY;
}

(3) public void doTag() throws JspException {
PageContext pc = (PageContext) getJspContext();
JspWriter out=pc.getOut();
try {
String value = pc.getParameter(this.name);
if(value == null) value = "";
out.write("<input type=\"text\" name=\""+this.name+"\" value=\""+value+"\"/>");
} catch (java.io.IOException ex) {
throw new JspException(ex.getMessage());
}
}


(4) public void doTag() throws JspException {
PageContext pc = (PageContext) getJspContext();
JspWriter out=pc.getOut();
try {
String value = pc.getRequest().getParameter(this.name);
if(value == null) value = "";
out.write("<input type=\"text\" name=\""+this.name+"\" value=\""+value+"\"/>");
} catch (java.io.IOException ex) {
throw new JspException(ex.getMessage());
}
}


This question is from Enthuware.

The correct answer given is 4.

But according to me the correct answer should be 2.

Please do advice.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer given is wrong. Hope you didn't pay too much for this test
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James can you please justify your answer.
 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First two should be wrong, because doStartTag returns int.

Fourth should be the correct answer , Assume this class extends SimpleTagSupport.
[ August 27, 2008: Message edited by: deepa raj ]
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell the source of the question
 
siyabonga khanyile
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure but I think 1 and 2 is wrong cos the classic tags do not have a getJspContext() method like Simple tags , but i see they use it there to get PageContext object.
 
Milind Patil
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. First answer is wrong because doStartTag returning void. getJspContext() method is not present in classic tags( why classic tags because code is using doStartTag () method).

2. Second anwer is wrong for the same reson as first one + its returning value although return type is void.

3. Third is incorrect because of line "String value = pc.getParameter(this.name);" PageContext don't have method called getParameter(String).

4. Fourth is correct one.
[ August 27, 2008: Message edited by: Milind Patil ]
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome man, that explain's the reason.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic