【问题标题】:i need to redirect page after particular session count我需要在特定会话计数后重定向页面
【发布时间】:2014-11-05 07:49:07
【问题描述】:

我正在创建一个 Web 应用程序,在该应用程序中我正在创建 sessionlistener。在该会话侦听器中,如果条件不起作用,如何在达到特定会话计数后将页面重定向到另一个页面。

import java.awt.Window;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.xml.ws.Response;


public class SessionListener implements HttpSessionListener {
    private int sessionCount = 0;
    HttpServletResponse response;

    public void sessionCreated(HttpSessionEvent event) {
        synchronized (this) {

            sessionCount++; 

            if(sessionCount>=2)
            {

                response.sendRedirect("Error.jsp");
            }

        }

        System.out.println("Session Created1: " + event.getSession().getId());



        System.out.println("Total Sessions1: " + sessionCount);



    }

    public void sessionDestroyed(HttpSessionEvent event) {
        synchronized (this) {
            sessionCount--;
        }
        System.out.println("Session Destroyed: " + event.getSession().getId());
        System.out.println("Total Sessions: " + sessionCount);
    }
}

我将这个类文件映射到 xml 中。

【问题讨论】:

    标签: java jsp session servlets servlet-listeners


    【解决方案1】:

    你不能从你的Listenerredirect,对于重定向,你必须在serveletjspfilter中编写重定向代码。

    这是解决方案。

    Session_Count存储在application scope中,并通过这两种方式检查每个网页上Session_count的值。

    a.) 使用\* url-pattern 创建一个Filter,并在其中添加重定向代码。

    b.) 在所有其他网页的顶部包括一个common jsp(将包含重定向代码)。 或者在每个网页中手动添加重定向代码。

    • 更新你的监听器

      公共类 SessionListener 实现 HttpSessionListener { 私有整数会话计数;

      public void sessionCreated(HttpSessionEvent event) {
          synchronized (this) {
              ServletContext application = event.getSession().getServletContext();
              sessionCount = (Integer) application.getAttribute("SESSION_COUNT");
              if (sessionCount == null) {
                  application.setAttribute("SESSION_COUNT", (sessionCount = 1));//setting sessioncount inside application scope
              } else {
                  application.setAttribute("SESSION_COUNT", sessionCount + 1);
              }
              System.out.println("Session Created1: "+ event.getSession().getId());
              System.out.println("Total Sessions1: " + sessionCount);
          }
      }
      
      public void sessionDestroyed(HttpSessionEvent event) {
          synchronized (this) {
              ServletContext application = event.getSession().getServletContext();
              sessionCount = (Integer) application.getAttribute("SESSION_COUNT");
              application.setAttribute("SESSION_COUNT", sessionCount - 1);
          }
          System.out.println("Session Destroyed: " + event.getSession().getId());
          System.out.println("Total Sessions: " + sessionCount);
      }
      

      }

    • 使用 <url-pattern>/*</url-pattern> 创建过滤器,如下所示。

          @WebFilter("/*")
          public class redirectOnSessionCount implements Filter {
          public void destroy() {
              // TODO Auto-generated method stub
          }
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
              HttpServletResponse httpResponse = (HttpServletResponse) response;
              HttpServletRequest httpRequest = (HttpServletRequest)request;
              Integer sessionCount = (Integer) request.getServletContext().getAttribute("SESSION_COUNT");//fetching session count from application scope
                  if(sessionCount!=null && sessionCount>2 && ! httpRequest.getRequestURL().toString().contains("Error.jsp")){
                   //httpRequest.getRequestURL().toString().contains("Error.jsp") - > if it's already redirecting to error.jsp then no redirection 
                      httpResponse.sendRedirect("Error.jsp");//redirection code
                  }
              chain.doFilter(request, response);
          }
      
          public void init(FilterConfig fConfig) throws ServletException {
              // TODO Auto-generated method stub
          }
      
      }
      
    • web.xml 内映射过滤器。

      <filter>
          <filter-name>redirectOnSessionCount</filter-name>
          <filter-class>redirectOnSessionCount</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>redirectOnSessionCount</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      

    更新:-

    • 不使用过滤器

    如下创建redirectOnSessionCount.jsp,并将其包含在顶部的所有页面中,或将此代码添加到每个网页。

    <%
    Integer sessionCount = (Integer) application.getAttribute("SESSION_COUNT");//fetching session count from application scope
    if(sessionCount!=null && sessionCount>2){
        response.sendRedirect("Error.jsp");//redirection code
    }
    %>
    

    【讨论】:

    • 嗨 vikrant,我不知道在 xml 文件中映射什么,你能帮帮我吗?我有 4 个 jsp 文件索引、添加用户、销毁和错误。如果会话计数达到限制,它会转到错误页面,直到一个会话终止。我已经创建了您发送给我的类文件 redirectOnSessionCount.java。我不知道要在 xml 中更改什么,因为我是 xml 文件的新手,请帮助我。谢谢。
    • 嗨 Vikrant,我的 web.xml 文件我只是自己编辑。它是否正确?? index.jsp sessionListener net.viralpatel.servlet .listener.SessionListener redirectOnSessionCountredirectOnSessionCountredirectOnSessionCount/*
    • 是的,看起来不错:),嘿,我在过滤器中做了一些更改,也更新了您的过滤器:)
    • 嗨朋友,我使用过滤器检查了这两种类型并包含 jsp,但两者都不起作用,因为我设置条件 sessioncount>2 但它超过 2 但它应该重定向到 error.jsp 知道
    • 我没有收到 ant 错误,但它超出了会话限制。我设置的会话限制不超过 2 但它超过了会话限制 2 。如果超过意味着它应该去错误页面正确
    【解决方案2】:

    您没有实例化响应,您很可能在 response.sendRedirect 语句中获得 NPE。侦听器可能不是尝试重定向的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 2017-09-11
      • 2023-03-20
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      相关资源
      最近更新 更多