Please see this code this may hep you
In web.xml do like this
<filter>
<filter-name>SessionExpireCheck</filter-name>
<filter-class>ManualFilters.SessionExpireFilter</filter-class>
<init-param>
<param-name>redirect</param-name>
<param-value>/Expire.jsp</param-value>
</init-param>
<init-param>
<param-name>LoginPage</param-name>
<param-value>/Login.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionExpireCheck</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
here login page is your login page so that we can skip login page session checking
In your sessionExpireFileter do like this
public class SessionExpireFilter implements Filter {
private HttpSession session;
private
String redirect = null;
private String LoginPage = null;
private FilterConfig filterconfig = null;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filter_chain) throws IOException, ServletException {
redirect = filterconfig.getInitParameter("redirect");
LoginPage = filterconfig.getInitParameter("LoginPage");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
session = request.getSession(false);
if (session != null && !session.isNew() && session.getAttribute("ok") !=null ) {
filter_chain.doFilter(req, res);
}
else
{
if(request.getServletPath().equals(LoginPage))
{
session = request.getSession(true);
session.setAttribute("ok", "ok");
filter_chain.doFilter(req, res);
}
else
{
RequestDispatcher requestDispatcher = request.getRequestDispatcher(redirect);
requestDispatcher.forward(request, response);
}
}
}
public void init(FilterConfig conf) throws ServletException {
setFilterConfig(conf);
}
public void destroy() {
}
public FilterConfig getFilterConfig() {
return filterconfig;
}
public void setFilterConfig(FilterConfig filterconfig) {
this.filterconfig = filterconfig;
}
}