使用JavaBeans将表单输入值从一个JSP页面共享到另一个JSP页面?

时间:2017-12-14 17:05:05

标签: java eclipse jsp servlets javabeans

我对JSP和JavaBeans很陌生,并尝试如何将参数值从一个JSP(theForm.jsp)中的表单传递到另一个JSP(theResult.jsp),如下所示:

在我的theResult.jsp

<body>
   <jsp:useBean id="user" class="people.User" scope="application"/>

   Your username is:  <jsp:getProperty name="user" property="username"/><br>
   Your password is:  <jsp:getProperty name="user" property="password"/><br>
</body>

在我的theForm.jsp文件中:

<body>
   <jsp:useBean id="user" class="people.User" scope="application">
      <jsp:setProperty name="user" property="username" value='<%=request.getParameter("username")%>'/>
      <jsp:setProperty name="user" property="password" param="password"/>  // another way to use setProperty, I read
    </jsp:useBean>


   <form action="theResult.jsp" method="post">
      <input type="text" name="username" placeholder="Type your username"><br>
      <input type="password" name="password" placeholder="Type your password"><br>
      <input type="submit" value="submit">
   </form>
</body>

但我的结果显示是:

您的用户名是:null
您的密码为:null

我的people.user javabean:

package people;

public class User implements java.io.Serializable {

    private String username;
    private String password;


    public User(){}

    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }

    //... and same thing for 'password'

}

1 个答案:

答案 0 :(得分:0)

<body>
   <jsp:useBean id="user" class="people.User"/>
   <jsp:setProperty name="user" property="*"/>
   <%-- the (*) symbol indicates the value should come
        from request parameter whose name matches property
        name . Thus simple automatic conversion should be
        performed --%/>
   Your username is: <jsp:getProperty name="user" property="username"/><br>
   Your password is: <jsp:getProperty name="password"/><br>