我正在创建一个 Web 应用程序,我正在该应用程序中创建 session 监听器。在该 session 监听器中,如果条件不起作用如何在达到特定 session 计数后将页面重定向到另一个页面。
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 中映射这个类文件。
最佳答案
你不能redirect
来自你的 Listener
,对于重定向,您必须在 servelet
中编写重定向代码, jsp
和 filter
.
这是解决方案。
店铺 Session_Count
里面application scope
并检查 Session_count
的值在每个网页上,通过这两种方式。
a.) 创建 Filter
与 \*
url-pattern
并在其中添加重定向代码。
或
b.) 包括 common jsp
(这将包含重定向代码)在所有其他网页的顶部。
或者在每个网页中手动添加重定向代码。
更新你的监听器
公共(public)类 SessionListener 实现 HttpSessionListener { private Integer 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); }
使用
<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
}
%>
关于java - 我需要在特定 session 计数后重定向页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25786145/