0

I'm trying to use the setAttribute and getAttribute functions to store session variables. I have a jsp page Dashboard.jsp which includes

<%
session.setAttribute("test", "value");
out.print(session.getAttribute("test"));
%>
<a href = "temp.jsp">temp</a>

and another jsp page temp.jsp which just has

<%
out.print(session.getAttribute("test"));
%>

In the Dashboard page, it correctly outputs "value", but in the temp.jsp page it outputs null. Is there any other setup I have to do or anything? I've been trying to get this to work forever, but the session variable keeps turning into null when I change pages.

Thanks.

Edit: I'm using the Eclipse IDE for Java EE developers, so I was wondering if I have to change some settings or something, because it seems to me that my code is right, yet it is not working...

Edit: I used session.getID() and discovered that the session is changing when I go from the Dashboard.jsp page to the temp.jsp page. How do I keep the same session?

Alan
  • 96
  • 2
  • 9
  • check this link : https://stackoverflow.com/questions/22369717/how-to-pass-a-value-from-one-jsp-to-another-jsp-page – burg.developer Nov 06 '19 at 20:25
  • I'm able to use a hidden variable, but since I'd like to save login information, I'd have to pass that information every single time the user goes from one page to another if I do it that way. The session object way is the one I'm trying and can't seem to get to work. – Alan Nov 08 '19 at 17:14
  • you can use [sessionStorage](https://www.w3schools.com/jsref/prop_win_sessionstorage.asp) javascript property but you should aware of security dangers – burg.developer Nov 08 '19 at 17:27
  • I just tried that. (It's missing a " , so I assume that the closing " should go after the last parenthesis after test). When I click on the link, it leads me to the temp.jsp page. The url includes the test=session.getAttribute('test') part, but other than that, nothing changes and it still shows null. Do you have any other ideas? Thanks for the help by the way. – Alan Nov 08 '19 at 20:02
  • I also tested out sessionStorage which works, but I am using the saved info to query a database, which I can only do through jsp. – Alan Nov 08 '19 at 20:04
  • I think you miss something I don't know what is it, anyway, You can create a function that communicates with the database which is return that data you want to invoke it in each JSP and call it by AJAX for example in every time that you load the JSP's. that function in the servlet – burg.developer Nov 08 '19 at 20:11
  • Oh, I finally discovered the problem. I used session.getId() and it looks like I am starting a new session every time the page changes. Any ideas on how to fix this? – Alan Nov 08 '19 at 20:23
  • But in your code its session.getAttribute ? – burg.developer Nov 08 '19 at 20:27
  • Yes. I added session.getId() just to see whether the session stays the same and it looks like it does not for some reason... – Alan Nov 08 '19 at 20:29
  • Could you please share your related Back end code (Servlet) function , if you can't check this [link](https://stackoverflow.com/questions/29968481/session-getattribute-is-returning-null) – burg.developer Nov 08 '19 at 20:33
  • also, check this [answer](https://stackoverflow.com/questions/6437705/jsp-session-getattribute-value-return-null) – burg.developer Nov 08 '19 at 20:35

0 Answers0