Error in JSP page

Asked

Viewed 98 times

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)

1 answer

0

You are trying to get information from the request inside the scriplet, but the request object with the form values will only exist when you submit the form. That’s why null Pointer is happening, and why it works when you send it to another page.

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

If you need any more information, feel free to ask.

Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.