Convert String to Date (request.getParameter)

Asked

Viewed 1,259 times

0

I have a form in JSP that makes a register that has a date.

<form  method="Post" action="InserirCompromisso">
            titulo : <input type="text" name="titulo" required="true">
            local : <input type="text" name="local" required="true">
            data : <input type="text" name="data">
            <input type="submit" value="Cadastrar">

        </form>

And there’s the problem. How do I convert this String of Input using the request.getParameter like I did with the others Strings? The way I did in mine Servlet is not spinning. Someone could help me.

Follows my Servlet down below.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String Titulo = request.getParameter("titulo");
    String Local = request.getParameter("local");
    String dataEmTexto = request.getParameter("data");
    Calendar data = null;

    try {
        Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dataEmTexto);
        data = Calendar.getInstance();
        data.setTime(date);
    } catch (ParseException e) {
        out.println("Erro de conversão da data");
        return; //para a execução do método
    }

    Compromisso compromisso = new Compromisso();
    compromisso.setTitulo(Titulo);
    compromisso.setLocal(Local);
    compromisso.setData(data);


    CompromissoDAO dao = new CompromissoDAO();
    String retorno = dao.inserir(compromisso);
    if(retorno.equals("sucesso")){

        response.sendRedirect("index.jsp");

    }else{
        PrintWriter out = response.getWriter();
        out.print("<html>");
        out.print("<h2>Não foi possivel inserir</h2>");
        out.print("<br>");
        out.print("</html>");
    }

}
  • 1

    Tiago, welcome to [en.so]! You need to tell us what the mistake is. The code looks right.

  • Ola utluiz. Thank you for answering. You are making an error on this line: compromise.setData(data); The error is this: incompatible types : Calendar cannot be converted to Date.

1 answer

1


I ran it here and it worked. Pass the date instead of Calendar. Persistence needs to go inside Try-catch as well.

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String Titulo = request.getParameter("titulo");
    String Local = request.getParameter("local");
    String dataEmTexto = request.getParameter("data");


        try {
            Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dataEmTexto);
            Compromisso compromisso = new Compromisso();
            compromisso.setTitulo(Titulo);
            compromisso.setLocal(Local);
            compromisso.setData(date);
            CompromissoDAO dao = new CompromissoDAO();
            String retorno = dao.inserir(compromisso);
            if (retorno.equals("sucesso")) {

                response.sendRedirect("index.jsp");

            } else {
                PrintWriter out = response.getWriter();
                out.print("<html>");
                out.print("<h2>Não foi possivel inserir</h2>");
                out.print("<br>");
                out.print("</html>");
            }
        } catch (ParseException e) {
            out.println("Erro de conversão da data");
            return; //para a execução do método
        }   

}

Note: You don’t even need this Calendar. You’re not using in your model.

  • I’ll try tomorrow Mariana.Thank you very much for your help. :)

Browser other questions tagged

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