• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing values from javascript to jsp scriplets throws numberformatexception error

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i was trying to pass a javascript variable value to jsp by using request.getParameter() , but it throws numberformatexception when i try to convert it to integer

I donot understand the cause of the error in this simple program




The error log is down here:



 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you first go to that JSP page what will the value of "name" be?

That's the answer to your question.

A key thing, though, i that that Java code should not be in a JSP page.
Indeed the JSP page should not be accessible directly from the browser.  You should be going via a servlet.
 
Gourav Das
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:When you first go to that JSP page what will the value of "name" be?

That's the answer to your question.

A key thing, though, i that that Java code should not be in a JSP page.
Indeed the JSP page should not be accessible directly from the browser.  You should be going via a servlet.



I agree java code should not be in jsp page , but my question was why when converting to integer  it threw NumberFromatException.

"name" must store "2" as a string when clicked on the button called "Get".
But when i tried converting the string "2" into integer value it threw error , i dont understand. :(
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gourav Das wrote:"name" must store "2" as a string when clicked on the button called "Get".



And yet the error information that you posted clearly shows that it's null when it gets to the server. So it looks like your line of thought which expected it to be "2" was incorrect.

Your next step should be to figure out why that form submit doesn't send "2" to the server. It must be a JavaScript thing.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Gourav Das
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i was trying to pass a javascript variable value to jsp by using request.getParameter() , but it throws numberformatexception when i try to convert it to integer

I donot understand the cause of the error in this simple program




The error log is down here:



 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gourav Das wrote:


The NumberFormatException tells you what the input was. In this case it was null, which simply means that the "name" parameter was not sent correctly. This probably occurs because the first time you go to this URL, you don't pass the "name" parameter yet.
 
Gourav Das
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No sir , i have passed the name parameter via the url , check the code , if i remove the Integer.parseInt() part the code runs successfully on my tomcat server. So its for sure that name is ="2" , but i dont understand whats the problem converting it to integer
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gourav Das wrote:No sir , i have passed the name parameter via the url , check the code



Yes, you pass a "name" parameter. What you pass as the value of that parameter is far less obvious.

if i remove the Integer.parseInt() part the code runs successfully on my tomcat server.



Well, sure, if you remove the line of code which throws the exception, then that exception doesn't get thrown. You can count that as "runs successfully" if you like.

So its for sure that name is ="2" , but i dont understand whats the problem converting it to integer



No, it doesn't follow that it's "2". You have some JavaScript which may or may not send "2" as the value of the "name" parameter. I don't see anywhere in your JSP code where you verify that it's actually "2". All you did was to remove the code which demonstrates that it isn't.

Let me give you some advice about problem-solving: Declaring that a certain part of the code can't possibly be the cause of the problem is futile unless you demonstrate in a positive way that it isn't the cause of the problem. You haven't done that.
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic