Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF ServletFilter Restriction on Restricted pages when logged in
    primarykey
    data
    text
    <p>I've this servlet filter servlet called everytime a file (images and xhtmls) from my "com.shadibandhan.Restricted" folder is called.</p> <p>I'm using JSF, so there's also <strong>Faces Servlet</strong>.</p> <p>This is my web.xml</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="3.0" 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"&gt; &lt;context-param&gt; &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt; &lt;param-value&gt;Production&lt;/param-value&gt; &lt;/context-param&gt; &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet&gt; &lt;servlet-name&gt;SbServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;com.shadibandhan.ControllerLayer.SbServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;SbServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/SbServlet&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;session-config&gt; &lt;session-timeout&gt; 30 &lt;/session-timeout&gt; &lt;/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;faces/index.xhtml&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;listener&gt; &lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;filter&gt; &lt;filter-name&gt;SessionFilter&lt;/filter-name&gt; &lt;filter-class&gt; com.shadibandhan.ControllerLayer.SessionFilter &lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;avoid-urls&lt;/param-name&gt; &lt;param-value&gt;&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;SessionFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/faces/com.shadibandhan.Restricted/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;filter&gt; &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt; &lt;filter-class&gt;org.primefaces.webapp.filter.FileUploadFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;thresholdSize&lt;/param-name&gt; &lt;param-value&gt;4096&lt;/param-value&gt; &lt;!-- 4 Mb --&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;/filter-mapping&gt; &lt;/web-app&gt; </code></pre> <p>And this is my Servlet Filter named SessionFilter</p> <pre><code>/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.shadibandhan.ControllerLayer; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * * @author MUDASSIR */ public class SessionFilter implements Filter { private ArrayList&lt;String&gt; urlList; @Override public void init(FilterConfig config) throws ServletException { System.out.println("****************************************"); System.out.println("***Session Filter Servlet initialized***"); System.out.println("****************************************"); String urls = config.getInitParameter("avoid-urls"); System.out.println("The urls to avoid are = " + urls); StringTokenizer token = new StringTokenizer(urls, ","); urlList = new ArrayList&lt;String&gt;(); while (token.hasMoreTokens()) { urlList.add(token.nextToken()); } } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { System.out.println("This is the doFilter method"); HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String servletPath = request.getRequestURI(); String contextPath = request.getContextPath(); String remoteHost = request.getRemoteHost(); String url = contextPath + servletPath; System.out.println("-----------------&gt; Servlet path is = " + servletPath); System.out.println("-----------------&gt; Context path is " + contextPath); System.out.println("-----------------&gt; URL is " + url); System.out.println("-----------------&gt; Remote Host is " + remoteHost); boolean allowedRequest = false; if (urlList.contains(servletPath)) { allowedRequest = true; } if (!allowedRequest) { HttpSession session = request.getSession(false); if (null == session) { System.out.println("Session is not present"); response.sendRedirect(contextPath); return; } if (null != session) { //String loggedIn = (String) session.getAttribute("sb_logged_in"); System.out.println("Session is present"); System.out.println("\nSession no. is = " + session.getId()); if (session.getAttribute("logged-in") == "true") { System.out.println("Session logged-in attribute is true, " + session.getAttribute("sessionUsername") + " is logged in."); //ServletContext context = request.getServletContext(); RequestDispatcher dispatcher = request.getRequestDispatcher(servletPath); dispatcher.forward(request, response); } else { System.out.println("Session logged-in attribute is not true"); response.sendRedirect(contextPath); return; } } } chain.doFilter(req, res); } @Override public void destroy() { } } </code></pre> <p>Before, I used request.getServletPath(). Now, i'm using request.getRequestURI() to get the path where the user wants to go.</p> <p>But it's not opening up the page. When ever i try to access the restricted pages, the sessionfilter is called, it gives me this error.</p> <pre><code>type Status report message /ShadiBandhan/ShadiBandhan/faces/com.shadibandhan.Restricted/home.xhtml description The requested resource (/ShadiBandhan/ShadiBandhan/faces/com.shadibandhan.Restricted/home.xhtml) is not available. </code></pre> <p>I've asked the question before but with a different title which made it unclear. <a href="https://stackoverflow.com/questions/11014039/jsf-servletfilter-restriction-on-index-page-when-logged-in">JSF ServletFilter Restriction on index page when logged in</a></p> <p><strong>NOTE</strong> It is adding the context two times. I don't know why. Can anybody please help me. Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload