• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Passing values from javascript to jsp scriplets throws numberformatexception error

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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: 4576
47
  • Mark post as helpful
  • send pies
  • 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
  • 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: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 69411
276
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21949
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
I don't even know how to spell CIA. But this tiny ad does:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic