Código baixado do link: https://caelum-online-public.s3.amazonaws.com/1001-servlets-parte2/05/1001-servlet-parte2-aula5.zip
HTTP Status 500 – Internal Server Error
Type Exception Report
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException
br.com.alura.gerenciador.servlet.AutorizacaoFilter.doFilter(AutorizacaoFilter.java:32)
br.com.alura.gerenciador.servlet.MonitoramentoFilter.doFilter(MonitoramentoFilter.java:28)
Note A pilha de erros completa da causa principal está disponível nos logs do servidor.
Apache Tomcat/9.0.27
package br.com.alura.gerenciador.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//@WebFilter("/entrada")
@SuppressWarnings("unused")
public class AutorizacaoFilter implements Filter {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
System.out.println("AutorizacaoFilter");
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String paramAcao = request.getParameter("acao");
HttpSession sessao = request.getSession();
boolean usuarioNaoEstaLogado = (sessao.getAttribute("usuarioLogado") == null);
boolean ehUmaAcaoProtegida = !(paramAcao.equals("Login") || paramAcao.equals("LoginForm"));
if(ehUmaAcaoProtegida && usuarioNaoEstaLogado) {
response.sendRedirect("entrada?acao=LoginForm");
return;
}
chain.doFilter(request, response);
}
}
package br.com.alura.gerenciador.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
//@WebFilter(urlPatterns="/entrada")
@SuppressWarnings("unused")
public class MonitoramentoFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("MonitoramentoFilter");
long antes = System.currentTimeMillis();
String acao = request.getParameter("acao");
//executa a acao
chain.doFilter(request, response);
long depois = System.currentTimeMillis();
System.out.println("Tempo de execu��o da acao " + acao + " -> " + (depois - antes));
}
}