This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Error in code

 
gokulnaath venkatesan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the inputs "distance" and "service " from a previous html page and passing the values here to calculate "estimateamt" and pass the calculated "estimateamt" to a jsp page "service2.jsp" using session attribute



@@@@@@@@@@@@ this is the end of the servlet .Now the service.jsp is given below


Now when i run the servlet i am getting the Estimated amount as "0" always.which is the initial amount assigned..what is the mistake i did

 
kopparapu v kiran kumar
Ranch Hand
Posts: 32
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things here:

1. First while saving the amount into the session you have to convert the amount into the string.
For example


This means what ever data type you are placing into the session the same data type needs to be used while retreiving from the session object.
Error occured since you placed the estimate amount as Long value into the session and you are trying to retrieve in the jsp page as string.

2. In the Jsp page you don't have to get the session object from the HttpRequest object as shown below:


Instead you can directly refer the HttpSession object in the jsp using the implict object

 
olivier dutranoit
Ranch Hand
Posts: 81
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is see in your jsp is the following :


String dist=(String)sess.getAttribute("distance");
String transport=(String)sess.getAttribute("service");
String amt=(String)sess.getAttribute("estimateamt");


First of all, your primitives "long" are put into session, trough autoboxing.

Further, you want to get them out. (code above)
You need to cast them back to long.
You cannot cast a primitive long to String
use String.valueOf(value) instead to get your string.


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kopparapu v kiran kumar wrote:1. First while saving the amount into the session you have to convert the amount into the string.
For example


Not so. Any type of object can be stored in the session. There is no need for conversion to string.
 
gokulnaath venkatesan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kopparapu v kiran kumar wrote:Two things here:

1. First while saving the amount into the session you have to convert the amount into the string.
For example


This means what ever data type you are placing into the session the same data type needs to be used while retreiving from the session object.
Error occured since you placed the estimate amount as Long value into the session and you are trying to retrieve in the jsp page as string.

2. In the Jsp page you don't have to get the session object from the HttpRequest object as shown below:


Instead you can directly refer the HttpSession object in the jsp using the implict object




thanks ..problem solved with your first solution by adding String.valueOf();

Now the problem is every time i am getting the amount as 0.it is not displaying the calculated amount.it is always displaying the initial assigned amount 0...can you help it out?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is a numeric value why are you casting it to a string in the first place?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Hg,
Your post was moved to a new topic.
Please start new questions in new topics.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic