在JSP页面中调用另一个JSP页面中的变量

      

         在jsp学习中,经常需要在一个jsp页面中调用另一个jsp页面中的变量,下面就这几天的学习,总结一下。


         jsp页面之间的变量调用有多种方法:

       

        1、通过jsp的内置对象—request对象获取参数:

         (1)通过超链接传参:

                 例:把a.jsp中i的值传到b.jsp中:

                      在a.jsp页面中的核心代码为:

                           <a href="b.jsp?i=1">传参</a>     (说明:给i赋值时也可以用jsp表达式,例如i=<%变量名 %>)

                      在b.jsp页面中的核心代码为:

                          <%String j=request.getParameter("i"); %>

                          <%=j %>                      

          (2)通过表单传参:

                  例:把a.jsp中表单中的name的值传送到b.jsp中:

                       在a.jsp页面中的核心代码为:

                            <form action="b.jsp" method="post">

                                 <ul>

                                       <li><input type="text" name="username">

                                       <li>w<input type="submit" name="submit" value="提交">

                                 <ul>

                            </form>

                       在b.jsp页面中的核心代码为;

                            <%String getUserName=request.getParameter("username");>

                            <%=getUserName %>

          (3)通过表单的隐藏域传参:

                   例:把a.jsp中定义的变量传送到b.jsp中;

                        在a.jsp中的核心代码为:

                             <%!int a=4; %>
                             <form action="b.jsp" method="post">
                                   <input type="hide" name="b" value=<%a%>>

                                   <input type="submit" value="提交">

                             </form>

                        在b.jsp中的核心代码为:

                             <%int c=(Integer)request.getAttribute("b"); %>    

                            (上段代码或者为<%String c=request.getAttribute("c").toString(); %>等等,类型转换通java一样)
                             <%=c %>

                        运行结果:变量a=4的值通过表单隐藏域b传到b.jsp中的c中。

             

           2、通过JavaBean获取表单信息:

            例:用JavaBean获取用户登录表单信息:

                 Login.jsp中的核心代码为:

                   <form action="Deal.jsp" method="post">

                         <ul>

                              <li>用户名:<input type="text" name="username">

                              <li>密码:<input type="password" name="password">

                              <li><input type="submit" name="submit" value="登录">

                         </ul>

                  </form>

                JavaBean中的核心代码为:

                   package com;

                   public class UserBean{

                          private String username;

                          private String password;

                          public String getUserName(){

                                 return username;

                          }

                          public void setUserName(String username){

                                 this.username=username; 

                          }

                          public String getPassword(){

                                 return password;

                          }

                          public void setPassword(String password){

                                 this.password=password;

                          }

                    }

                 Deal.jsp中的核心代码为:

                   <jsp:useBean id="user" class="com.UserBean" >

                   <jsp:setProperty name="user" property="*">

                   <%String username=user.getUserName();%>

                   <%String password=user.getPassword();%>

                   得到的用户名为:<%=username%><br>

                   得到的密码为:<%=password%>

              其中,Deal.jsp中的<jsp:setProperty name="user" property="*">中的星号是表示对所有同名的属性赋值,当然还可以对属性选择性地赋值: <jsp:setProperty name="user" property="username">、<jsp:setProperty name="user" property="password">等。


           当然肯定还有更好更多的方法,期待高手赐教。


 

你可能感兴趣的:(jsp)