简体   繁体   English

如何将值从一个jsp传递到另一个jsp页面?

[英]How to pass a value from one jsp to another jsp page?

I have two jsp pages: search.jsp and update.jsp .我有两个 jsp 页面: search.jspupdate.jsp

When I run search.jsp then one value fetches from database and I store that value in a variable called scard .当我运行search.jsp从数据库中获取一个值并将该值存储在一个名为scard的变量中。 Now, what I want is to use that variable's value in another jsp page.现在,我想要的是在另一个 jsp 页面中使用该变量的值。 I do not want to use request.getparameter() .我不想使用request.getparameter()

Here is my code:这是我的代码:

<% 
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 {

}
%>

How can I achieve this?我怎样才能做到这一点?

Using Query parameter使用查询参数

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

Using Hidden variable .使用隐藏变量。

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

you can send Using Session object.您可以使用会话对象发送。

   session.setAttribute("userId", userid);

These values will now be available from any jsp as long as your session is still active.只要您的会话仍然处于活动状态,这些值现在可以从任何 jsp 中获得。

   int userid = session.getAttribute("userId"); 

Use sessions使用会话

On your search.jsp在您的 search.jsp 上

Put your scard in sessions using session.setAttribute("scard","scard")使用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,ie the scard value.

And in your next page you retrieve it using session.getAttribute("scard")在下一页中,您使用session.getAttribute("scard")检索它

UPDATE更新

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

Use below code for passing string from one jsp to another jsp使用下面的代码将字符串从一个 jsp 传递到另一个 jsp

A.jsp jsp

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

B.jsp B.jsp

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

Suppose we want to pass three values(u1,u2,u3) from say 'show.jsp' to another page say 'display.jsp' Make three hidden text boxes and a button that is click automatically(using javascript).假设我们想将三个值(u1,u2,u3)从“show.jsp”传递到另一个页面“display.jsp” 制作三个隐藏的文本框和一个自动点击的按钮(使用javascript)。 //Code to written in 'show.jsp' //代码写入'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>

// Code to be written in 'display.jsp' // 将要写入'display.jsp'的代码

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

If you want to use these variables of servlets in javascript then simply write如果您想在 javascript 中使用这些 servlet 变量,那么只需编写

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

Hope it helps :)希望能帮助到你 :)

How can I send data from one JSP page to another JSP page? 如何将数据从一个 JSP 页面发送到另一个 JSP 页面? One of the best answer which I filtered out from above discussion.我从上述讨论中过滤掉的最佳答案之一。

Can be done in three ways:可以通过三种方式完成:

  1. using request attributes: Set the value to send in request attribute with a name of your choice as request.setAttribute("send", "valueToSend") and retrieve it on another jsp using request.getAttribute("send");使用请求属性:设置要在请求属性中发送的值,并使用您选择的名称作为 request.setAttribute("send", "valueToSend") 并使用 request.getAttribute("send") 在另一个 jsp 上检索它;
  2. using session attributes Similar to above but using session object instead of request.使用会话属性与上面类似,但使用会话对象而不是请求。
  3. using application attributes Same as 1 and 2 above but using application object in place of request and session.使用应用程序属性 与上面的 1 和 2 相同,但使用应用程序对象代替请求和会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM