sending variable from one jsp to another jsp

前端 未结 5 562
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 13:01

I have one JSP file as jsp 1.jsp and another JSP file as jsp 2.jsp

I\'ve included jsp 2.jsp in jsp 1.jsp

相关标签:
5条回答
  • 2020-11-28 13:37

    You can set a jsp:param, as in this example jsp:param element or take a look at this similar post Passing parameters between JSP pages.

    0 讨论(0)
  • 2020-11-28 13:43

    in jsp1 use this way to pass value.

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

    in jsp2 to get value

    String value=request.getParameter("hidden");
    
    0 讨论(0)
  • 2020-11-28 13:50

    Try to use Ajax with JQuery to achieve your goal or you can simply store the String object in session and you can delete it in jsp2.jsp when ever you use it. or you can simply append to a url as query string there are multiple ways among them the above are few.

    These questions may help you

    Question 1

    Question 2

    Question 3

    Question 4

    0 讨论(0)
  • 2020-11-28 13:52

    If the reason for including your jsp 2 in jsp 1 is for sending variable, then its not needed. you just need to set a hidden variable in jsp 1, and in your jsp 2 you should able to access it, by using the jsp implicit request object.

    0 讨论(0)
  • 2020-11-28 13:57

    You have a number of options:

    1. 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);
      }
      
    2. Store it as a hidden field in the form.

      <input name="username" type="hidden" value=""/>
      
    3. 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 ("");
      }
      
    4. Persist it on the client side in sessionStorage. See here for details.

      sessionStorage.setItem("username", "...");
      
    5. Not really another option but a mechanism - pass it in the URL:

      .... onclick="window.location='details.jsp?username=...'
      
    0 讨论(0)
提交回复
热议问题