Convert String to Date (request.getParameter)

0

I have a form in JSP that performs a registration 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 here's the problem. How do I convert this String of Input using request.getParameter as I did with other Strings ? The way I did it on my Servlet is not running. Could anyone help me.

Follow my Servlet 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>");
    }

}
    
asked by anonymous 06.03.2016 / 03:56

1 answer

1

I performed here and it worked. Pass the date instead of the Calendar. Persistence needs to go inside the 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 do not even need this Calendar. You're not even using it in your model.

    
07.03.2016 / 00:31