<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Tutorial: HTTP Session handling using Servlet Filters</title>
	<atom:link href="http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/feed" rel="self" type="application/rss+xml" />
	<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html</link>
	<description>Tutorials, Java, J2EE, Struts, AJAX, JavaScript, CSS, Web 2.0, MySQL, Articles</description>
	<lastBuildDate>Wed, 17 Mar 2010 06:28:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Viral Patel</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-12187</link>
		<dc:creator>Viral Patel</dc:creator>
		<pubDate>Tue, 23 Feb 2010 18:41:35 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-12187</guid>
		<description>@Chandra: The above example will work for you in any case. i.e. it will work for JSPs, Servlets or Struts2 as well. Filters are available for both servlets and JSPs because we have mapped /* url with filter SessionFilter.</description>
		<content:encoded><![CDATA[<p>@Chandra: The above example will work for you in any case. i.e. it will work for JSPs, Servlets or Struts2 as well. Filters are available for both servlets and JSPs because we have mapped /* url with filter SessionFilter.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chandra</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-12186</link>
		<dc:creator>Chandra</dc:creator>
		<pubDate>Tue, 23 Feb 2010 17:53:16 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-12186</guid>
		<description>Hi Viral,

Can we do the same thing..i.e when user copy paste the URL in new window, he must be redirected to Login page using JSPs. I am using Struts 2.0 framework. I am not using servlets. Filters are available for servlets only or for JSPs also?

TIA</description>
		<content:encoded><![CDATA[<p>Hi Viral,</p>
<p>Can we do the same thing..i.e when user copy paste the URL in new window, he must be redirected to Login page using JSPs. I am using Struts 2.0 framework. I am not using servlets. Filters are available for servlets only or for JSPs also?</p>
<p>TIA</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Selvanayagam</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-11658</link>
		<dc:creator>Selvanayagam</dc:creator>
		<pubDate>Tue, 17 Nov 2009 05:15:37 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-11658</guid>
		<description>I have a problem

       After the session expires. the user is asked to relogin. When he relogins 
he must be redirected to the page the where the session expired. I please to send me your
reply to mail id. I am waiting for your reply

thanks in advance</description>
		<content:encoded><![CDATA[<p>I have a problem</p>
<p>       After the session expires. the user is asked to relogin. When he relogins<br />
he must be redirected to the page the where the session expired. I please to send me your<br />
reply to mail id. I am waiting for your reply</p>
<p>thanks in advance</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-11608</link>
		<dc:creator>Keith</dc:creator>
		<pubDate>Mon, 09 Nov 2009 18:39:43 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-11608</guid>
		<description>Hello Viral,

Thank you for this tutorial it&#039;s been very useful.  Is there a way to send the user to the page they&#039;ve selected when the session expired? 

What I have done is to store the selected link as an attribute and tried to alter the response by setting it as the string of the response.sendRedirect but this isn&#039;t working for me.  Do you have any advice as to how I might be able to achieve this?

Thanks.</description>
		<content:encoded><![CDATA[<p>Hello Viral,</p>
<p>Thank you for this tutorial it&#8217;s been very useful.  Is there a way to send the user to the page they&#8217;ve selected when the session expired? </p>
<p>What I have done is to store the selected link as an attribute and tried to alter the response by setting it as the string of the response.sendRedirect but this isn&#8217;t working for me.  Do you have any advice as to how I might be able to achieve this?</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-11571</link>
		<dc:creator>Keith</dc:creator>
		<pubDate>Fri, 30 Oct 2009 15:25:47 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-11571</guid>
		<description>When my session expires it&#039;s not making it to the filter? Any idea of what I might be missing that is causing this to happen?</description>
		<content:encoded><![CDATA[<p>When my session expires it&#8217;s not making it to the filter? Any idea of what I might be missing that is causing this to happen?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viral Patel</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-11568</link>
		<dc:creator>Viral Patel</dc:creator>
		<pubDate>Fri, 30 Oct 2009 13:57:50 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-11568</guid>
		<description>Hi daniel, Thanks for sharing useful link.</description>
		<content:encoded><![CDATA[<p>Hi daniel, Thanks for sharing useful link.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: daniel</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-11560</link>
		<dc:creator>daniel</dc:creator>
		<pubDate>Thu, 29 Oct 2009 12:35:46 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-11560</guid>
		<description>I had the problem with “Cannot create a session after the response has been committed” when using filter and JSF.
The solution was to do a requestDispatcher.forward instead of response.sendRedirect.
Look here:
http://javaboutique.internet.com/tutorials/Servlet_Filters/redirect.html</description>
		<content:encoded><![CDATA[<p>I had the problem with “Cannot create a session after the response has been committed” when using filter and JSF.<br />
The solution was to do a requestDispatcher.forward instead of response.sendRedirect.<br />
Look here:<br />
<a href="http://javaboutique.internet.com/tutorials/Servlet_Filters/redirect.html" rel="nofollow">http://javaboutique.internet.com/tutorials/Servlet_Filters/redirect.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FlorianR</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-11511</link>
		<dc:creator>FlorianR</dc:creator>
		<pubDate>Fri, 16 Oct 2009 10:43:58 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-11511</guid>
		<description>Hi dod,

you&#039;ve got an error in line 34, please remove the semicolon.
Additionally i get the &quot;Cannot create a session after the response has been committed&quot;-message, too, although i don&#039;t put anything in out?!

Any idea?

--------------
public class SessionFilter implements Filter {
	private ArrayList urlList;
	private int totalURLS;

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String url = request.getServletPath();
		boolean noSessionNeeded = false;

		for(int i=0; i&lt;totalURLS; i++) {
			String pattern=urlList.get(i);
			if(url.contains(pattern)) {
				noSessionNeeded = true;
				break;
			}
		}

		if (!noSessionNeeded) {
			HttpSession session = request.getSession(false);
			boolean exists=(session!=null);
			boolean isNew=session.isNew();
			if(!exists) response.sendRedirect(&quot;index.jsp&quot;);
			if(isNew) response.sendRedirect(&quot;index.jsp&quot;);
		}

		chain.doFilter(req, res);
	}

	public void init(FilterConfig config) throws ServletException {
		String urls = config.getInitParameter(&quot;noSessionNeeded&quot;);
		StringTokenizer token = new StringTokenizer(urls, &quot;,&quot;);

		urlList = new ArrayList();

		while (token.hasMoreTokens()) {
			urlList.add(token.nextToken());

		}
		totalURLS = urlList.size();
	}
}</description>
		<content:encoded><![CDATA[<p>Hi dod,</p>
<p>you&#8217;ve got an error in line 34, please remove the semicolon.<br />
Additionally i get the &#8220;Cannot create a session after the response has been committed&#8221;-message, too, although i don&#8217;t put anything in out?!</p>
<p>Any idea?</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8211;<br />
public class SessionFilter implements Filter {<br />
	private ArrayList urlList;<br />
	private int totalURLS;</p>
<p>	public void destroy() {<br />
	}</p>
<p>	public void doFilter(ServletRequest req, ServletResponse res,<br />
			FilterChain chain) throws IOException, ServletException {</p>
<p>		HttpServletRequest request = (HttpServletRequest) req;<br />
		HttpServletResponse response = (HttpServletResponse) res;<br />
		String url = request.getServletPath();<br />
		boolean noSessionNeeded = false;</p>
<p>		for(int i=0; i&lt;totalURLS; i++) {<br />
			String pattern=urlList.get(i);<br />
			if(url.contains(pattern)) {<br />
				noSessionNeeded = true;<br />
				break;<br />
			}<br />
		}</p>
<p>		if (!noSessionNeeded) {<br />
			HttpSession session = request.getSession(false);<br />
			boolean exists=(session!=null);<br />
			boolean isNew=session.isNew();<br />
			if(!exists) response.sendRedirect(&quot;index.jsp&quot;);<br />
			if(isNew) response.sendRedirect(&quot;index.jsp&quot;);<br />
		}</p>
<p>		chain.doFilter(req, res);<br />
	}</p>
<p>	public void init(FilterConfig config) throws ServletException {<br />
		String urls = config.getInitParameter(&quot;noSessionNeeded&quot;);<br />
		StringTokenizer token = new StringTokenizer(urls, &quot;,&quot;);</p>
<p>		urlList = new ArrayList();</p>
<p>		while (token.hasMoreTokens()) {<br />
			urlList.add(token.nextToken());</p>
<p>		}<br />
		totalURLS = urlList.size();<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viral Patel</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-7842</link>
		<dc:creator>Viral Patel</dc:creator>
		<pubDate>Thu, 28 May 2009 15:35:09 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-7842</guid>
		<description>Nice to see that your error got resolved :-)
I think the code  HttpSession session = request.getSession(); needs to be replaced with  HttpSession session = request.getSession(false); otherwise everytime a new session will get created.</description>
		<content:encoded><![CDATA[<p>Nice to see that your error got resolved :-)<br />
I think the code  HttpSession session = request.getSession(); needs to be replaced with  HttpSession session = request.getSession(false); otherwise everytime a new session will get created.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dod</title>
		<link>http://viralpatel.net/blogs/2009/02/http-session-handling-tutorial-using-servlet-filters-session-error-filter-servlet-filter.html/comment-page-1#comment-7841</link>
		<dc:creator>dod</dc:creator>
		<pubDate>Thu, 28 May 2009 15:26:47 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=798#comment-7841</guid>
		<description>Ok, so the code seems to be working

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;  
        HttpServletResponse response = (HttpServletResponse) res;  
        
        String url = request.getServletPath();  
        String contextPath = request.getContextPath();
        boolean allowedRequest = false;  
  
        for(int i=0; i&lt;totalURLS; i++) {  
        	if(url.contains(urlList.get(i))) {  
                allowedRequest = true;  
                break;  
            }  
        }  
        if (!allowedRequest) {  
        	HttpSession session = request.getSession();  
            if (null == session) {  
            	response.sendRedirect(contextPath+&quot;/jsp/loginError.jsp&quot;);  
            }else{
            	String logged = (String) session.getAttribute(&quot;logged-in&quot;);
            	if (logged == null){
                 	response.sendRedirect(contextPath+&quot;/jsp/loginError.jsp&quot;);  
        	    }else{
                	chain.doFilter(request, response);
                }
            }  
        }  else{
        	chain.doFilter(request, response);
        }
		
	}</description>
		<content:encoded><![CDATA[<p>Ok, so the code seems to be working</p>
<p>	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {</p>
<p>        HttpServletRequest request = (HttpServletRequest) req;<br />
        HttpServletResponse response = (HttpServletResponse) res;  </p>
<p>        String url = request.getServletPath();<br />
        String contextPath = request.getContextPath();<br />
        boolean allowedRequest = false;  </p>
<p>        for(int i=0; i&lt;totalURLS; i++) {<br />
        	if(url.contains(urlList.get(i))) {<br />
                allowedRequest = true;<br />
                break;<br />
            }<br />
        }<br />
        if (!allowedRequest) {<br />
        	HttpSession session = request.getSession();<br />
            if (null == session) {<br />
            	response.sendRedirect(contextPath+&#8221;/jsp/loginError.jsp&#8221;);<br />
            }else{<br />
            	String logged = (String) session.getAttribute(&#8220;logged-in&#8221;);<br />
            	if (logged == null){<br />
                 	response.sendRedirect(contextPath+&#8221;/jsp/loginError.jsp&#8221;);<br />
        	    }else{<br />
                	chain.doFilter(request, response);<br />
                }<br />
            }<br />
        }  else{<br />
        	chain.doFilter(request, response);<br />
        }</p>
<p>	}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
