0

I'm trying to set 2 variables in 1 JSP:

session.setAttribute("name", aName);
session.setAttribute("amount", amount);
response.sendRedirect("transferSuccess.jsp");

And trying to access this information from another JSP:

<h1>
   Transfer successful! Customer 
   <% session.getAttribute("name");%> 
   received $
   <% session.getAttribute("amount");%>.
</h1>

what's the best way to do this? My many google searches have either not worked or left me confused.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 1
    Using lots of java code in `JSP` isn't a very good practice. – itwasntme Sep 06 '15 at 21:40
  • 1
    I think it should be `<%=session.getAttribute("name")%>` or `<% out.print(session.getAttribute("name"));%>` –  Sep 06 '15 at 22:18

1 Answers1

2

You are using <% // statements %> to print the values, but this is used to embed java code into jsp.<%= //expressions %> using this we can embed the java expression to jsp and with this we can print the values or we can assign the values to html elements.

Find the below code:

<h1>
  Transfer successful! Customer 
  <%= session.getAttribute("name");%> 
  received $
  <%= session.getAttribute("amount");%>.

My suggestion is avoid java code in jsp, as much as possible, you can achieve the above using JSTL and EL. Find the below example:

<h1>
  Transfer successful! Customer 
  ${requestScope.name} 
  received $
  ${requestScope.amount}
</h1>

You can use ${sessionScope.name} if you get value from the session.

Alexander Ziubin
  • 35
  • 1
  • 2
  • 7
Srinu Chinka
  • 1,471
  • 13
  • 19