sending variable from one jsp to another jsp

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

Leave a Comment