你扩展RequestProcess类就可以了。
让session失效的话,在tomcat中设置就行了,在conf目录下的web.xml中
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

解决方案 »

  1.   

    在servlet开始添加 
    HttpSession session=request.getSession(false);
    if (session==null)
    response.sendRedirect("Index.jsp");HttpSession类有一个方法设置失效时间 通过该方法设置最大失效时间为15×60就可以了
    good luck~:)
      

  2.   

    你是struts结构就是在具体Action类的execute()方法中添加以上那句话
      

  3.   

    对struts平台的扩展,可以在平台上进行权限控制等操作,扩展RequestProcessor.java类。
    1.在这个类中,只要对processPreprocess方法进行相对应的扩充,就可以了。
    public class MyRequestProcessor extends RequestProcessor{
    /**
     * <p>Process an <code>HttpServletRequest</code> and create the
     * corresponding <code>HttpServletResponse</code>.</p>
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a processing exception occurs
     */
    public void process(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException { // Wrap multipart requests with a special wrapper
    request = processMultipart(request); // Identify the path component we will use to select a mapping String path = processPath(request, response);
    System.out.println("33333333333333333333333="+path);
    if (path == null) {
    return;
    }
    if (log.isDebugEnabled()) {
    log.debug("Processing a '" + request.getMethod() +
      "' for path '" + path + "'");
    } // Select a Locale for the current user if requested
    processLocale(request, response); // Set the content type and no-caching headers if requested
    processContent(request, response);
    processNoCache(request, response); // General purpose preprocessing hook
    if (!processPreprocess(request, response)) {
    path="/fail";
    System.out.println("11111111111="+path);
    //return;
    } // Identify the mapping for this request
    ActionMapping mapping = processMapping(request, response, path);
    if (mapping == null) {
    return;
    } // Check for any role required to perform this action
    if (!processRoles(request, response, mapping)) {
    return;
    } // Process any ActionForm bean related to this request
    ActionForm form = processActionForm(request, response, mapping);
    processPopulate(request, response, form, mapping);
    if (!processValidate(request, response, form, mapping)) {
    return;
    } // Process a forward or include specified by this mapping
    if (!processForward(request, response, mapping)) {
    return;
    }
    if (!processInclude(request, response, mapping)) {
    return;
    } // Create or acquire the Action instance to process this request
    Action action = processActionCreate(request, response, mapping);
    if (action == null) {
    return;
    } // Call the Action instance itself
    ActionForward forward =
    processActionPerform(request, response,
     action, form, mapping); // Process the returned ActionForward instance
    processForwardConfig(request, response, forward); }
    /**
     * General-purpose preprocessing hook that can be overridden as required
     * by subclasses.  Return <code>true</code> if you want standard processing
     * to continue, or <code>false</code> if the response has already been
     * completed.  The default implementation does nothing.
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     */ protected boolean processPreprocess(HttpServletRequest request,
    HttpServletResponse response) {
    HttpSession session = request.getSession();
    //String ss = (String) session.getAttribute("ss");
    String ss="sdfds";
    System.out.println("---------------------processPreprocess=" + ss);
    if (ss != null) {
    return (true);
    }else {
    return false;
    } }}
    2。在struts-config.xml中的配置
    <controller   processorClass="com.mystruts.MyRequestProcessor" />
      

  4.   

    1)if(request.getSession() == null)
         return actionMapping.findForward(你要转向的指定页面);
    2)如楼上,把30改为15就满足你的要求了。
      

  5.   

    扩展 RequestProcessor 能让session失效时自动转向吗?
    我想该用个监听器吧
      

  6.   

    to: d80(今天没事做)如果向转到login.jsp,请问应该怎么写??
      

  7.   

    在processPreprocess方法中有返回true和false的。
    在process方法中有调用processPreprocess的地方。
    if (!processPreprocess(request, response)) {
    path="/fail";
    System.out.println("11111111111="+path);
    //return;
    }
    你在这里的path设为你想要跳转的路径就行了。
      

  8.   

    在action加如下语句(在action的execute方法的开始就加上)
    HttpSession session=request.getSession(false);
    if (session==null)
    response.sendRedirect("***.jsp");
      

  9.   

    为什么session已经超时啦,但仍不为null HttpSession session=request.getSession(false);
    if (session!=null) {
    return (true);
    }else {
    return false;
    }
    而这段代码的else永远都不会执行。
      

  10.   

    判断一个属性是否在session里面,比如用户的登陆属性,如果没有了说明登陆的session已经过期了
      

  11.   

    这样恐怕不行吧,登陆的action也被process调用,而这时session还没有用户的登陆属性呢!这样就造成第次运行Action都返回这个页面啦!
      

  12.   

    利用HttpSessionBindingListenter接口!
    session失效时会激活valueUnbound事件,你在这里面控制就可以了。
      

  13.   

    to:s_phoenix能给个例子吗?谢谢
      

  14.   

    import javax.servlet.http.HttpSessionBindingEvent;
    import javax.servlet.http.HttpSessionBindingListener;
    public class UserTrace implements HttpSessionBindingListener { //当UserTrace被加入session对象时会调用此方法
    public void valueBound(HttpSessionBindingEvent event) { }
    //当UserTrace被移出session时会调用此方法
    public void valueUnbound(HttpSessionBindingEvent event) { }}
    这是我写的一个在线用户功能的片断(程序框架),你可以参照看看。
    具体的方法你就自己写了。
      

  15.   

    这个类写完之后,在什么地方调用呢,是不是需要要配置文件中标记一下呀??在你设置SESSION的时候把实现HttpSessionBindingListener的类写进去,例如:
    session.setAttribute("Binding",new HttpSessionBindingListener);
    其中new HttpSessionBindingListener是你实现该接口的类的实例
      

  16.   

    <session-config>
            <session-timeout>30</session-timeout>
        </session-config>
    可以设置有效时间。
      

  17.   

    这样调用:
    <jsp:useBean id="usertrace" class="com.xx.xx.UserTrace" scope="session"/>
      

  18.   

    为什么session已经超时啦,但仍不为null
    HttpSession session=request.getSession(false);
    if (session!=null) {
    return (true);
    }else {//我这里也是永远不执行,为什么啊????
    return false;
    }
    然后打印出来的sessionId都是一个新id,为什么session=request.getSession(false);参数是false,居然还给我创建了新session??????????????????????
      

  19.   

    问题1:用HttpSessionBindingListener这个方法的话,岂不是还得判断是 session过期触发 还是 用户正常删除某个   session变量??问题2:在这个valueUnbound里边如何转出到某页面?
    public void valueUnbound(HttpSessionBindingEvent event) {
                 //如何说失效时候跳转到xx.jsp???
    }
      

  20.   

    在 Page_OnInit()事件中 if (Session["Loginid"]==null)
    {
       ..跳转到你要去的页面
    }
      

  21.   

    唉,又是 strusts.
    strusts中我不知道,我只能告诉你平时的方法关于如何判断SESSION过期就不说了,主要说说在 过期后的处理方式。一、使用javascript提示然后再跳转
    <%
    if(.....){//SESSION过期
    %>
    <body onload="alert('session timeout.');window.locaitin='XXXXXX'">
    </body>
    <%
        return;    
    }
    %>
    二、直接再后台跳转。
    <%
    if(.....){//SESSION过期
        response.sendRedirect("XXXXXXX"); 
    }
    %>
      

  22.   

    能不能在配置文件中设置呢,例如在web.xml或struts-config.xml中设置呢??
      

  23.   

    做个HTTP会话监听SERVLET能够监听session的销毁并触发相应事件
      

  24.   

    用过滤器吧,和监听器一起使用也许效果更好:
    -------------------web.xml配置:
        <filter>
            <filter-name>mainfilter</filter-name>
            <filter-class>com.chance.MainFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>mainfilter</filter-name>
            <url-pattern>*</url-pattern>
        </filter-mapping>
    ------------------MainFilter.java--------------------------
    package com.chance;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.*;
    import java.io.IOException;public class MainFilter implements Filter{
        public void init(FilterConfig filterConfig)throws ServletException{
            //
        }    public void doFilter(ServletRequest request,
                             ServletResponse response,FilterChain chain)throws ServletException, IOException{
            HttpServletRequest req = (HttpServletRequest)request;
            HttpServletResponse res = (HttpServletResponse)response;
            boolean isValid=true;
            ///////check attribute
            isValid=req.getSession().getAttribute("name")==null;
            if(isValid){
                req.getSession().getServletContext()
                        .getRequestDispatcher("/login.jsp").forward(req, res);
            }else
                chain.doFilter(request,response);
        }    public void destroy(){}
    }
      

  25.   

    我的代码如下:
    public class MyHttpSessionAttributeListener
    implements HttpSessionAttributeListener,HttpSessionListener { /**
     * 
     */

    private static List list=new ArrayList();
    public MyHttpSessionAttributeListener() {
    super();
    // TODO Auto-generated constructor stub
    } /* (non-Javadoc)
     * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent se) {
    if("userInfo".equals(se.getName()))
    {
    UserForm user=(UserForm)se.getValue();
    Iterator userList=list.iterator();
    if(!list.contains(user.getUserId()))
    {
    //list.add(se.getValue());
    list.add(user.getUserId());
    }else
    {
    // se.getSession().invalidate();
    // se.getSession().setAttribute("userInfo",user);
    System.out.println("重复登陆");
    // throw new Exception("重复登陆");
    }
    System.out.println("user added:"+se.getValue());
    } } /* (non-Javadoc)
     * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent se) {
    if("userInfo".equals(se.getName()))
    {
    String userId=((UserForm)se.getValue()).getUnitId();
    list.remove(userId);
    System.out.println("user removed:"+userId);
    }
    } /* (non-Javadoc)
     * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent arg0) {
    // TODO Auto-generated method stub } /* (non-Javadoc)
     * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent arg0) {
    // TODO Auto-generated method stub

    } /* (non-Javadoc)
     * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent se) {
    String userId = (String)se.getSession().getAttribute("userid");
    list.remove(userId);
    System.out.println("user removed:"+userId);

    }}但不知道一个帐号不能同时两个人用怎么做?而在这个类中能跳转到其它页面吗?
      

  26.   

    在监听程序里不能实现指定用户跳转页面的吧?~~除非传response过去~不过怎么传给监听程序啊?
      

  27.   

    to:chancelin() 你的方法我试啦,但如果按这种方法过滤的话,用户登陆前Session一定是NULL,而这时过滤的结果就是总是会转向到            req.getSession().getServletContext()
                        .getRequestDispatcher("/login.jsp").forward(req, res);
    而不会继续执行后面的代码!
      

  28.   

    首先在用户登陆后在session里维护一个user类的实例,user类有关于登陆客户的信息,在struts的jsp模板文件里判断,如果session.getAttribute("user")==null为true,就显示另一个页面,在这个页面的load事件里用javascript跳转到login.jsp,这是肯定可以的.
      

  29.   

    不好做。用户有请求的话,SESSION就不会过期。用户无请求的话。WEB服务器怎么主动去变化客户端浏览器的地址呢?WEB服务是有请求才有响应的。似乎办不到吧。
    我上面说的方法只能检测到SESSION过期,但是过期了怎么让客户端页面跳转?好像办不到?
      

  30.   

    恩~~楼上的和我想得一样~~
    过期了怎么让客户端页面跳转???如果用户关掉浏览器使得SESSION过期,更加不可能是他跳转了吧?
      

  31.   

    有的网站做成的效果是这样的!例如session的最大超时时间设为30分钟,当用户挂机超过30分钟时,网页自动转到了登陆页面,这是怎么实现的呢??
      

  32.   

    应该不是session失效~~是判断登陆时间的吧
      

  33.   

    在web.xml中设置session的有效时间判断session中你的对象是否是null,如果是null就forward到你指定的页面
      

  34.   

    试试在JSP中导入其他线程类,在线程中检测.
      

  35.   

    觉得线程类到是可以做到~~每个用户登入的servlet里面运行监听器线程,并且传request和response过去不过这样线城什么时候销毁就是个问题了