Java 如何将值从一个jsp传递到另一个jsp页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22369717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a value from one jsp to another jsp page?
提问by
I have two jsp pages: search.jsp
and update.jsp
.
我有两个 jsp 页面: search.jsp
和update.jsp
.
When I run search.jsp
then one value fetches from database and I store that value in a variable called scard
. Now, what I want is to use that variable's value in another jsp page. I do not want to use request.getparameter()
.
当我运行时,search.jsp
从数据库中获取一个值并将该值存储在一个名为scard
. 现在,我想要的是在另一个 jsp 页面中使用该变量的值。我不想使用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?
我怎样才能做到这一点?
采纳答案by Benjamin
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");
回答by Santino 'Sonny' Corleone
Use sessions
使用会话
On your search.jsp
在您的 search.jsp 上
Put your scard
in sessions using session.setAttribute("scard","scard")
把你scard
的会话使用session.setAttribute("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.
//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.
And in your next page you retrieve it using session.getAttribute("scard")
在下一页中,您可以使用检索它 session.getAttribute("scard")
UPDATE
更新
<input type="text" value="<%=session.getAttribute("scard")%>"/>
回答by Raman B
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%>
回答by Ankit Saxena
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). //Code to written in 'show.jsp'
假设我们想将三个值(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>
// 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 :)
希望能帮助到你 :)