Error in JSP page

0

Good evening guys,

I am making a page in JSP, where the user informs 2 values and through a method, I make the sum of these 2 values and it shows below the value of the result.

But the page is not loading, it is displaying an error on line 17, where I am declaring the first variable to the input "ddo" value that the user reported.

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 anyone help me?

JSP Page Code

<%@ 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)
    
asked by anonymous 16.10.2018 / 02:40

1 answer

0

You are trying to get request information inside the script, but the request object with the form values will only exist when you submit the form. This is why the null pointer is occurring, so it works when you send it to another page.

The correct one is to send to another page, following the normal JSP flow. To stay all in one page, you would have to do the scripts with Javascript.

If you need more information feel free to ask.

Good luck!

    
16.10.2018 / 03:43