How to request with integer number?

Asked

Viewed 253 times

0

I have the following form:

<form action="backend" method="get">
            <h1>Bem vindo ao Teacher Points</h1>
            <h3>Sistema web para calcular pontuação de Professores.</h3>
            <table>
            <tr>
                <td>Meses no cargo:</tr>
                <td><input type="text" name="mes_cargo"/></tr>
            </tr>
            <tr>
                <td>Meses na lotação:</tr>
                <td><input type="text" name="mes_lotacao"/></tr>
            </tr>
            <tr>
                <td>Meses na carreira adjunto:</tr>
                <td><input type="text" name="mes_adjunto"/></tr>
            </tr>
            <tr>
                <td>Meses na carreira titular:</tr>
                <td><input type="text" name="mes_titular"/></tr>
            </tr>
            <tr>
                <td>Magisterio: </tr>
                <td><input type="text" name="magisterio"/></tr>
            </tr>
            <td>
                <input type="submit" value="Calcular pontuação"/>
            </td>

            </table>

He is sending the values to my Servlet called "backend", the problem is when making the request, I made the following code:

int mes_cargo = Integer.parseInt(request.getParameter("mes_cargo"));
int mes_lotacao = Integer.parseInt(request.getParameter("mes_lotacao"));
int mes_adjunto = Integer.parseInt(request.getParameter("mes_adjunto"));
int mes_titular = Integer.parseInt(request.getParameter("mes_titular"));
int magisterio = Integer.parseInt(request.getParameter("mes_magisterio"));

but when I send it to Servlet it returns an HTTP Status 500 error with the following message

HTTP Status 500 - Internal Server Error
type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NumberFormatException: null
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.

GlassFish Server Open Source Edition 4.1.1

And I don’t know what to do, probably I’m not doing the right request and ta null in all imputs

  • Where is the error accused? Are you receiving the values in the backend? The code is in the same form address?

  • error is accused right after I click on html Submit "Calculate score"

  • I receive the values with the request I commented upstairs, but I don’t know if it’s correct

  • They are in the same project but in different folders, my Serrvlet is in "Teacher Points Web src java br with teacherpoints control" Already my html is in "Teacher Points Web"

3 answers

2

Hello @Arthur!

This error happens when you are trying to convert a non-numeric or null value to the whole type. Example:

Integer.parseInt(null);
Integer.parseInt("a");
Integer.parseInt("");
Integer.parseInt(" ");
Integer.parseInt("1a");

You need to check the values being sent to your Rvlet.

  • Okay, now it’s starting to make sense... but how do I do this check?

  • You can debug each request.getParameter() or print your output on the console to see what’s coming in the backend. For example: System.out.println(request.getParameter("mes_cargo")). Note: If I said something wrong please correct me, I only used java in college :)

  • But how do I make it whole?

  • ta reaching null on the console

  • You need to show your Servlet.

  • You want me to send my Ranger here?

Show 1 more comment

0

At the end my index was like this

<!DOCTYPE html>
<html>
    <head>
        <title>Teacher Points Web</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta author="Arthur dos Santos Almeida">
        <!--Linkedin: https://www.linkedin.com/in/arthursantosalmeida/-->
        <link rel="shortcut icon" href="ico/teacher.ico" type="image/x-icon"/>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <form action="backend" method="get">
            <h1>Bem vindo ao Teacher Points</h1>
            <h3>Sistema web para calcular pontuação de Professores.</h3>
            <table>
            <tr>
                <td>Meses no cargo:</tr>
                <td><input type="text" name="mes_cargo"/></tr>
            </tr>
            <tr>
                <td>Meses na lotação:</tr>
                <td><input type="text" name="mes_lotacao"/></tr>
            </tr>
            <tr>
                <td>Meses na carreira adjunto:</tr>
                <td><input type="text" name="mes_adjunto"/></tr>
            </tr>
            <tr>
                <td>Meses na carreira titular:</tr>
                <td><input type="text" name="mes_titular"/></tr>
            </tr>
            <tr>
                <td>Magisterio: </tr>
                <td><input type="text" name="magisterio"/></tr>
            </tr>
            <td>
                <input type="submit" value="Calcular pontuação"/>
            </td>

            </table>
        </form>
    </body>
</html>

0


Problem was that I was not closing the tag and had written wrong parameter. Thank you all.

  • 2

    if possible, complete the answer with the correct code, this can help other people in the community.

Browser other questions tagged

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