aspose file tools*
The moose likes Servlets and the fly likes Getting Error in code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting Error in code" Watch "Getting Error in code" New topic
Author

Getting Error in code

gokulnaath venkatesan
Greenhorn

Joined: Oct 18, 2011
Posts: 3
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

Joined: Oct 16, 2008
Posts: 32

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

Joined: Aug 20, 2011
Posts: 81

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

Joined: Jan 10, 2002
Posts: 61433
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
gokulnaath venkatesan
Greenhorn

Joined: Oct 18, 2011
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

If it is a numeric value why are you casting it to a string in the first place?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Ashish Hg,
Your post was moved to a new topic.
Please start new questions in new topics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Error in code