How to submit via java post?

Asked

Viewed 337 times

0

In a system all data is exposed via URL due to the sending method 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>");

    }
}
  • tried to put inside the <form> method="POST tag" ?

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>

Browser other questions tagged

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