【问题标题】:How to pass a value from one jsp to another jsp page?如何将值从一个 jsp 传递到另一个 jsp 页面?
【发布时间】:2014-04-17 15:52:45
【问题描述】:

我有两个 jsp 页面:search.jspupdate.jsp

当我运行search.jsp 时,会从数据库中获取一个值,并将该值存储在一个名为scard 的变量中。现在,我想要的是在另一个 jsp 页面中使用该变量的值。我不想使用request.getparameter()

这是我的代码:

<% 
String scard = "";
String id = request.getParameter("id");

try {
    String selectStoredProc = "SELECT * FROM Councel WHERE CouncelRegNo ='"+id+"'";

    PreparedStatement ps = cn.prepareStatement(selectStoredProc);
    ResultSet rs = ps.executeQuery();

    while(rs.next()) {
        scard = rs.getString(23);
    }

    rs.close();
    rs = null;
} catch (Exception e) {
    out.println(e.getLocalizedMessage());
} finally {

}
%>

我怎样才能做到这一点?

【问题讨论】:

  • 会话属性,去吧!
  • 你如何解释演示
  • 你可以使用会话,或者如果是无状态使用cookies
  • 这些是关键字,使用它们。

标签: java jsp


【解决方案1】:

使用查询参数

<a href="edit.jsp?userId=${user.id}" />  

使用隐藏变量。

<form method="post" action="update.jsp">  
...  
   <input type="hidden" name="userId" value="${user.id}">  

您可以发送 Using Session 对象。

   session.setAttribute("userId", userid);

现在,只要您的会话仍处于活动状态,这些值就可以从任何 jsp 获得。

   int userid = session.getAttribute("userId"); 
【解决方案2】:

使用会话

在你的 search.jsp 上

使用session.setAttribute("scard","scard") 将您的scard 放入会话中

//the 1st variable is the string name that you will retrieve in ur next page,and the 2nd variable is the its value,i.e the scard value.

然后在您的下一页中使用session.getAttribute("scard") 检索它

更新

<input type="text" value="<%=session.getAttribute("scard")%>"/>

【讨论】:

  • 如何在下一页打印这个疤痕
  • 我如何打印?你的意思是在你的下一页上检索它...使用 session.getAttribute("scard")
【解决方案3】:

使用下面的代码将字符串从一个jsp传递到另一个jsp

A.jsp

   <% String userid="Banda";%>
    <form action="B.jsp" method="post">
    <%
    session.setAttribute("userId", userid);
        %>
        <input type="submit"
                            value="Login">
    </form>

B.jsp

    <%String userid = session.getAttribute("userId").toString(); %>
    Hello<%=userid%>

【讨论】:

    【解决方案4】:

    假设我们想将三个值(u1,u2,u3)从“show.jsp”传递到另一个页面“display.jsp” 制作三个隐藏的文本框和一个自动单击的按钮(使用 javascript)。 //写在'show.jsp'中的代码

    <body>
    <form action="display.jsp" method="post">
     <input type="hidden" name="u1" value="<%=u1%>"/>
     <input type="hidden" name="u2" value="<%=u2%>" />
     <input type="hidden" name="u3" value="<%=u3%>" />
     <button type="hidden" id="qq" value="Login" style="display: none;"></button>
    </form>
      <script type="text/javascript">
         document.getElementById("qq").click();
      </script>
    </body>
    

    //要写在'display.jsp'中的代码

     <% String u1 = request.getParameter("u1").toString();
        String u2 = request.getParameter("u2").toString();
        String u3 = request.getParameter("u3").toString();
     %>
    

    如果你想在 javascript 中使用这些 servlet 变量,那么只需编写

    <script type="text/javascript">
     var a=<%=u1%>;
    </script>
    

    希望对你有帮助:)

    【讨论】:

      【解决方案5】:

      How can I send data from one JSP page to another JSP page? 我从上述讨论中过滤掉的最佳答案之一。

      可以通过三种方式完成:

      1. 使用请求属性: 使用您选择的名称设置要在请求属性中发送的值 request.setAttribute("send", "valueToSend") 并使用 request.getAttribute("send"); 在另一个 jsp 上检索它;
      2. 使用会话属性 与上面类似,但使用会话对象而不是请求。
      3. 使用应用程序属性 与上面的 1 和 2 相同,但使用应用程序对象代替请求和会话。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多