How to send via post in java?

0

In a system all the data is exposed via url due to the method of sending: GET I need to send via POST

code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="adicionarContato" enctype="multipart/form-data">
        <br><label>Nome:</label>
        <input type="text" name="nome">

        <br><label>email</label>    
        <input type="text" name="email">

        <br><label>Endereco</label>
        <input type="text" name="endereco">

        <br><label>Data Nascimento</label>
        <input type="text" name="dataNascimento">

        <br><input type="submit" value="gravar">

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

servlet

package com.dev;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class adicionarContato
 */
@WebServlet("/adicionarContato")
public class adicionarContato extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        PrintWriter out = response.getWriter();


        //pegando os valores
        String nome            = request.getParameter("nome");
        String endereco        = request.getParameter("endereco");
        String email           = request.getParameter("email");
        String dataNascimentoT = request.getParameter("dataNascimento");

        Date dataNascimento = null;


        //montagem do objeto
        Contato contato = new Contato();
        contato.setNome(nome);
        contato.setEndereço(endereco);
        contato.setEmail(email);


        //conversao da data
        try {
            dataNascimento = new SimpleDateFormat("dd/mm/YYYY").parse(dataNascimentoT);
        } catch (Exception e) {
            // TODO: handle exception
            out.println("Erro de conversao");
            return;
        }

        contato.setDataNascimento(dataNascimento);

        //imprimir os dados
        out.println("<html>");
        out.println("<body>");

        out.println("Nome: "+contato.getNome()
          +"<br> Endereco: "+contato.getEndereço()
          +"<br> Email:"    +contato.getEmail()
          +"<br> Data:"     +contato.getDataNascimento());

        out.println("</body>");
        out.println("</html>");

    }
}
    
asked by anonymous 23.08.2018 / 01:37

1 answer

1

Change your form tag to this:

<form action="adicionarContato" enctype="multipart/form-data" method="POST">
    <br><label>Nome:</label>
    <input type="text" name="nome">

    <br><label>email</label>    
    <input type="text" name="email">

    <br><label>Endereco</label>
    <input type="text" name="endereco">

    <br><label>Data Nascimento</label>
    <input type="text" name="dataNascimento">

    <br><input type="submit" value="gravar">

</form>
    
23.08.2018 / 04:26