Null ouput on the JSP page from Java class
I am getting a null output for the 3 variables on my JSP page. These are my files:
index.jsp
<jsp:useBean id="user" scope="session" class="user.CompileClass" />
<jsp:setProperty property="*" name="user"/>
<HTML>
<BODY>
<FORM METHOD=POST ACTION="Result.jsp">
What's your name? <INPUT TYPE=TEXT NAME=uname SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=mail SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
CompileClass.java
package user;
public class CompileClass {
public String uname;
public String mail;
public int age;
public CompileClass(){
}
public void setUname( String name ) {
uname = name;
}
public void setMail( String name ) {
mail = name;
}
public void setAge( int num ) {
age = num;
}
public String getUname() {
return uname;
}
public String getMail() {
return mail;
}
public int getAge() {
return age;
}
/*public void main()
{
}*/
}
Result.jsp
<jsp:useBean id="user" scope="session" class="user.CompileClass" />
<html>
&开发者_JAVA技巧lt;body>
You entered:<BR>
Name: <%= user.getUname() %><br/>
Email: <%= user.getMail() %><BR>
Age: <%= user.getAge() %><BR>
</body>
</html>
The project containing these files in Eclipse is compiling and running successfully but when I am giving any input for Name *Email* and Age the ouput is null
Any help?
You need to set the bean properties during displaying the result page, not during displaying the start page (simply because there's nothing submitted then). Move
<jsp:setProperty property="*" name="user"/>
from index.jsp
to result.jsp
. Actually, the whole <jsp:useBean>
on index.jsp
is also pointless, you aren't using it anywhere there. You could just remove that line from index.jsp
.
You also need to replace those old fashioned scriptlets by EL to get it to display the real bean properties.
Name: ${user.uname}<br/>
Email: ${user.mail}<br/>
Age: ${user.age}<br/>
精彩评论