I have created a web application. Everything works fine.But, if the user is not logged in still they can have access to other jsp pages through url. I want to stop url access. I saw some example it shows the usage of filters. I'm new to filters I don't how to implement it. I'm using servlets, dao and jsp pages.
Please suggests me how to do it. I want to make one filter for all the jsp or servlets pages.
Web.XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MBO</display-name>
<filter>
<filter-name>MyFiltersDAO</filter-name>
<filter-class>Model.MyFiltersDAO</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFiltersDAO</filter-name>
<url-pattern>/secret/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Class :
public class MyFiltersDAO {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
String abc=(String) req.getSession().getAttribute("Username");
if(null==((String) req.getSession().getAttribute("Username")) || ((String) req.getSession().getAttribute("Username")).equals("")){
chain.doFilter(req, resp);
} else {
resp.sendRedirect("/Login.jsp");
}
}
public void destroy() {
}
}
when i run project it shows HTTP404 error