roncato
Outubro 31, 2017, 12:33pm
#1
Pessoal, bom dia.
Criei um JSP chamado aluno-cadastrar.jsp, o problema está no com erro de elemento não encontrado.
Como faço para corrigir este erro?
> <%@ 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>
> <form action="cadastrarAluno">
> <table>
> <tr>
> <td>Nome:</td> <td><input type="text" name="nome" /><br /></td>
>
> <tr>
> <td>Registro:</td> <td><input type="text" name="registro" /> <br /></td>
> </tr>
> <tr>
> <td>E-Mail:</td> <td><input type="text" name="email" /><br /></td>
> </tr>
> <tr>
> <td>Endereço:</td> <td><input type="text" name="endereco" /><br /></td>
> </tr>
> <tr>
> <td>Data Inscriçao:</td> <td><input type="text" name="dataInscricao" /><br /></td>
> </tr>
> </table>
> <input type="submit" value="Gravar" />
> </form>
> </body>
> </html>
Acho que falta a extensão e o método,tente usar:
<form action="cadastrarAluno.php" method="post">
trocando php pela extensão do seu arquivo (usei php como exemplo)
roncato
Outubro 31, 2017, 12:52pm
#3
Samuel,
A logica para adicionar o aluno, ainda está no “service”, por isso ainda não havia colocado o method=“post”… de qualquer forma coloquei, mas ainda não funcionou…
roncato
Outubro 31, 2017, 2:22pm
#5
sim, coloquei, mas tb não deu certo
roncato
Novembro 7, 2017, 11:38am
#6
Pessoal, alguém pode me ajudar com o erro acima?
O atributo action está apontado em um Servlet.
Código 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">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:import url="cabecalho.jsp" />
<form action="cadastrarAluno" method="post">
<table>
<tr>
<td>Nome:</td>
<td><input type="text" name="nome" /><br /></td>
</tr>
<tr>
<td>Registro:</td>
<td><input type="text" name="registro" /> <br /></td>
</tr>
<tr>
<td>E-Mail:</td>
<td><input type="text" name="email" /><br /></td>
</tr>
<tr>
<td>Endereço:</td>
<td><input type="text" name="endereco" /><br /></td>
</tr>
<tr>
<td>Data Inscriçao:</td>
<td><input type="text" name="dataInscricao" /><br /></td>
</tr>
</table>
<input type="submit" value="Gravar" />
</form>
<c:import url="rodape.jsp" />
</body>
</html>
Servlet:
package br.com.vroncato.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.vroncato.Aluno;
import br.com.vroncato.AlunoDao;
/**
* Servlet implementation class AlunoCadastrarServlets
*/
@WebServlet("/cadastrarAluno")
public class AlunoCadastrarServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AlunoCadastrarServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String nome = request.getParameter("nome");
String registro = request.getParameter("registro");
String email = request.getParameter("email");
String endereco = request.getParameter("endereco");
String dataInscricao = request.getParameter("dataInscricao");
LocalDate data = null;
try {
data = LocalDate.parse(dataInscricao, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
} catch (DateTimeException ex) {
out.println("Data inválida: " + dataInscricao + ". O formato esperado é dd/MM/yyyy.");
return;
}
Aluno aluno = new Aluno();
aluno.setNome(nome);
aluno.setRegistro(registro);
aluno.setEmail(email);
aluno.setEndereco(endereco);
aluno.setDataInscricao(data);
new AlunoDao().salva(aluno);
out.println("<html>");
out.println("<body>");
out.println("Aluno (a) " + aluno.getNome() + " foi cadastrado (a) com sucesso!");
out.println("</body>");
out.println("</html>");
doGet(request, response);
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" metadata-complete="true" version="3.1">
<display-name>projetoteste</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AlunoCadastrarServlet</servlet-name>
<servlet-class>br.com.vroncato.servlets.AlunoCadastrarServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AlunoCadastrarServlet</servlet-name>
<url-pattern>/cadastrarAluno</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/erro.html</location>
</error-page>
<error-page>
<exception-type>404</exception-type>
<location>/pagina-nao-encontrada.html</location>
</error-page>
</web-app>
Este é o erro onde não encontra o action:
nov 07, 2017 10:35:45 AM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [jsp] in context with path [/projetoteste] threw exception [An exception occurred processing [/aluno-cadastrar.jsp] at line [12]
</head>
<body>
<c:import url="cabecalho.jsp" />
<form action="cadastrarAluno" method="post">
<table>
<tr>
<td>Nome:</td>
Boa tarde,
Tente trocar o código abaixo cadastrarAluno no action do form.
por /cadastrarAluno
Isso deve ser feito pois você mapeou a servlet como /cadastrarAluno quando anotou a classe do servlet com a anotação @WebServlet ("/cadastrarAluno")
https://www.caelum.com.br/apostila-java-web/servlets/#5-6-erros-comuns