Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - sending variable from one jsp to another jsp

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 using <%@include file="jsp 2.jsp" %>

Now I need a click event on some element. And on that event I want to transfer a string variable to included jsp.

Lets say I have a list and on click of it I want to transfer the name of the list to another JSP,

And in another JSP I am trying to use that string to carry out some task.

And I am doing all these without any servlet. challenging one!! I have google'd a lot, but didnt find anything.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...