Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jsp---blank fields  RSS feed

 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I, Hi am working on this .html and .jsp files.




I am asking the user to enter two numbers in the .html and then the .jsp does a sum with the two numbers.

I have made a condition with: if and else,
so that when the textbox are empty the jsp would display a message, but all i get is errors.

this is the code in the jsp:


[ April 05, 2007: Message edited by: Jack Bolton ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Bolton:
..... but all i get is errors.



It would be a lot easier for people to help you if you posted the actual error messages.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you declaring the variables "a" and "b"?
What data type are they (Strings, ints)?

request.getParameter returns a string.
Integer.parseInt(string) returns an int.

The int type has no .equals method.
Even if it did, an int would never have a value of " ".

Be careful not to confuse "" with " ".
One is an empty string. the other is a single blank character.


Since null, "", and " " would never be valid values for an int, you'll need to test for these possibilities before trying to convert the parameters from Strings to ints.
[ April 05, 2007: Message edited by: Ben Souther ]
 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the variables a and b are declared in the jsp file, they are ints.

so you are suggesting me to do the "if", with the getParameter and equals and then i do the conversion to do the sum?
[ April 05, 2007: Message edited by: Jack Bolton ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Bolton:
the variables a and b are declared in the jsp file, they are ints.

so you are suggesting me to do the "if", with the getParameter and equals and then i do the conversion to do the sum?

[ April 05, 2007: Message edited by: Jack Bolton ]


You will want to validate the values before you try to convert them to ints.
Another approach is to wrap the call to parseInt in a try catch block and catch the NumberFormatException (NFE) that will be thrown if the parameter values can not be converted to ints. This will also cover you against someone typing "Jack" into one of the fields.



Another approach....
Starting with version 1.4, Java now has good support for regular expressions. This makes it pretty easy to write an isInt(String) method that doesn't require a try/catch block.
 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This the code, and i get this error when i enter the numbers and when i leave the field in blank:

C:\Blazix\jspdir\Suma_jsp.java:180: 'else' without 'if'
else
^
1 error
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, all this code should be in a Java class rather in a JSP. It'd be a lot easier to debug that way as well as following accepted practices.

But that said, why are you putting <% %> around each an every line rather than around the whole Java block? It's making your code even harder to read than it should be and is hiding the syntax errors from you.
[ April 05, 2007: Message edited by: Bear Bibeault ]
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jack,

I think you should remove the ';' in the first line, so it becomes:


Herman
NB:
-I don't understand why you check the parameter 'numero1' twice in one line
-jsp's are meant for View, not for logic, but if you really want to put all this Java code into your page, you don't have to put <% %> in every line, once is enough:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!