Bom dia galera.
estou tendo um problema quando criei a classe Cookies, estou recebendo HTTP Status 404:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.67
Segue o código:
Login
package br.com.alura.gerenciador.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.alura.gerenciador.Usuario;
import br.com.alura.gerenciador.dao.UsuarioDAO;
@WebServlet(urlPatterns = "/login")
public class Login extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String email = request.getParameter("email");
String senha = request.getParameter("senha");
new UsuarioDAO().adicionaUsuario(email, senha);
PrintWriter writer = response.getWriter();
Usuario usuarioBuscado = new UsuarioDAO().buscaPorEmailESenha(email, senha);
writer.println("<html>");
if (usuarioBuscado != null) {
Cookie cookieCriado = new Cookie("usuario.logado", email);
response.addCookie(cookieCriado);
writer.println(email+"<p> bem vindo!");
}
// ===========botao voltar==========
writer.println("<form action='./' method='post'>");
writer.println("<input type='submit' value='Voltar' /><br>");
writer.println("</form>");
writer.println("</html>");
// =================================
}
}
FiltroDeAuditoria
package br.com.alura.gerenciador.web;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebFilter(urlPatterns = "/*")
public class FiltroDeAuditoria implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String usuarioLogado = "Visitante";
Cookie[] listaDeCookies = req.getCookies();
if (listaDeCookies !=null) {
Cookie cookieUsuarioLogado = new Cookies(listaDeCookies).getUsuarioLogado();
usuarioLogado=cookieUsuarioLogado.getValue();
}
System.out.println(usuarioLogado+ " acessando a URI "
+ req.getRequestURI());
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
Cookies
package br.com.alura.gerenciador.web;
import javax.servlet.http.Cookie;
public class Cookies {
private final Cookie[] cookies;
public Cookies(Cookie[] cookies) {
this.cookies = cookies;
}
public Cookie getUsuarioLogado() {
if (cookies == null)
return null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals("usuario.logado")) {
return cookie;
}
}
return null;
}
}