Java 我需要在特定会话计数后重定向页面

Java 我需要在特定会话计数后重定向页面,java,jsp,session,servlets,servlet-listeners,Java,Jsp,Session,Servlets,Servlet Listeners,我正在创建一个web应用程序,在该应用程序中我正在创建sessionlistener。在该会话侦听器中,如果条件不起作用,如何在达到特定会话计数后将页面重定向到另一个页面 import java.awt.Window; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; i

我正在创建一个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映射这个类文件。

你不能从你的
监听器
重定向
,为了重定向,你必须在
servelet
jsp
过滤器
中编写重定向代码

这是解决方案。

Session\u Count
存储在
application scope
中,并通过这两种方式在每个网页上检查
Session\u Count
的值

a)使用
\*
url模式创建
过滤器
,并在其中添加重定向代码

b.)在每一个其他网页的顶部包含一个
通用jsp
(该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
}
%>
  • 更新您的听众

    公共类SessionListener实现了HttpSessionListener{ 私有整数sessionCount

    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);
    }
    
    }

  • 使用
    /*
    创建一个过滤器,如下所示

        @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>
    

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

    嗨,vikrant,我不知道在xml文件中映射什么,请您帮助我。我有4个jsp文件索引、adduser、destroy和error。若会话计数达到限制,则转到错误页,直到一个会话终止。我已经创建了您发送给我的类文件redirectOnSessionCount.java。我不知道要在xml中更改什么,因为我不熟悉xml文件,请帮助我。谢谢。嗨,Vikrant,我的web.xml文件我只是自己编辑的。这是正确的吗??index.jsp sessionListener net.viralpatel.servlet.listener.sessionListener redirectOnSessionCount redirectOnSessionCount redirectOnSessionCount redirectOnSessionCount/*是的,看起来很好:),嘿,我在过滤器内部做了一些更改也更新了你的过滤器:)嗨,朋友,我使用过滤器和include jsp检查了这两种类型,但都不起作用,因为我设置了条件sessioncount>2,但它超过了2,但应该重定向到error.jsp知道我没有收到ant错误,但它超过了会话限制。我设置的会话限制不超过2,但它超过了会话限制2。如果超过,则表示它假定转到错误页右侧