我有两个jsp页面: search.jsp
和update.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 { } %>
我怎样才能做到这一点?
使用Query参数
<a href="edit.jsp?userId=${user.id}" />
使用隐藏变量.
<form method="post" action="update.jsp"> ... <input type="hidden" name="userId" value="${user.id}">
您可以发送使用会话对象.
session.setAttribute("userId", userid);
只要您的会话仍处于活动状态,现在可以从任何jsp获取这些值.
int userid = session.getAttribute("userId");
使用会议
在您的search.jsp上
把你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.
然后在下一页中使用 session.getAttribute("scard")
更新
<input type="text" value="<%=session.getAttribute("scard")%>"/>
使用以下代码将字符串从一个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%>