index.html
<html> <head> <title>AddModule | Home page</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 offset-md-3"> <form action="op.jsp"> <div class="card"> <div class="card-header bg-dark text-white"> <h3>Provide me a number</h3> </div> <div class="card-body"> <div class="form-group"> <input name="n1" type="number" class="form-control" placeholder="Enter n1"> </div> <div class=form-group> <input name="n2" type="number" class="form-control" placeholder="Enter n2"> </div> </div> <div class="card-footer text-center"> <button type="submit" class="btn btn-primary">Divide</button> </div> </div> </div> </form> </div> </div> </body> </html>
Getting an exception in op.jsp named java.lang.NumberFormatException
HTTP Status 500-Internal Server Error
op.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <html> <head> <title>Jsp Page</title> </head> <body> <% String n1= request.getParameter("n1"); String n2= request.getParameter("n2"); int a=Integer.parseInt(n1); int b=Integer.parseInt(n2); int c=a/b; %> <h3>Result is <%=c %></h3> </body> </html>
While converting value from string to integer it is generating an exception
Also Tried below code but still not working
<%int n1=Integer.parseInt(request.getParameter("n1")); int n2=Integer.parseInt(request.getParameter("n1")); int c=n1/n2 %>
Advertisement
Answer
The problem is not with your code, but rather how you run it.
You need to access index.html
first to allow you to input numbers, then hit Divide
. This will invoke op.jsp
with n1
and n2
as parameters.
If you try to access op.jsp
directly, your code will run without values for n1
and n2
. If you don’t have input, then trying to parse input as integers will obviously fail.
If you want to simplify testing, you can manually specify HTTP GET query parameters in the URL using op.jsp?n1=42&n2=7