2

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

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

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

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

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

我在没有任何 servlet 的情况下完成所有这些工作。挑战一个!!我已经google了很多,但没有找到任何东西。

4

5 回答 5

12

您有多种选择:

  1. 将其存储在会话中。

    // 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 中的客户端。有关详细信息,请参见此处

    sessionStorage.setItem("username", "...");
    
  5. 不是真正的另一种选择,而是一种机制 - 在 URL 中传递它:

    .... onclick="window.location='details.jsp?username=...'
    
于 2013-09-12T11:55:11.293 回答
2

jsp 2如果包含你的原因jsp 1是为了发送变量,那么它的not needed. 您只需要在 中设置一个hidden变量jsp 1,并且jsp 2您应该能够通过使用该jsp implicit request对象来访问它。

于 2013-09-12T11:45:04.597 回答
1

尝试使用Ajax with JQuery来实现您的目标,或者您可以简单地将 String 对象存储在其中,并且您可以在使用它时session将其删除。jsp2.jsp或者您可以简单地附加到 a url,因为query string其中有多种方法,上述方法很少。

这些问题可能对你有所帮助

问题 1

问题2

问题 3

问题 4

于 2013-09-12T11:48:30.480 回答
0

您可以设置一个jsp:param,如本示例中的jsp:param 元素或查看类似的帖子在 JSP 页面之间传递参数

于 2013-09-12T11:50:12.927 回答
0

在 jsp1 中使用这种方式传递值。

<form action="">
<input type="hidden" name="hidden" value="hidden">
<input type="submit" value="submit"></form>

在jsp2中获取价值

String value=request.getParameter("hidden");
于 2013-09-12T11:47:28.043 回答