您正在创建一个Web应用程序,在该应用程序中,我想限制访问用户的数量。例如:如果超过4个用户,只有4个用户可以访问该应用程序,它会显示超出限制的错误消息,直到4个用户中的任何一个用户注销。如果4个用户注销中的任何一个意味着计数将为3,那么其他一个用户就可以访问该应用程序。另一个最佳示例:如果有人给出一个减号,它将反映在每个人的页面中,在stackoverflow页面中的声誉。
索引页:
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<html>
<body>
<form method="get" action="PageHitCounter">
<input type="submit" value="Add User" />
</form>
</body>
<%
if(session.getAttribute("ht")!=null)
{
String Aht= session.getAttribute("ht").toString();
int hitCount=Integer.parseInt(Aht);
System.out.println("hit count in login"+hitCount);
session.setAttribute("hitCount",hitCount);
}
else {
int hitCount = 0;
session.setAttribute("hitCount",hitCount);
System.out.println("new session count"+hitCount);
}
%>
</html>
servlet页面:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.sql.Date;
//import java.sql.SQLException;
//import java.util.*;
//import java.sql.*;
public class PageHitCounter extends HttpServlet{
private int hitCount;
public void init()
{
// Reset hit counter.
hitCount = 0;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session = request.getSession();
// Set response content type
response.setContentType("text/html");
// This method executes whenever the servlet is hit
// increment hitCount
if(session.getAttribute("hitCount")!= null){
String Aht= session.getAttribute("hitCount").toString();
int hitCount=Integer.parseInt(Aht);
System.out.println(hitCount);
hitCount++;
System.out.println("after increment"+hitCount++);
if(hitCount <= 5)
{
System.out.println("if");
PrintWriter out = response.getWriter();
String title = "Total Number of Hits";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<form action='Logout.jsp'>"+
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h2 align=\"center\">" + hitCount + "</h2>\n" +
"<input type='submit' value='logout'/>"+
"</body></html>");
}
}
else
{
System.out.println("else");
hitCount++;
session.setAttribute("ht", hitCount);
if(hitCount <= 4)
{
System.out.println("aa");
PrintWriter out = response.getWriter();
String title = "Total Number of Hits";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<form action='Logout.jsp'>"+
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h2 align=\"center\">" + hitCount + "</h2>\n" +
"<input type='submit' value='logout'/>"+
"</body></html>");
}
}
/* else
{
destroy();
response.sendRedirect("Logout.jsp");
}*/
}
public void destroy()
{
// hitCount -= 1;
// This is optional step but if you like you
// can write hitCount value in your database.
}
}
退出页面:
<%
session.removeAttribute("hitCount");
//session.invalidate();
String ht1=session.getAttribute("ht").toString();
int ht2=Integer.parseInt(ht1);
System.out.println("hit count before"+ht1);
if(ht2 != 0)
{
ht2 -= 1;
System.out.println("hit count after"+ht2);
}
session.setAttribute("ht", ht2);
%>
<h2>Session Destroyed successfully.. </h2>
<a href="index1.jsp">Click here to go Back</a>
现在面临的问题是计数不会减少,因为计数将在会话中,但在注销页面中我需要使会话无效,所以我不会正确计算。所以请有人帮助我。
答案 0 :(得分:0)
因此,您希望将应用程序限制为最多4个会话。您应该将活动会话数保留在ServletContext
属性中。您可以从servlet(getServletContext()
)或JSP(${application.attrName}
)
您必须在登录时使用它,但不能依赖注销页面,因为如果用户忘记注销,会话将会过期。您必须设置会话侦听器才能正确维护会话计数。
会话监听器可能如下所示:
@WebListener
public class SessionCountListener implements HttpSessionListener, ServletContextListener {
private ServletContext ctx;
private int sessionCount = 0;
private static final String DEFAULT_ATTR = "sessionCount";
private String attr;
private static final String PARAM_NAME = "session_count.attr_name";
@Override
public void contextInitialized(ServletContextEvent sce) {
// store servletContext
ctx = sce.getServletContext();
// try to get attribute name in config
attr = ctx.getInitParameter(PARAM_NAME);
if ((attr == null) || attr.isEmpty()) {
attr = DEFAULT_ATTR;
}
sessionCount = 0;
ctx.setAttribute(attr, sessionCount);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ctx.removeAttribute(attr);
}
@Override
public void sessionCreated(HttpSessionEvent se) {
countUpdate(1);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
countUpdate(-1);
}
synchronized private void countUpdate(int delta) {
sessionCount += delta;
ctx.setAttribute(attr, sessionCount);
}
}
使用此侦听器,会话计数默认存储在属性名称sessionCount
下,但可以使用名称为session_count.attr_name
的上下文参数进行配置。并且对变量sessionCount
的所有访问都正确同步,因为多个请求可以同时创建或关闭会话。