title: how to disable direct access to pages from url for jsp pages
body: <p>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.</p>
  
  <p>Please suggests me how to do it. I want to make one filter for all the jsp or servlets pages.</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;web-app version="2.5" 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_2_5.xsd"&gt;
          &lt;filter&gt;
              &lt;filter-name&gt;MyFilter&lt;/filter-name&gt;
              &lt;filter-class&gt;com.eis.servlet.MyFilter&lt;/filter-class&gt;
          &lt;/filter&gt;
          &lt;filter-mapping&gt;
              &lt;filter-name&gt;MyFilter&lt;/filter-name&gt;
              &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
          &lt;/filter-mapping&gt;
          &lt;servlet&gt;
              &lt;servlet-name&gt;login&lt;/servlet-name&gt;
              &lt;servlet-class&gt;com.eis.servlet.LoginServlet&lt;/servlet-class&gt;
          &lt;/servlet&gt;
          &lt;servlet&gt;
              &lt;servlet-name&gt;DayWiseServlet&lt;/servlet-name&gt;
              &lt;servlet-class&gt;com.eis.servlet.DayWiseServlet&lt;/servlet-class&gt;
          &lt;/servlet&gt;
          &lt;servlet&gt;
              &lt;servlet-name&gt;RegisterServlet&lt;/servlet-name&gt;
              &lt;servlet-class&gt;com.eis.servlet.RegisterServlet&lt;/servlet-class&gt;
          &lt;/servlet&gt;
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;login&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/LoginServlet&lt;/url-pattern&gt;
          &lt;/servlet-mapping&gt;
          &lt;servlet&gt;
              &lt;servlet-name&gt;RetrieveServlet&lt;/servlet-name&gt;
              &lt;servlet-class&gt;com.eis.servlet.RetrieveServlet&lt;/servlet-class&gt;
          &lt;/servlet&gt;
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;RetrieveServlet&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/RetrieveServlet&lt;/url-pattern&gt;
          &lt;/servlet-mapping&gt;
          &lt;servlet&gt;
              &lt;servlet-name&gt;TimeSheet&lt;/servlet-name&gt;
              &lt;servlet-class&gt;com.eis.servlet.TimeSheet&lt;/servlet-class&gt;
          &lt;/servlet&gt;
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;TimeSheet&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/TimeSheet&lt;/url-pattern&gt;
          &lt;/servlet-mapping&gt;
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;DayWiseServlet&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/DayWiseServlet&lt;/url-pattern&gt;
          &lt;/servlet-mapping&gt;
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;RegisterServlet&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/RegisterServlet&lt;/url-pattern&gt;
          &lt;/servlet-mapping&gt;
          &lt;welcome-file-list&gt;
              &lt;welcome-file&gt;/index.jsp&lt;/welcome-file&gt;
          &lt;/welcome-file-list&gt;
          &lt;session-config&gt;
              &lt;session-timeout&gt;15&lt;/session-timeout&gt;
          &lt;/session-config&gt;
      &lt;/web-app&gt;
  </code></pre>
  
  <p>loginservlet.java</p>
  
  <pre><code>public class LoginServlet extends HttpServlet{  
  
      private static final long serialVersionUID = 1L;  
  
      @Override
      public void doGet(HttpServletRequest request, HttpServletResponse response)    
              throws ServletException, IOException {    
  
  
          response.setContentType("text/html");    
          PrintWriter out = response.getWriter();    
  
          String n=request.getParameter("Emp_id");    
          String p=request.getParameter("Pwd");   
          String Usertype=request.getParameter("usertype"); 
  
  
          HttpSession session = request.getSession(false);  
          if(session!=null){
          session.setAttribute("name", n);  
          session.setAttribute("usertype", Usertype);
          }
          if(LoginDao.validate(n,p)){    
              RequestDispatcher rd=request.getRequestDispatcher("/daywise.jsp");    
              rd.forward(request,response);    
          }    
          else{    
              out.print("&lt;p style=\"color:red\"&gt;Sorry Employee ID or password error&lt;/p&gt;");    
              RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");    
              rd.include(request,response);  
  
          }    
  
          out.close();    
      }
       protected void doPost(HttpServletRequest request,
              HttpServletResponse response)
              throws ServletException, IOException {
          doGet(request, response);
      }
  
  
  }   
  </code></pre>
  
  <p>myfilter:</p>
  
  <pre><code>public class MyFilter implements Filter{  
  
  @Override
  public void init(FilterConfig config) throws ServletException {}  
  
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
  
      HttpServletRequest req = (HttpServletRequest)request;
      HttpServletResponse resp = (HttpServletResponse)response;
  
          if(null==((String) req.getSession().getAttribute("empid")) || ((String) req.getSession().getAttribute("empid")).equals("")){
              chain.doFilter(req, resp);
      } else {
        resp.sendRedirect("/WebTimeSheet/index.jsp");
    }
      }  
  @Override
      public void destroy() {}  
  }  
  </code></pre>
  
  <p>Loginpage:</p>
  
  <pre><code>&lt;form action="LoginServlet" method="post"&gt;  
      &lt;fieldset style="width: 300px"&gt;  
          &lt;legend&gt; Login to App &lt;/legend&gt;   
          &lt;table&gt;  
              &lt;tr&gt;  
                  &lt;td&gt;User ID&lt;/td&gt;  
                  &lt;td&gt;&lt;input type="text" name="Emp_id" required="required" /&gt;&lt;/td&gt;  
              &lt;/tr&gt;  
              &lt;tr&gt;  
                  &lt;td&gt;Password&lt;/td&gt;  
                  &lt;td&gt;&lt;input type="password" name="Pwd" required="required" /&gt;&lt;/td&gt;  
              &lt;/tr&gt; 
             &lt;tr&gt;  
                  &lt;td&gt;User Type&lt;/td&gt;  
                  &lt;td&gt; &lt;select name="usertype"&gt;
                  &lt;option&gt;Employee&lt;/option&gt;
                  &lt;option&gt;Manager&lt;/option&gt;
                  &lt;option&gt;Admin&lt;/option&gt;
              &lt;/select&gt;&lt;/td&gt;   
              &lt;/tr&gt;
              &lt;tr&gt;  
                  &lt;td&gt;&lt;input type="submit" value="Login" /&gt;&lt;/td&gt;  
              &lt;/tr&gt;  
          &lt;/table&gt;  
      &lt;/fieldset&gt;  
  &lt;/form&gt;  
  &lt;/body&gt;  
  &lt;%@include file="/footer.jsp" %&gt;
  &lt;/html&gt;  
  </code></pre>
  
  <p>and all my jsp pages are in the web pages folder which is outside the Web-inf folder. Web-inf folder only got web.xml init</p>
  
  <p>Header.jsp</p>
  
  <pre><code> &lt;c:choose&gt;
               &lt;c:when test="${usertype eq 'Employee'}"&gt;
          &lt;div class="nav"&gt;      
              &lt;ul&gt;&lt;li class="container"&gt;&lt;img src="${pageContext.request.contextPath}/images/enabling.jpg" /&gt;&lt;/li&gt;
              &lt;li class="current"&gt;&lt;a href="WEB-INF/daywise.jsp"&gt;DayWise TimeSheet&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="WEB-INF/timesheet.jsp"&gt;Weekly TimeSheet&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
       &lt;/c:when&gt;
       &lt;c:when test="${usertype eq 'Manager'}"&gt;
          &lt;div class="nav"&gt;      
              &lt;ul&gt;&lt;li class="container"&gt;&lt;img src="${pageContext.request.contextPath}/images/enabling.jpg" /&gt;&lt;/li&gt;
              &lt;li class="current"&gt;&lt;a href="/WEB-INF/daywise.jsp"&gt;DayWise TimeSheet&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="WEB-INF/timesheet.jsp"&gt;Weekly TimeSheet&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="WEB-INF/newemployee.jsp"&gt;Add New Employeer&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="WEB-INF/retrieve.jsp"&gt;Retrieve TimeSheet&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
       &lt;/c:when&gt;
  </code></pre>