File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Getting List class from session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting List class from session" Watch "Getting List class from session" New topic
Author

Getting List class from session

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

I create a List<MyClass>, do whatever I need to do and then insert it into a session:



My question is how do I retrieve it ? I have tried the following and it doesnt work:





.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

What's "doesn't work" mean?

And yes, you should be able to retrieve the scoped variable in that way.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Sorry, should have been more specific. I do the following to get my Lis<class> session:



The first problem here is the complier gives me a note saying "java uses unchecked or unsafe operations" on the get session bit.

When I compile using javac class.java -Xlint it gives me the following error:



The second problem is the for each loop doesn't work. Everything compiles but I keep getting a 500 interal error.

[ September 07, 2008: Message edited by: shaf maff ]

[ September 07, 2008: Message edited by: shaf maff ]

[ September 07, 2008: Message edited by: shaf maff ]
[ September 07, 2008: Message edited by: shaf maff ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

The compilation warnings are irrelevant. What are the details of the 500 error?
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Here is the 500 error:

java.lang.NullPointerException
Get.doGet(Get.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

This is line 140:

for(MailSession mail : test) {

BTW, what does the note/warning mean and why ignore it ?



.
li bei
Greenhorn

Joined: Jul 22, 2007
Posts: 20
HttpSession getMailSession = request.getSession(false);
List<MailSession> test = ( List<MailSession> ) checkSession.getAttribute("MailSession");


You typed it , or just copy and paste?

You named the reference "getMailSession", but use another reference "checkSession".


I am sorry that my English is poor.If there is something wrong with my words,please tell me .Thank you very much.
li bei
Greenhorn

Joined: Jul 22, 2007
Posts: 20
for(MailSession mail : test) {
StringBuilder xml = new StringBuilder();
xml.append(mail.getLetter());
}

And this, maybe there is something wrong.

you defined the StringBuilder "xml" in the loop.

it should be outside, I guess.
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Originally posted by li bei:
HttpSession getMailSession = request.getSession(false);
List<MailSession> test = ( List<MailSession> ) checkSession.getAttribute("MailSession");


You typed it , or just copy and paste?

You named the reference "getMailSession", but use another reference "checkSession".
I typed it. Ignore the small errors. The main message was to outline the struture of the code/how Im doing things..
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Originally posted by :
[QB][/QB]
Sorry it was a typo when typing up a more summerised version. Im sure you wouldnt want the full 600-700 line code.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Originally posted by shaf maff:
I typed it. Ignore the small errors. The main message was to outline the struture of the code/how Im doing things..

Please don't waste people's time with sloppy posting. The "small errors" as you call them can be the source of your errors or not. People trying to help you don't know what's real and what's just a result of your unwillingness to put any effort into your questions.

People who volunteer their time to help others will quickly grow tired of such sloppiness and just go on to help others who are putting more effort into their posts.
Sam Aran
Greenhorn

Joined: Sep 06, 2008
Posts: 12
Originally posted by shaf maff:
Here is the 500 error:

java.lang.NullPointerException
Get.doGet(Get.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

This is line 140:

for(MailSession mail : test) {

BTW, what does the note/warning mean and why ignore it ?



.

What this suggests to me is that for some reason the session is not returning a List (that is, the attribute returned is null, because mail being null would not throw an NPE) Test is probably null. This may be because it is a different Session object somehow. Your best bet is to store some kind of identifier value into the session (as a string) and then look for this value at this line with a debugger. If that value is not a Session attribute, then you do indeed have a different session for the current request and as such may not have the information you require.

As for the warnings, the reason you *have* to ignore this is that the Servlet API has been here for awhile, especially before the Java 5 change. Therefore, it is backwards compatible (for the most part) with previous versions. getAttribute returns Object, which is what it has to return, naturally (you can store anything) but the compiler now warns you that you're doing a potentially unsafe cast (casting it to your List<special> type) because the only safe cast in that situation is a method defined as returning (directly) a List<special> instance.

The other one is a SerializableUID warning, which Eclipse can solve instantly for you (add default serializable id) which creates a private static final long serialVersionUID = 1L; This is because your class implements the Serializable interface, which recommends that a class provide an identifier for its version when being serialized (otherwise, this will be generated by the JVM).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting List class from session