RBz
RBz

Reputation: 935

session-timeout not working for some links in Struts

My application is using Struts 1 and I have configured 60 as the session timeout in my web.xml. I am using Frames and the left side of my view having the menu tree(links) remains same through out and the right side shows the corresponding pages on click. On timeout majority of the links are displaying the login page. But few are not,they are still in the same flow. Please find the configuration and jsp snippets below for the links which are not timing out.

web.xml

<session-config>
  <session-timeout>60</session-timeout>
</session-config>
<login-config>
  <auth-method>FORM</auth-method>
  <realm-name>default</realm-name>
  <form-login-config>
    <form-login-page>/login.jsp</form-login-page>
    <form-error-page>/do/login</form-error-page>
  </form-login-config>
</login-config>

What Not Working

struts-config.xml

<action path="/Report" scope="request" name="form.Report" type="com.mypack.ReportAction" input="/Data.jsp">
  <forward name="show" path="/Data.jsp" />
</action>

menu.jsp

<tr>
  <td height="24"><b><font size="2">
      <html:link forward="/Report" target="main">Reports</html:link></font></b>
  </td>
</tr>

Upvotes: 1

Views: 1746

Answers (2)

IBJ
IBJ

Reputation: 31

You can add filter as below:

add lines below to your web.xml

<filter>
    <filter-name>SessionFilter</filter-name>
    <filter-class>
        com.rbz.SessionFilter
    </filter-class>    
</filter>
<filter-mapping>
    <filter-name>SessionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Then in your sources, add class below

package com.rbz;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter {


    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;    

    // try to get session
    HttpSession session = request.getSession(false);

    String url = request.getServletPath();

    if ((null != session && !session.isNew()) || (null == session && url.contains("login.jsp")) {     
      chain.doFilter(req, res);          
    } else {          
      response.sendRedirect("login.jsp");
    }   


    }

    public void init(FilterConfig config) throws ServletException {
    }
}

This code is not tested !

Upvotes: 0

IBJ
IBJ

Reputation: 31

Session timout is not related to struts but to your web application container. To be sure that every link is redirected to login page when session is dead, you could add a filter in your web.xml and this filter will check the request, if timout is reached then it redirects it to login page, otherwide it let it carry on.

Upvotes: 1

Related Questions