May*_*pta 2 javascript java session jsp servlets
我有一个JSP文件作为jsp 1.jsp ,另一个JSP文件作为jsp 2.jsp
我已经包括JSP 2.jsp在JSP 1.jsp页面使用<%@include file="jsp 2.jsp" %>
现在我需要一些元素上的click事件.在那个事件上,我想将一个字符串变量传递给包含的jsp.
让我说我有一个列表,点击它我想将列表的名称转移到另一个JSP,
在另一个JSP中,我试图使用该字符串来执行某些任务.
我正在做所有这些没有任何servlet.挑战一个!! 我已经google了很多,但没有找到任何东西.
Old*_*eon 12
你有很多选择:
将其存储在会话中.
// Memorise any passed in user.
String username = request.getParameter("username");
if (username != null && username.length() > 0) {
session.setAttribute("username", username);
}
Run Code Online (Sandbox Code Playgroud)将其存储为表单中的隐藏字段.
<input name="username" type="hidden" value=""/>
Run Code Online (Sandbox Code Playgroud)将其存储在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 ("");
}
Run Code Online (Sandbox Code Playgroud)在sessionStorage中将其保留在客户端.详情请见此处.
sessionStorage.setItem("username", "...");
Run Code Online (Sandbox Code Playgroud)不是另一种选择,而是一种机制 - 在URL中传递它:
.... onclick="window.location='details.jsp?username=...'
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
52385 次 |
| 最近记录: |