Não está funcionando quando adiciono o contato
contato-adicionado.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Contato ${param.nome} adicionado com sucesso
</body>
</html>
AdicionaContatoServlet
public class AdicionaContatoServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// busca o writer
PrintWriter out = response.getWriter();
// buscando os parâmetros no request
String nome = request.getParameter("nome");
String endereco = request.getParameter("endereco");
String email = request.getParameter("email");
String dataEmTexto = request
.getParameter("dataNascimento");
Calendar dataNascimento = null;
// fazendo a conversão da data
try {
Date date =
new SimpleDateFormat("dd/MM/yyyy")
.parse(dataEmTexto);
dataNascimento = Calendar.getInstance();
dataNascimento.setTime(date);
} catch (ParseException e) {
out.println("Erro de conversão da data");
return; //para a execução do método
}
// monta um objeto contato
Contato contato = new Contato();
contato.setNome(nome);
contato.setEndereco(endereco);
contato.setEmail(email);
contato.setDataNascimento(dataNascimento);
// salva o contato
ContatoDao dao = null;
try {
dao = new ContatoDao();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dao.adiciona(contato);
RequestDispatcher rd = request.getRequestDispatcher("/contato-adicionado.jsp");
rd.forward(request, response);
}
}
ContatoDao
public class ContatoDao {
private Connection connection;
public ContatoDao() throws ClassNotFoundException {
this.connection = new ConnectionFactory().getConnection();
}
public void adiciona(Contato contato) {
String sql = "insert into contatos" + "(nome,email,endereco,dataNascimento)" + "values(?,?,?,?)";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, contato.getNome());
stmt.setString(2, contato.getEmail());
stmt.setString(3, contato.getEndereco());
stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
stmt.execute();
stmt.close();
} catch(SQLException e){
throw new RuntimeException(e);
}
}
public List<Contato> getLista() {
try {
List<Contato> contatos = new ArrayList<Contato>();
PreparedStatement stmt = this.connection.prepareStatement("select * from contatos");
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
Contato contato = new Contato();
contato.setId(rs.getLong("id"));
contato.setNome(rs.getString("nome"));
contato.setEmail(rs.getString("email"));
contato.setEndereco(rs.getString("endereco"));
Calendar data = Calendar.getInstance();
data.setTime(rs.getDate("dataNascimento"));
contato.setDataNascimento(data);
contatos.add(contato);
}
rs.close();
stmt.close();
return contatos;
} catch(SQLException e) {
throw new DAOException(e);
}
}
public Contato pesquisar(int id) {
try {
PreparedStatement stmt = this.connection.prepareStatement("select * from contatos where id=" + id);
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
Contato contato = new Contato();
contato.setId(rs.getLong("id"));
contato.setNome(rs.getString("nome"));
contato.setEmail(rs.getString("email"));
contato.setEndereco(rs.getString("endereco"));
Calendar data = Calendar.getInstance();
data.setTime(rs.getDate("dataNascimento"));
contato.setDataNascimento(data);
return contato;
}
rs.close();
stmt.close();
} catch(SQLException e) {
throw new DAOException(e);
}
return null;
}
public void altera(Contato contato) {
String sql = "update contatos set nome=?, email=?, endereco=?, dataNascimento=? where id=?";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, contato.getNome());
stmt.setString(2, contato.getEmail());
stmt.setString(3, contato.getEndereco());
stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
stmt.setLong(5, contato.getId());
stmt.execute();
stmt.close();
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
public void remove(Contato contato){
try {
PreparedStatement stmt = connection.prepareStatement("delete from contatos where id=?");
stmt.setLong(1, contato.getId());
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
jul 12, 2016 7:39:05 PM org.apache.tomcat.util.digester.SetPropertiesRule begin ADVERTÊNCIA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:fj21-agenda’ did not find a matching property. jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Server version: Apache Tomcat/7.0.70 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Server built: Jun 15 2016 16:27:45 UTC jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Server number: 7.0.70.0 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: OS Name: Windows 10 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: OS Version: 10.0 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Architecture: amd64 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Java Home: C:\Program Files\Java\jre1.8.0_91 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: JVM Version: 1.8.0_91-b15 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: JVM Vendor: Oracle Corporation jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: CATALINA_BASE: C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: CATALINA_HOME: C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Command line argument: -Dcatalina.base=C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Command line argument: -Dcatalina.home=C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70 jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Command line argument: -Dwtp.deploy=C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\wtpwebapps jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Command line argument: -Djava.endorsed.dirs=C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\endorsed jul 12, 2016 7:39:05 PM org.apache.catalina.startup.VersionLoggerListener log INFORMAÇÕES: Command line argument: -Dfile.encoding=Cp1252 jul 12, 2016 7:39:05 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFORMAÇÕES: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_91\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_91/bin/server;C:/Program Files/Java/jre1.8.0_91/bin;C:/Program Files/Java/jre1.8.0_91/lib/amd64;C:\Program Files (x86)\Borland\Delphi7\Bin;C:\Program Files (x86)\Borland\Delphi7\Projects\Bpl;C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Skype\Phone;C:\Program Files\Java\jdk1.8.0_60\bin;C:\Program Files\Java\jre1.8.0_77\bin;C:\Users\Junior\Documents\Curso de Java - RL System\eclipse-jee-mars-2-win32-x86_64\eclipse;;. jul 12, 2016 7:39:05 PM org.apache.coyote.AbstractProtocol init INFORMAÇÕES: Initializing ProtocolHandler [“http-bio-8080”] jul 12, 2016 7:39:05 PM org.apache.coyote.AbstractProtocol init INFORMAÇÕES: Initializing ProtocolHandler [“ajp-bio-8009”] jul 12, 2016 7:39:05 PM org.apache.catalina.startup.Catalina load INFORMAÇÕES: Initialization processed in 2831 ms jul 12, 2016 7:39:05 PM org.apache.catalina.core.StandardService startInternal INFORMAÇÕES: Starting service Catalina jul 12, 2016 7:39:05 PM org.apache.catalina.core.StandardEngine startInternal INFORMAÇÕES: Starting Servlet Engine: Apache Tomcat/7.0.70 jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/core_rt">http://java.sun.com/jstl/core_rt</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/core">http://java.sun.com/jstl/core</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/fmt_rt">http://java.sun.com/jstl/fmt_rt</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/fmt">http://java.sun.com/jstl/fmt</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://jakarta.apache.org/taglibs/standard/permittedTaglibs">http://jakarta.apache.org/taglibs/standard/permittedTaglibs</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://jakarta.apache.org/taglibs/standard/scriptfree">http://jakarta.apache.org/taglibs/standard/scriptfree</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/sql_rt">http://java.sun.com/jstl/sql_rt</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/sql">http://java.sun.com/jstl/sql</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/xml_rt">http://java.sun.com/jstl/xml_rt</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/xml">http://java.sun.com/jstl/xml</a> is already defined jul 12, 2016 7:39:08 PM org.apache.catalina.startup.TldConfig execute INFORMAÇÕES: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 12, 2016 7:39:09 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom INFORMAÇÕES: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [243] milliseconds. jul 12, 2016 7:39:09 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deploying web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\docs jul 12, 2016 7:39:09 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deployment of web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\docs has finished in 204 ms jul 12, 2016 7:39:09 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deploying web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\examples jul 12, 2016 7:39:10 PM org.apache.catalina.core.ApplicationContext log INFORMAÇÕES: ContextListener: contextInitialized() jul 12, 2016 7:39:10 PM org.apache.catalina.core.ApplicationContext log INFORMAÇÕES: SessionListener: contextInitialized() jul 12, 2016 7:39:11 PM org.apache.catalina.core.ApplicationContext log INFORMAÇÕES: ContextListener: attributeAdded(‘org.apache.jasper.compiler.TldLocationsCache’, ‘org.apache.jasper.compiler.TldLocationsCache@2d73e75’) jul 12, 2016 7:39:11 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deployment of web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\examples has finished in 1,432 ms jul 12, 2016 7:39:11 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deploying web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\host-manager jul 12, 2016 7:39:11 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deployment of web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\host-manager has finished in 209 ms jul 12, 2016 7:39:11 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deploying web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\manager jul 12, 2016 7:39:11 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deployment of web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\manager has finished in 253 ms jul 12, 2016 7:39:11 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deploying web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\ROOT jul 12, 2016 7:39:11 PM org.apache.catalina.startup.HostConfig deployDirectory INFORMAÇÕES: Deployment of web application directory C:\Users\Junior\Documents\Caelum\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\ROOT has finished in 124 ms jul 12, 2016 7:39:11 PM org.apache.coyote.AbstractProtocol start INFORMAÇÕES: Starting ProtocolHandler [“http-bio-8080”] jul 12, 2016 7:39:11 PM org.apache.coyote.AbstractProtocol start INFORMAÇÕES: Starting ProtocolHandler [“ajp-bio-8009”] jul 12, 2016 7:39:11 PM org.apache.catalina.startup.Catalina start INFORMAÇÕES: Server startup in 5940 ms jul 12, 2016 7:39:21 PM org.apache.catalina.core.StandardContext reload INFORMAÇÕES: Reloading Context with name [/fj21-agenda] has started jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/core_rt">http://java.sun.com/jstl/core_rt</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/core">http://java.sun.com/jstl/core</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/fmt_rt">http://java.sun.com/jstl/fmt_rt</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/fmt">http://java.sun.com/jstl/fmt</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://jakarta.apache.org/taglibs/standard/permittedTaglibs">http://jakarta.apache.org/taglibs/standard/permittedTaglibs</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://jakarta.apache.org/taglibs/standard/scriptfree">http://jakarta.apache.org/taglibs/standard/scriptfree</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/sql_rt">http://java.sun.com/jstl/sql_rt</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/sql">http://java.sun.com/jstl/sql</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/xml_rt">http://java.sun.com/jstl/xml_rt</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TaglibUriRule body INFORMAÇÕES: TLD skipped. URI: <a href="http://java.sun.com/jstl/xml">http://java.sun.com/jstl/xml</a> is already defined jul 12, 2016 7:39:24 PM org.apache.catalina.startup.TldConfig execute INFORMAÇÕES: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 12, 2016 7:39:24 PM org.apache.catalina.core.StandardContext reload INFORMAÇÕES: Reloading Context with name [/fj21-agenda] is completed jul 12, 2016 7:45:54 PM org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [br.com.caelum.agenda.servlet.AdicionaContatoServlet] in context with path [/fj21-agenda] threw exception java.lang.NullPointerException at java.text.SimpleDateFormat.parse(Unknown Source) at java.text.DateFormat.parse(Unknown Source) at br.com.caelum.agenda.servlet.AdicionaContatoServlet.service(AdicionaContatoServlet.java:40) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)