you can try using filters :
The filter can pre-process the request before it reaches the servlet, resell the response, leaving the servlet, or do both. Filters can intercept, check, and modify requests and responses.
NOTE. be sure to add the session attribute after the user logs in, you can use this session attribute on the filter
on login.jsp :
session.setAttribute("LOGIN_USER", user); //user entity if you have or user type of your user account... //if not set then LOGIN_USER will be null
web.xml
<filter> <filter-name>SessionCheckFilter</filter-name> <filter-class>yourjavapackage.SessionCheckFilter</filter-class> </filter> <filter-mapping> <filter-name>SessionCheckFilter</filter-name> <url-pattern>/main.jsp</url-pattern> </filter-mapping>
SessionCheckFilter.java
public class SessionCheckFilter implements Filter { private String contextPath; @Override public void init(FilterConfig fc) throws ServletException { contextPath = fc.getServletContext().getContextPath(); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; if (req.getSession().getAttribute("LOGIN_USER") == null) {
Joseph Caracuel
source share