将变量从一个jsp发送到另一个jsp [英] sending variable from one jsp to another jsp
问题描述
我有一个JSP文件为 jsp 1.jsp
,另一个JSP文件为 jsp 2.jsp
I have one JSP file as jsp 1.jsp and another JSP file as jsp 2.jsp
我使用<%@ include file =jsp 2在 jsp 1.jsp 中包含 jsp 2.jsp 。 jsp%>
I've included jsp 2.jsp in jsp 1.jsp using <%@include file="jsp 2.jsp" %>
现在我需要在某个元素上进行点击事件。在那个事件上我想把一个字符串变量转移到包含jsp。
Now I need a click event on some element. And on that event I want to transfer a string variable to included jsp.
假设我有一个列表,点击它我想传输列表的名称到另一个JSP,
Lets say I have a list and on click of it I want to transfer the name of the list to another JSP,
在另一个JSP中,我试图使用该字符串来执行某些任务。
And in another JSP I am trying to use that string to carry out some task.
我正在做所有这些没有任何servlet。
挑战一个!!
我已经google了很多,但没有找到任何东西。
And I am doing all these without any servlet. challenging one!! I have google'd a lot, but didnt find anything.
推荐答案
你有很多选择:
-
将其存储在会话中。
Store it in the session.
// Memorise any passed in user.
String username = request.getParameter("username");
if (username != null && username.length() > 0) {
session.setAttribute("username", username);
}
将其存储为表单中的隐藏字段。
Store it as a hidden field in the form.
<input name="username" type="hidden" value=""/>
将其存储在cookie中。
Store it in a cookie.
username = getCookie(userCookieName);
// Get from cookie.
function getCookie(name) {
if (document.cookie) {
index = document.cookie.indexOf(name);
if (index !== -1) {
f = (document.cookie.indexOf("=", index) + 1);
t = document.cookie.indexOf(";", index);
if (t === -1) {
t = document.cookie.length;
}
return(document.cookie.substring(f, t));
}
}
return ("");
}
在sessionStorage中将其保留在客户端。有关详细信息,请参见此处。
sessionStorage.setItem("username", "...");
不是另一种选择,只是一种机制 - 在URL中传递它:
Not really another option but a mechanism - pass it in the URL:
.... onclick="window.location='details.jsp?username=...'
这篇关于将变量从一个jsp发送到另一个jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!