• Post Reply Bookmark Topic Watch Topic
  • New Topic

Session question  RSS feed

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I set two object in my session like this in a servlet:
getServletContext().setAttribute("nomination_qtr", new Integer(term[0]));
getServletContext().setAttribute("nomination_yr", new Integer(term[1]));
Now I want to access them both in a jsp page.
I can make them an int now. I thought I could do:
int qtr = Integer.parseInt((String)session.getAttribute("nomination_qtr"));
int y = Integer.parseInt((String)session.getAttribute("nomination_yr"));

What can I do?
By the way the servlet is automatically loaded at startup.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Actually, just to be safe, I'd also make sure that the values were actually there and what I thought they were so nasty NullPointerExceptions and ClassCastExceptions don't get thrown...

 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anthony Smith:
I set two object in my session like this in a servlet:
getServletContext().setAttribute("nomination_qtr", new Integer(term[0]));
getServletContext().setAttribute("nomination_yr", new Integer(term[1]));
Now I want to access them both in a jsp page.
I can make them an int now. I thought I could do:
int qtr = Integer.parseInt((String)session.getAttribute("nomination_qtr"));
int y = Integer.parseInt((String)session.getAttribute("nomination_yr"));

What can I do?
By the way the servlet is automatically loaded at startup.

Just to add, your code above will definitely throw a ClassCastException since you are setting Integer objects as attributes while attempting to cast/retrieve them as Strings.
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I am setting them as Intger Objects, but what can I not acces them as such?
int qtr = session.getAttribute("nomination_qtr").intValue();

How come I cannot do that since it is n Integer Object?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to cast the attribute to Integer first before calling intValue()
Try this:
Integer num = (Integer) session.getAttribute("nomination_qtr");
int qtr = num.intValue();
or
int qtr = ((Integer) session.getAttribute("nomination_qtr")).intValue();
Also, checking if the attribute is null or if term[i] will not throw a NumberFormatException is also recommended.
 
sampy jagan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you are putting the Attribute in the ServletContext and are trying to fetch it from the Session.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use application.getAttribute() and not session.getAttribute() in JSP since u have stored the attribute in servletContext.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!