Issue
I created a Dynamic Web Project using Eclipse IDE 2019-12 and Apache Tomcat 8.5.55 server, where a user can give integers as input and will get their sum as output. The HTML file has a JavaScript function to check for empty, non-numeric and non-integer inputs before sending it to the Java EE servlet.
As the checking happens before sending request to the servlet, the servlet will throw errors if it receives anything other than an integer.
I used the Number.isInteger(x)
method to check if the input is an integer. However, when I run the HTML file in Eclispe, the Number
class in JavaScript doesn't seem to be working in the IDE. Any mention of the Number
class seems to be causing the IDE to ignore the if
blocks and sending the wrong data to the servlet. But checking using the Number class works fine in any browser, as it should.
Below is the HTML code
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Addition Page</title>
<style type="text/css">
div
{
position: absolute;
top: 10px;
left: 100px;
width: 250px;
height: 120px;
border: 2px solid green;
border-radius: 20px;
}
</style>
<script type="text/javascript">
function valueEntryValidation() // Should allow only integers. No empty, character or floating point inputs allowed
{
var int1 = document.AdditionForm.integer1.value;
var int2 = document.AdditionForm.integer2.value;
if (int1 == "" || int2 == "")
{
alert ("Fields cannot be empty");
return false;
}
// Only the if statement below works properly in Eclipse IDE
// if (isNaN (int1) || isNaN (int2) || int1.indexOf (".") > -1 || int2.indexOf (".") > -1)
// The if statement below works fine in browsers, but not in Eclipse IDE
if (isNaN (int1) || isNaN (int2) || !Number.isInteger (Number (int1)) || !Number.isInteger (Number (int2)))
{
alert ("Enter integers only");
return false;
}
}
</script>
</head>
<body>
<form name="AdditionForm" action="AdditionServlet" method="get" onsubmit="return valueEntryValidation()">
<div>
<br>
<table>
<tr><td>Integer 1</td><td> <input type="text" name="integer1"/></td></tr>
<tr><td>Integer 2</td><td> <input type="text" name="integer2"/></td></tr>
<tr><td></td><td colspan="2" align="Center"><input type="Submit" value="Add"/></td></tr>
</table>
</div>
</form>
</body>
</html>
How do I fix this problem with the IDE?
Solution
So the problem for me was probably incompatible versions of Eclipse IDE's components.
Eclipse lets you install softwares that are required for a type of project from a URL of their software's repository, it's the Web, XML, Java EE and OSGi Enterprise Development
in my case. I installed Eclipse a long time ago and installed the softwares for older version, which did not get updated when Eclipse updated to the latest version. I had to manually install the components for the version.
First, update Eclipse by going to Help->Check for Updates
.
Next, go to Help->Install New Software
and put the URL https://download.eclipse.org/releases/<Eclipse-version>/
in the Work with:
field.
Finally, install the softwares required and restart Eclipse.
This is what worked for me. Hope this helps you too.
Answered By - tukykarmakar