sending variable from one jsp to another jsp

前端 未结 5 643
伪装坚强ぢ
伪装坚强ぢ 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: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.

      
      
    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=...'
      

提交回复
热议问题