3

I have two jsp files, jsp1 and jsp2. When an edit button clicked on jsp1 will call a java script function . From javascript function i am calling jsp2. I used window.open beacuse i need this jsp2 as opens as a popup.Now i want to pass an object from jsp1 to jsp2 when i click edit button.How can i do that?.

    This is my code

    jsp1

    < script language="JavaScript">

            function edit(){

                    window.open("jsp2.jsp",'popuppage',"height=600,width=800,status=yes,toolbar=no,menubar=no,location=no");
                    return true;

                }
    < /script>

     < stripes:form name="upload"
                        action="/upload.action" enctype="multipart/form-data">
          < table>

         <% List<TableDto> list = (List<TableDto>)request.getAttribute("table"); %>

                            <%
                                if(list != null){
                                 for (TableDto dto : list) { 
                            %>
                      < tr>
                                <td><%=dto.getName()%></td>
                                <td><img src="<%=dto.getPath() %>" width="40px"
                                    height="40px" /></td>
                                <td><%=dto.getText()%></td>
                                <td>


                                <input type="image" name="edit"onclick="edit()"
                                    src="${globalPath}/images/edit_pen.jpg">

                    < /tr>

                            <%
                                } 
                                }
                            %>

          </table>
    < /stripes:form>
Aragon
  • 143
  • 2
  • 9
  • You can try to use `postMessage` https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage or add get parameter `window.open("jsp2.jsp?foo=bar",...` – jcubic Mar 11 '16 at 09:33
  • 1
    http://stackoverflow.com/questions/18763168/sending-variable-from-one-jsp-to-another-jsp – Frederik Witte Mar 11 '16 at 09:37
  • @FrederikWitte it's different question because it use `include` to load second file. – jcubic Mar 11 '16 at 09:42

5 Answers5

2

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

you need to use

  • If you are using forward (jsp:foprward or RequestDispatcher) from one page to another, then use request.setAttribute(..) and request.getAttribute(), because you are within the same request

  • If you are using redirect (via response.sendRedirect()), then use request.getSession().setAttribute(..) and request.getSession().getAttribute().

Harshil Kulkarni
  • 411
  • 5
  • 20
0

Below are ways to pass values from one page to another page -

  1. Putting values into the session object.
  2. Putting values into the application object
  3. Putting values at the end of the redirect URL.
Amit Naik
  • 983
  • 1
  • 5
  • 16
0

The following sample works for me. The first page contains handler function:

function popup() {
    window.data = {name : 'myName'};
    var handle = window.open('importJSON.html', 'popup',"height=600,width=400,status=yes,toolbar=no,menubar=no,location=no");
}

The second page contains handler function:

function showName2() {
    alert(window.opener ? window.opener.data.name : undefined);
}

Tested on Chromium 47 and Firefox 43. It doesn't throws exception 'Blocked a frame with origin "null"... Protocols, domains, and ports must match' if both pages opened from the same host. It looks to me better not to send a data object through a network back and forth if it is possible not to do that.

beemaster
  • 291
  • 1
  • 3
  • 13
-1

You can set the object in an attribute which you can store in the session and in the other jsp , you can retrieve from the session and use.

Kalyan Chavali
  • 1,330
  • 8
  • 24