HTML + JSP form

Asked

Viewed 230 times

0

personal created an html page with a simple form and I’m wanting to use the links in jsp

1) java class "Calculus":

package principal;
public class Calculo {
    public int n1;
    public int n2;
    public int resultado;
}

2) html form

<form method="POST">
    soma<input type="radio" name="soma"> | subtracao <input type="radio" name="sub"><br>
    valor1:<input type="text" name="valor01"/><br>
    valor2:<input type="text" name="valor02"/><br>
    <input type="submit" value="calcular"/><br>
</form>

3) jsp (within html)

<%
    Calculo c1 = new Calculo();
    int valor1 = Integer.parseInt(request.getParameter("valor01"));
    int valor2 = Integer.parseInt(request.getParameter("valor02"));

    c1.n1 = valor1;
    c1.n2 = valor2;

    c1.resultado = valor1+valor2;
    out.print(c1.resultado);
%>

I am using Tomcat 7, the error is as follows on line 21:

org.apache.jasper.JasperException: An exception occurred processing JSP page /teste.jsp at line 21

Line 21:

int valor1 = Integer.parseInt(request.getParameter("valor01"));

I’ve tried everything, and I still can’t, can anyone help me? vlw

  • it would be interesting to put all the error stacktrace

1 answer

0

Is JSP in the same file as this right form? The first time, you don’t have any parameters passed, then it tries to convert null to integer, and causes the exception. You need to check if you have the values in the request before using them. Try this:

<%
if (request.getParameter("valor01") == null || request.getParameter("valor02") == null) {
  out.print("<p>Informe os valores</p>");
} else {
  Calculo c1 = new Calculo();
  int valor1 = Integer.parseInt(request.getParameter("valor01"));
  int valor2 = Integer.parseInt(request.getParameter("valor02"));

  c1.n1 = valor1;
  c1.n2 = valor2;

  c1.resultado = valor1+valor2;
  out.print(c1.resultado);
}
%>

Browser other questions tagged

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