Hi all,
i am new to javabeans and JSP, i have some problem to retrieve value from beans, any help will be appreciate.
my index.jsp has a from

<form action="update.jsp" method="POST">
         userName    <input type="text" name="userName" />
            <br>
 
         realName    <input type="text" name="realName" />
            <br>
            <input type="submit" value="Log In" />
        </form>

which POST to update.jsp,
update.jsp retrieves userName

<jsp:useBean id="simple" class="bean.UserBean">
<jsp:setProperty name="simple" property="*"/>
</jsp:useBean>
    <body>
        <h1>  
           User <jsp:getProperty name="simple" property="userName" />
 
                      
</h1>
<input type="button" onclick = "location.href='next.jsp'" value="next">

i am OK at this point. But if i want to go to next.jsp which display realName, i got null.
next.jsp :

<jsp:useBean id="simple" class="bean.UserBean">
 
    </jsp:useBean>
    <body>
        <h1>Hello <jsp:getProperty name="simple" property="realName" /></h1>
    </body>

UserBean :

public class UserBean implements java.io.Serializable {
 
    private String userName;
    private String realName;
 
 
    /** No-arg constructor (takes no arguments). */
    public UserBean() {
        
    }
 
    /**
     * Property <username>name</username> (note capitalization) readable/writable.
     */
    public String getuserName() {
        return this.userName;
    }
 
    /**
     * Setter for property <username>name</username>.
     * @param name
     */
    public void setuserName(String userName) {
        this.userName = userName;
    }
 
    /**
     * Property <realname>name</realname> (note capitalization) readable/writable.
     */
    public String getRealName() {
        return this.realName;
    }
 
    /**
     * Setter for property <realname>name</realname>.
     * @param name
     */
   public void setRealName(String realName) {
        this.realName = realName;
    }
}

I am really new to JavaBeans, any help please!

Recommended Answers

All 4 Replies

It is because you don't pass "realName" from update.jsp to next.jsp in the request.
Maybe you can put them as hidden fields at the update.jsp. (I don't really use javabeans)

<form name="formName" action="next.jsp">

<input type="hidden" name="realName" value="the value from the bean" />
<input type="hidden" name="userName" value"the value from the bean" />

<input type="submit" value="next" />
</form>

To tell you the truth, I don't use javabeans so I can't tell you for sure how to pass their values to the hidden fields. If it was my project I would have first look for some tutorials or examples on javabeans

hi javaAddict,
thanks for your reply, i had thought to put object entities into hidden field, however, things are more complex than what i thought as i have about ten different entities, storing all of them into a long single input field or into ten different input fields is a really pain, and this is the reason i choose to use Beans.

I had looked some examples from Google, which only help me to update.jsp stage, so it will be great if anyone can give me examples (links or anything) about passing Beans around many JSP pages.

Thanks again, this is my first post here even i had been using this forum for a while.

Any other suggestions??

Is your problem that you need to pass the entire object without having to declare different hidden fields?

The only think I can think right now is to put the object directly in the request:

<%
request.setAttribute("USER", simple);
%>

And get it at the next.jsp like this:

<%
bean.UserBean simple =  (bean.UserBean)request.getAttribute("USER");
%>

Hi javaAddict,
Thanks for you suggestion, and it works.
However, i just have another question.
If i run the JSP pages, there will be a User object stored in the request,
then i run the JSP pages again without closing the first JSP pages, i believe that the previous User object would be rewritten (If reload the first JSP page, i will get the latest User object).

So is that right which there can only be one instance stored in the request at any time.
if the answer is YES, then i am confused about "What is session?".
A session is an object for each visitor, right? But how could the second visiting rewrite User object of first visiting.

I hope you can understand my English.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.