简体   繁体   中英

How to pass a value from one jsp to another jsp page?

I have two jsp pages: search.jsp and update.jsp .

When I run search.jsp then one value fetches from database and I store that value in a variable called scard . Now, what I want is to use that variable's value in another jsp page. I do not want to use request.getparameter() .

Here is my code:

<% 
String scard = "";
String id = request.getParameter("id");

try {
    String selectStoredProc = "SELECT * FROM Councel WHERE CouncelRegNo ='"+id+"'";

    PreparedStatement ps = cn.prepareStatement(selectStoredProc);
    ResultSet rs = ps.executeQuery();

    while(rs.next()) {
        scard = rs.getString(23);
    }

    rs.close();
    rs = null;
} catch (Exception e) {
    out.println(e.getLocalizedMessage());
} finally {

}
%>

How can I achieve this?

Using Query parameter

<a href="edit.jsp?userId=${user.id}" />  

Using Hidden variable .

<form method="post" action="update.jsp">  
...  
   <input type="hidden" name="userId" value="${user.id}">  

you can send Using Session object.

   session.setAttribute("userId", userid);

These values will now be available from any jsp as long as your session is still active.

   int userid = session.getAttribute("userId"); 

Use sessions

On your search.jsp

Put your scard in sessions using session.setAttribute("scard","scard")

//the 1st variable is the string name that you will retrieve in ur next page,and the 2nd variable is the its value,ie the scard value.

And in your next page you retrieve it using session.getAttribute("scard")

UPDATE

<input type="text" value="<%=session.getAttribute("scard")%>"/>

Use below code for passing string from one jsp to another jsp

A.jsp

   <% String userid="Banda";%>
    <form action="B.jsp" method="post">
    <%
    session.setAttribute("userId", userid);
        %>
        <input type="submit"
                            value="Login">
    </form>

B.jsp

    <%String userid = session.getAttribute("userId").toString(); %>
    Hello<%=userid%>

Suppose we want to pass three values(u1,u2,u3) from say 'show.jsp' to another page say 'display.jsp' Make three hidden text boxes and a button that is click automatically(using javascript). //Code to written in 'show.jsp'

<body>
<form action="display.jsp" method="post">
 <input type="hidden" name="u1" value="<%=u1%>"/>
 <input type="hidden" name="u2" value="<%=u2%>" />
 <input type="hidden" name="u3" value="<%=u3%>" />
 <button type="hidden" id="qq" value="Login" style="display: none;"></button>
</form>
  <script type="text/javascript">
     document.getElementById("qq").click();
  </script>
</body>

// Code to be written in 'display.jsp'

 <% String u1 = request.getParameter("u1").toString();
    String u2 = request.getParameter("u2").toString();
    String u3 = request.getParameter("u3").toString();
 %>

If you want to use these variables of servlets in javascript then simply write

<script type="text/javascript">
 var a=<%=u1%>;
</script>

Hope it helps :)

How can I send data from one JSP page to another JSP page? One of the best answer which I filtered out from above discussion.

Can be done in three ways:

  1. using request attributes: Set the value to send in request attribute with a name of your choice as request.setAttribute("send", "valueToSend") and retrieve it on another jsp using request.getAttribute("send");
  2. using session attributes Similar to above but using session object instead of request.
  3. using application attributes Same as 1 and 2 above but using application object in place of request and session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM