java - 将变量从一个 jsp 发送到另一个 jsp

标签 java javascript jsp session servlets

我有一个 JSP 文件 jsp 1.jsp 另一个 JSP 文件为 jsp 2.jsp

我已使用 <%@include file="jsp 2.jsp" %>jsp 2.jsp 包含在 jsp 1.jsp

现在我需要某个元素上的单击事件。在该事件中,我想将一个字符串变量传输到包含的 jsp 中。

假设我有一个列表,单击它时我想将列表的名称传输到另一个 JSP,

在另一个 JSP 中,我尝试使用该字符串来执行某些任务。

我在没有任何 servlet 的情况下完成所有这些工作。 具有挑战性的一个! 我用谷歌搜索了很多,但没有找到任何东西。

最佳答案

您有多种选择:

  1. 将其存储在 session 中。

    // Memorise any passed in user.
    String username = request.getParameter("username");
    if (username != null && username.length() > 0) {
      session.setAttribute("username", username);
    }
    
  2. 将其存储为表单中的隐藏字段。

    <input name="username" type="hidden" value=""/>
    
  3. 将其存储在 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 ("");
    }
    
  4. 将其保留在客户端的 sessionStorage 中。请参阅here了解详情。

    sessionStorage.setItem("username", "...");
    
  5. 这并不是另一种选择,而是一种机制 - 将其传递到 URL 中:

    .... onclick="window.location='details.jsp?username=...'
    

关于java - 将变量从一个 jsp 发送到另一个 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18763168/

相关文章:

java - 稍后在程序中删除一行文本

java - 即使 subscribeOn() 在另一个线程上调用,Observable 也会在主线程上运行

javascript - 使用javascript从现有对象创建一个对象

java - 将 servlet 链接到 jsp。

java - 如何使用 barcode4j 生成随机条形码代码?

java - Class#getDeclaredMethods() 返回继承的方法

javascript - 只考虑页面速度,什么时候 CSS 或 JS 大到可以外化

javascript - 在 React 中使用 useState 设置 promise 状态时应用程序不断重新渲染

java - 比较 EL 中的字符串

java - < Spring :message> tag not working with resource bundles from database