-3

I am trying to pass two values from JSP1 to JSP2. I am using the below given codes in JSP1 and JSP2. I want to display the values of key and value in JSP2 but the output given in "Output" section below is getting displayed. I want these values to be passed to JSP2 to perform some logic on them. I want to move back to JSP1 after the logic in JSP2 get executed.

So JSP1 gets values from Controller class in Java. There is a loop in JSP1 and JSP2 is called inside the loop in JSP1. Now I am not able to access the values that are being passed from loop but I am able to access the values sent from Controller class to JSP1 in both JSP1 and JSP2. So the issue is being caused as the values from loop are not being passed to JSP2.

JSP1

<% request.setAttribute("key", "${entityitem.key}"); %>
<% request.setAttribute("value", "${entityitem.value}"); %>
<jsp:include page="JSP2.jsp"/>

JSP2

<%= request.getAttribute("key") %>  :    <br>
<%= request.getAttribute("value") %>

Output:

${entityitem.key}
${entityitem.value}

Desired output: Values in

${entityitem.key}
${entityitem.value}     

5 Answers5

0

You are trying to get those values after you submit you request? If not you should use the ${entityitem.key} and ${entityitem.value} in JSP2.

If you submit it and you want to have access to the variables , you need to have a forward and not a redirect request or else the variables will be erased, as you'll get a new request.

In you case you should check that you properly initialized the variables and try to use request.getSession().setAttribute() instead of request.setAttribute() to check if the variables are passing correctly.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • I want to send those values from JSP1 to JSP2 in order to perform some logic in JSP2 and then I want to control to come back to JSP1 to perform some other logic. – krishna chaitanya Jun 30 '15 at 05:01
  • So JSP1 gets values from Controller class in Java. There is a loop in JSP1 and JSP2 is called inside the loop in JSP1. Now I am not able to access the values that are being passed from loop but I am able to access the values sent from Controller class to JSP1 in both JSP1 and JSP2. So the issue is being caused as the values from loop are not being passed to JSP2. – krishna chaitanya Jun 30 '15 at 05:34
0

You have included your JSP2.jsp in your JSP1.jsp. ON compilation these jsp are compiled and rendered in the browser as single page .

So instead of the setting the Objects again in Request scope , you can directly access them in the JSP2.jsp like how you do in the JSP1.jsp.

Directly access it like how you do in the JSP2.jsp

side note : Avoid using scriplets , read How to avoid Java code in JSP files?

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • I tried that before and it didnt work. I will retry it now. Should I use - to call JSP2 in my JSP1? – krishna chaitanya Jun 30 '15 at 05:04
  • You can either write it like that or else as `<%@ include file="relative url" >` . i guess your page has `EL` disabled . please check whether you have this line `<%@ page isELIgnored="false" %>` , if so remove it – Santhosh Jun 30 '15 at 05:08
  • Tried it and I am not getting the values in JSP2. :( I am getting ${entityitem.key} ${entityitem.value} values from a loop in JSP1 and I am passing those to JSP2. Is that the issue? Will I be able to access the values in JSP2 if they are being sent from a loop in JSP1. I guess theoretically it should work. – krishna chaitanya Jun 30 '15 at 05:12
  • if the second page is included in the first jsp , you needn't explicitly pass the objects , you can directly access them – Santhosh Jun 30 '15 at 05:17
  • Ok. I did try accessing them. I am doing ${entityitem.key} :
    ${entityList} in JSP2 and its just printing : even though entityitem.key and entityitem.value has values in them
    – krishna chaitanya Jun 30 '15 at 05:19
  • So JSP1 gets values from Controller class in Java. There is a loop in JSP1 and JSP2 is called inside the loop in JSP1. Now I am not able to access the values that are being passed from loop but I am able to access the values sent from Controller class to JSP1 in both JSP1 and JSP2. So the issue is being caused as the values from loop are not being passed to JSP2. – krishna chaitanya Jun 30 '15 at 05:32
0

please check the following things

please make sure you remove this line from your jsp page

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

and ensure you are using servlet version 2.4 or higher

in your case please dont include a jsp page if you want to pass some values .instead redirect using request.getRequestDispatcher() to second jsp page and pass the values from second jsp page using the same requestdispatcher i think you will get the value

Richard Elite
  • 720
  • 5
  • 15
0

You want to send values from one jsp into another jsp but the problem is that how will this two page communicate with each other ? You have to call one jsp into another jsp if not you can set session variable and try to find the value in the another variable. you are doing same here but the problem is that in session set attribute you aren't setting the value using el expression you are passing the string.

you can use cookies and you can use the value in the next jsp. if possible use requestdispatcher because it is better than everything.

Sagar Rout
  • 651
  • 1
  • 12
  • 29
  • JSP1 gets values from Controller class in Java. There is a loop in JSP1 and JSP2 is called inside the loop in JSP1. Now I am not able to access the values that are being passed from loop but I am able to access the values sent from Controller class to JSP1 in both JSP1 and JSP2. So the issue is being caused as the values from loop are not being passed to JSP2. I am calling JSP2 in JSP1 using - . Will I be able to access the values in loop in JSP1 in JSP2 using cookies/request dispatcher? – krishna chaitanya Jun 30 '15 at 05:40
0

I was able to access the values in loop in JSP1 in JSP2 using the below code.

Code in JSP1:

<jsp:include page="JSP2.jsp">
<jsp:param name="listKey" value="${entityitem.key}"/>
<jsp:param name="listValue" value="${entityitem.value}"/>
</jsp:include>

Code in JSP2:

 ${param.listKey}