How to Pass a Value from One Jsp to Another Jsp Page

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

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"); 

How to pass the values from one jsp page to another jsp without submit button?

You just have to include following script.

<script language="javascript" type="text/javascript">  
var xmlHttp
function showState(str)
{
//if you want any text box value you can get it like below line.
//just make sure you have specified its "id" attribute
var name=document.getElementById("id_attr").value;
if (typeof XMLHttpRequest != "undefined")
{
xmlHttp= new XMLHttpRequest();
}
var url="forwardPage.jsp";
url +="?count1=" +str+"&count2="+name";
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("div_id").innerHTML=xmlHttp.responseText
}
}
</script>

So if you got the code, let me tell you, div_id will be id of div tag where you have to show your result.
By using this code, you are passing parameters to another page. Whatever the processing is done there will be reflected in div tag whose id is "div_id".
You can call showState(this.value) on "onChange" event of any control or "onClick" event of button not submit.
Further queries will be appreciated.

How to pass a value from loop of JSP page to another JSP page?

1st : your overwriting the session in while loop so only you always getting last row value .

2nd : Just pass it like query string get parameter like below

 <TD><a href="feepaypage.jsp?rollno=<%=resultset.getString(2) %>" >Pay Fee</a></TD>

3rd : Access the get parameter like below

<%
String id=request.getParameter("rollno").toString();
int idd=Integer.parseInt(id);
%>

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


Related Topics



Leave a reply



Submit