MemoryLeak
MemoryLeak

Reputation: 7318

What's wrong about request.getSession().setAttribute()

I use to pass data between *.java and *.jsp, since this is a MVC framework, it will go by the *.java first. so i used request.getSession().setAttribute("test", "01010101010") to save the value, and then in *.jsp, use request.getSession().getAttribute("test") to get value. But it returns a strange string "682342348" all the time.

Upvotes: 1

Views: 20489

Answers (4)

ACV
ACV

Reputation: 10560

request.getSession() - this will create a new session if it doesn't exist. You need to use request.getSession(false) if you want to make sure that if the session is not there, it won't be created.

Upvotes: 0

ironsam
ironsam

Reputation: 630

This might be a javascript question now, try adding quotes around the value of the alert parameter.

Change this:

<script>alert(<%=request.getAttribute("test")%>);</script>

To this:

<script>alert('<%=request.getAttribute("test")%>');</script>

Upvotes: 2

skaffman
skaffman

Reputation: 403591

It's possible that your java class and your JSP are getting different session objects somehow. You could try comparing the value you get back from session.getId() to make sure they're the same.

However, if all you're trying to do is pass objects from java class to JSP, then you may not need to use the session at all. Instead, store the data as a request attribute:

request.setAttribute("test", "01010101010")

Upvotes: 0

ironsam
ironsam

Reputation: 630

Try casting the value to a string when you get it out of session:

String.valueOf(request.getSession().getAttribute("test"));

Upvotes: 1

Related Questions