我有一个 JSP 文件 jsp 1.jsp 另一个 JSP 文件为 jsp 2.jsp
我已使用 <%@include file="jsp 2.jsp" %>
将 jsp 2.jsp 包含在 jsp 1.jsp 中
现在我需要某个元素上的单击事件。在该事件中,我想将一个字符串变量传输到包含的 jsp 中。
假设我有一个列表,单击它时我想将列表的名称传输到另一个 JSP,
在另一个 JSP 中,我尝试使用该字符串来执行某些任务。
我在没有任何 servlet 的情况下完成所有这些工作。 具有挑战性的一个! 我用谷歌搜索了很多,但没有找到任何东西。
最佳答案
您有多种选择:
将其存储在 session 中。
// Memorise any passed in user. String username = request.getParameter("username"); if (username != null && username.length() > 0) { session.setAttribute("username", username); }
将其存储为表单中的隐藏字段。
<input name="username" type="hidden" value=""/>
将其存储在 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 中。请参阅here了解详情。
sessionStorage.setItem("username", "...");
这并不是另一种选择,而是一种机制 - 将其传递到 URL 中:
.... onclick="window.location='details.jsp?username=...'
关于java - 将变量从一个 jsp 发送到另一个 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18763168/