aspose file tools*
The moose likes Servlets and the fly likes Problem with session timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with session timeout" Watch "Problem with session timeout" New topic
Author

Problem with session timeout

Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi to all,
I have wrote the JSF Code to design the login page.But i have faced one problem in the code.I have time out filter in my code.Whenever i tried to run the code at first time the session is timed out and it redirect to the session time out page.What is the reason for this bug? and How can i resolve this problem?
My Web.xml code is below,

Thanks in advance.

Cheers Munees
My Blog
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How can we debug code we cannot see?
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi David,
Now i attached the code here.Take a look at it and try to resolve my problem.
Thanks.


Try to resolve it.
Thanks.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

That counter is an instance variable and that will not fly. Filters are just like Servlets and the same instance will very probably be reused for several sessions/requests.

ram.

Ggaurav Gupta
Greenhorn

Joined: Mar 26, 2010
Posts: 9
Please see this code this may hep you

In web.xml do like this

<filter>
<filter-name>SessionExpireCheck</filter-name>
<filter-class>ManualFilters.SessionExpireFilter</filter-class>
<init-param>
<param-name>redirect</param-name>
<param-value>/Expire.jsp</param-value>
</init-param>
<init-param>
<param-name>LoginPage</param-name>
<param-value>/Login.jsp</param-value>
</init-param>
</filter>


<filter-mapping>
<filter-name>SessionExpireCheck</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>


here login page is your login page so that we can skip login page session checking



In your sessionExpireFileter do like this

public class SessionExpireFilter implements Filter {

private HttpSession session;
private String redirect = null;
private String LoginPage = null;
private FilterConfig filterconfig = null;


public void doFilter(ServletRequest req, ServletResponse res, FilterChain filter_chain) throws IOException, ServletException {
redirect = filterconfig.getInitParameter("redirect");
LoginPage = filterconfig.getInitParameter("LoginPage");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
session = request.getSession(false);
if (session != null && !session.isNew() && session.getAttribute("ok") !=null ) {
filter_chain.doFilter(req, res);
}
else
{
if(request.getServletPath().equals(LoginPage))
{
session = request.getSession(true);
session.setAttribute("ok", "ok");
filter_chain.doFilter(req, res);
}
else
{
RequestDispatcher requestDispatcher = request.getRequestDispatcher(redirect);
requestDispatcher.forward(request, response);
}
}

}

public void init(FilterConfig conf) throws ServletException {
setFilterConfig(conf);
}

public void destroy() {

}

public FilterConfig getFilterConfig() {
return filterconfig;
}

public void setFilterConfig(FilterConfig filterconfig) {
this.filterconfig = filterconfig;
}


}
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi ram,
You are correct.But i have used flag for skip the filter for first request.Because if i don't skip, the filter is executed and send my control to time-out page.Please have a look at it and try to resolve my problem.
Thanks for your reply.
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Guptha,
Thanks for your reply.But my problem is not resolved.The bug is not fixed.Anyother way to fix this bug?
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

But i have used flag for skip the filter for first request.Because if i don't skip, the filter is executed and send my control to time-out page.Please have a look at it and try to resolve my problem.


It will only skip the first request from the first user that logs in after the server is brought up. You have to think of another strategy. What is it that you are trying to achieve?

ram.

Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi ram,
Thanks for your valuable suggestion.But i can't find another way.If you have any idea please share with me.
Thanks.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

What is it that you are trying to achieve?

ram.
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi ram,
Whenever i try to run with my (url)code,At first time(i.e.)for my first request it redirect to logout page due to session expired.But I trying to go to the login page for my first request.

Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Muneeswaran Balasubramanian wrote:Hi Guptha,
Thanks for your reply.But my problem is not resolved.The bug is not fixed.Anyother way to fix this bug?


The code posted by GG is something I expect would work -- whats the problem you face when you use that code?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi sam,
I have tried to run the code posted by GG.But it also make the same effect.On the time of run the code,the filter is called before the session is created.So it redirect to the session expired page.
What can i do?I need your guidance.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Don't redirect if there's no session? Create a session if there isn't one?
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

The code

if(request.getServletPath().equals(LoginPage))
{
session = request.getSession(true);
session.setAttribute("ok", "ok");
filter_chain.doFilter(req, res);

should redirect the page 'Login.jsp' to the proper page. Ummm your login page is called Login.jsp right?

**********Configured In web.xml here ***************

<init-param>
<param-name>LoginPage</param-name>
<param-value>/Login.jsp</param-value>
</init-param>
Ggaurav Gupta
Greenhorn

Joined: Mar 26, 2010
Posts: 9
Hi Sam Mercs .....

Yes LoginPage is the LoginPage.jsp ........

if(request.getServletPath().equals(LoginPage))
{
session = request.getSession(true);
session.setAttribute("ok", "ok");
filter_chain.doFilter(req, res);


this code means whenever the request is coming from login.jsp a new session is created and one attribute is set into the session "ok" . and for other jsp pages we are checking for session and check for the attribute "ok". In case anyone is facing any issues please call me on 09324053008. I am using this filter for the last 45 day and i m not facing any issues .
Ggaurav Gupta
Greenhorn

Joined: Mar 26, 2010
Posts: 9
Hi Muneeswaran Balasubramanian,

Like to tell you that please make the entries in web.xml.

put your LoginPage as the first page from where you want that the session should be created or from the page where first request is made.
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi to all,
I have resolved the problem.Thanks to all for your guidance.
Cheers Munees
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with session timeout