tags:

views:

806

answers:

3

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. what's UP?????

+1  A: 

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

String.valueOf(request.getSession().getAttribute("test"));
ironsam
A: 

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")
skaffman
Yeah, i used this before, but it also return the error data. and when i try to use request.getAttribute("test").toString(), it throw out a exception
MemoryLeak
+1  A: 

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>
ironsam