3
respostas

O formato de data não pode ser nulo?

Ao preencher os campos do formulário no navegador e deixar o campo de data em branco, ele acusa o seguinte erro: javax.servlet.ServletException: java.text.ParseException: Unparseable date: ""

O que pode estar havendo? Ele não deveria aceitar valores nulos também?

public class NovaEmpresa {

   public void executa(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    System.out.println("Cadastrando nova empresa");

    String nomeEmpresa = request.getParameter("nome");
    String paramDataEmpresa = request.getParameter("data");

    Date dataAbertura = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        dataAbertura = sdf.parse(paramDataEmpresa);
    } catch (ParseException e) {
        throw new ServletException(e);
    }

    Empresa empresa = new Empresa();
    empresa.setNome(nomeEmpresa);
    empresa.setDataAbertura(dataAbertura);

    Banco banco = new Banco();
    banco.adiciona(empresa);

    request.setAttribute("empresa", empresa.getNome());

    response.sendRedirect("entrada?acao=ListaEmpresas");
}

}

3 respostas

Como está seu arquivo formNovaEmpresa.jsp e listaEmpresas.jsp?

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:url value="/entrada" var="linkEntradaServlet"/>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <form action="${linkEntradaServlet }" method="post">

        Nome: <input type="text" name="nome"  />
        Data Abertura: <input type="text" name="data"  />
        <input type="hidden" name="acao" value="NovaEmpresa">
        <input type="submit" />
    </form>

</body>
</html>

formNovaEmpresa.jsp

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<c:url value="/entrada" var="linkEntradaServlet"/>


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

     <form action="${linkEntradaServlet}" method="post">

        Nome: <input type="text" name = "nome"/>
        Data Abertura: <input type="text" name = "data"/>
        <input type = "submit"/> <!-- botão enviar -->
        <input type = "hidden" name = "acao" value = "NovaEmpresa"/> 
    </form>
</body>
</html>

listaEmpresas.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.List,br.com.alura.gerenciador.modelo.Empresa"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Java Standard Taglib</title>
</head>
<body>

    <c:if test = "${not empty empresa}"> 

        Empresa ${empresa} cadastrada com sucesso!

    </c:if>
    <ul>    
        <c:forEach items= "${empresas}" var = "empresa"> 

            <li>
            ${empresa.nome} - <fmt:formatDate value="${empresa.dataAbertura }" pattern = "dd/MM/yyyy"/> 
            <a href ="/gerenciador/entrada?acao=RemoveEmpresa&id=${empresa.id } ">remove</a>
            <a href ="/gerenciador/entrada?acao=MostraEmpresa&id=${empresa.id } ">edita</a>
            </li>
        </c:forEach>

    </ul>

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software