• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock question

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
The contents of beans class are as follows:-
package projsp;
public class trying
{
private String name;
private int age;
public void setName(String n)
{
name=n;
}
public void setAge(int a)
{
age=a;
}

public int getAge()
{
return age;
}
public String getName()
{
return name;
}
}
The contents of test.jsp file are as follows:
<%@ page language="java" import="projsp.*" %>
<jsp:useBean id="test" class="projsp.trying" />
<html>
The Name is <getProperty name="test" property="name" />
The Age is < jsp:getProperty name="test" property="age" />
</html>
If a user types in http://servername:8080/test.jsp?name=Anand&age=22 the out put is

The correct answer is The Name is null and The Age is null as the properties are not
set prior to accessing them,adding <jsp:setProperty name="test" property="*">
would give output as The Name is Anand and The Age is 22

<b>why does'nt it print nae is null and age =0??</b>
 
James Kwan
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into this question with the mock exam too. I think it may be an error since the default for age should be 0 as it is declared as an int. Can anyone clearify this?
-James
SCJP,SCJD,SCWCD
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using <jsp:setProperty name="test" property="*"> will map the request parameters to the bean fields in this case name and age .so u will get anand and 22.
 
Ray Smilgius
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont forget to terminate the end of the jsp:setproperty
<jsp:setProperty name="test" property="*"/>
peace
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
no answer???
 
alex lamb
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,
I agree with James as well. I just ran the example on Tomcat 4.0 . Returns 0 as we all expected it to.
alex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic