Sending Variable from One Jsp to Another Jsp

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

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

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.

passing value from one jsp file to another jsp file

You cannot access variables declared in a scriptlet in page1 from page2. This is one of the disadvantages of scriptlets. Check out the answer by BalusC.

If you are using JSTL, you can do something like this in page1:

  <c:set var = "salary" scope = "session" value = "666"/>

or with request scope:

  <c:set var = "salary" scope = "request" value = "666"/>

And in page2:

<c:out value = "${salary}"/>

But really, you should be setting variables in your servlets which you can then access anywhere in the JSPs.. The use of scriptlets is highly discouraged.

How to pass an Object from one JSP to other JSP?

1.use url parameters

You can add url parameters such as "jsp2?xxx=xxx&xxx=xxx". Then in controller(jsp2), you can get these parameters by

request.getParameter("xx")

and pass variables to jsp engine.

2.use cookies

You can write js code to save data in cookies and then read data in jsp2

///write cookies
document.cookie="xx="+xx;
///read cookies
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}

3.use session

In controller1(jsp1), save session by

request.getSession().setAttribute(name, value);

In jsp2, read session by

request.getSession().getAttribute(name);


Related Topics



Leave a reply



Submit