0
Good evening, everyone,
I am making a page in JSP, where the user informs 2 values and through a method, I do the sum of these 2 values and presents just below the value of the result.
However the page is not even loading, is showing error in line 17, where I am declaring the first variable to the value of "input" that the user informed.
I have tried to change the "input type" to "number" or "text", but it continues with the error. And if I change the "form" to submit to another page, it works normal (as long as I declare the variables on the other page).
Can someone help me?
Code of the JSP page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Exercício 6</title>
</head>
<body>
<h1>Informe 2 números</h1>
<form>
Valor 1: <input type="number" name="valor1" /><br><br>
Valor 2: <input type="number" name="valor2" /><br><br>
<input type="submit" />
</form>
<%
int valor1 = Integer.parseInt(request.getParameter("valor1"));
int valor2 = Integer.parseInt(request.getParameter("valor2"));
%>
<h2>Resultado do cálculo</h2>
<%!
public static int resultado(int x, int y){
return x + y;
}
%>
<div><%=resultado(valor1,valor2)%></div>
</body>
</html>
Error
org.apache.jasper.JasperException: An exception occurred processing JSP page /soma.jsp at line 17
14: <input type="submit" />
15: </form>
16: <%
17: int valor1 = Integer.parseInt(request.getParameter("valor1"));
18: int valor2 = Integer.parseInt(request.getParameter("valor2"));
19: %>
20: <h2>Resultado do cálculo</h2>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:579)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
org.apache.jsp.soma_jsp._jspService(soma_jsp.java:130)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)