Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setAttribute -> getAttribute  RSS feed

 
vivek parepalli
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello People,

I am trying to send a int vale as a Paramter from servlet1 to servlet2. Servlet1 looks similiar to following code.

int a;
<req object>.setAttribute("key",a);

and now Servlet2 looks like this.

int b =Integer.parseInt((String)<req object>.getAttribute("key"));

Well the problem is I am getting a ClassCastException at the above line. What could be the problem and How do I solve it? Well I tried out by converting the (int a) into a String value and then sending it as a valu;e in the setAttribute.

Waiting for solution.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of things

- you can only assign objects as attributes.
- I presume you have Java 1.5 with auto-boxing.

Because attributes can only be objects, request.setAttribute("key", a); would automagically turn the "int" into an "Integer" object.

So I think this would work:
int b = ((Integer)request.getAttribute("key")).intValue();

If that still doesn't work, I would recommend this:

Object obj = request.getAttribute("key");
System.out.println("The key in session is of type " + obj.getClass());
System.out.println("Value = " + obj);
 
vivek parepalli
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Evans:

Object obj = request.getAttribute("key");
System.out.println("The key in session is of type " + obj.getClass());
System.out.println("Value = " + obj);


Hey it worked.. thanks a lot ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!