session

用的是struts框架
过滤器部分代码:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException
  {
  HttpServletRequest httpreq = (HttpServletRequest)request;
  HttpSession session = httpreq.getSession();
  String url = httpreq.getRequestURI();
  url = url.substring(url.lastIndexOf("/") + 1, url.length());
  String exclude = "login.do,exit.do";
  if(exclude.indexOf(url) == -1)
  {
  String id = (String)session.getAttribute("id");
  if(id == null || "".equals(id))
  {
  HttpServletResponse httprep = (HttpServletResponse)response;
  httprep.sendRedirect("/chaoshi.jsp");
  } else
  {
  chain.doFilter(request, response);
  }
  } else
  {
  chain.doFilter(request, response);
  }
  }
  
web.xml部分:
<filter>
  <filter-name>sessionTimeout</filter-name>
  <filter-class>com.filter.SessionTimeout</filter-class>
</filter>
<filter-mapping>
  <filter-name>sessionTimeout</filter-name>
  <url-pattern>*.do</url-pattern>
</filter-mapping>

登录login.do部分代码:
  HttpSession session = request.getSession(TRUE);
  session.setMaxInactiveInterval(30*60);
  session.setAttribute("id", id);

你可能感兴趣的:(session)