Reputation: 728
I'm trying to do a servlet filter for a JSP project in Netbeans. What I want to do is to check if a user is logged in, if it is not, then redirect it to the logon page. I followed this tutorial:
https://stackoverflow.com/tags/servlet-filters/info
So I have this java file as my Filter class (file name is LoginFilter.java):
import java.io.IOException;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebFilter("/app/*")
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
// If you have any <init-param> in web.xml, then you could get them
// here by config.getInitParameter("name") and assign it as field.
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("usuario") == null) {
response.sendRedirect(request.getContextPath() + "/login.jsp"); // No logged-in user found, so redirect to login page.
} else {
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
}
@Override
public void destroy() {
// If you have assigned any expensive resources as field of
// this Filter class, then you could clean/close them here.
}
}
Here is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping>
However this is not working. Could someone please tell me what I'm missing?
Upvotes: 1
Views: 4187
Reputation: 728
I managed to solve the problem, the thing is that every time that a page is loaded, the filter checks the code, and in my case I was not declaring my variable "usuario" anywhere, this varialbe is the one that make possible to redirect to login page or continue loading the current page. so I made changes to set the variable when the user logs in (an auxiliar page checks the login, and the variable is set in there). Then all the other pages do not set the variable, they just take what was set on the login check.
Thanks a lot to the people who answered on the comments.
Upvotes: 1