-4

请我尝试将播放器对象存储在 HashTable 中,然后将其存储在 ServletContext 中。当类被调用时,它应该检查播放器(对象)是否已经在存储在 Servlet 上下文中的 playerList(HashTable)中。但是每次我调用该类时,它都会显示 player(object) 不在 playerList (HashTable) 中,因此它会创建一个新的 Player 对象。我试过包括

this.getServletContext().setAttribute("playerList",playerList);
this.getServletConfig().getServletContext().setAttribute("playerList",playerList);

但它仍然无法正常工作。

public class InitGameServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, JSONException {
        response.setContentType("text/plain;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            ServletContext context = getServletContext();
            Hashtable playerList = (Hashtable)context.getAttribute("playerList");
            String playerid=request.getParameter("id");
            HttpSession session=request.getSession(true);
            Player player=null;

            if(!playerList.contains(playerid)) {
                player=new Player(playerid,50);
                playerList.put(playerid, player);                           
                System.out.println("This player wasnt there before so i have put it");
            }
            else {
                player=(Player)playerList.get(playerid);
                System.out.println("This player was there so i have retreived it");
            }
        }
    }
}

@WebListener
public class InitializeGameContext implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        Hashtable<String, Game> gameList = new Hashtable();
        context.setAttribute("gameList", gameList);
        context.log("The game list has beeen loaded...............");
        Hashtable<String, Player> playerList = new Hashtable();
        context.setAttribute("playerList", playerList);
        context.log("The playerList list has beeen loaded..................");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

}

在我的 web.xml 我有

<listener>
    <listener-class>web.InitializeGameContext</listener-class>
</listener>

编辑:我完整的 web.xml

<servlet>
    <servlet-name>InitGameServlet</servlet-name>
    <servlet-class>com.whot.servlet.InitGameServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>InitGameServlet</servlet-name>
    <url-pattern>/InitGameServlet</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        3000
    </session-timeout>
</session-config>
<listener>
    <listener-class>web.InitializeGameContext</listener-class>
</listener>
4

1 回答 1

0

回答

Hashtable.contains()测试某些键是否映射到此哈希表中的指定

因此,在您的 Servlet 中的这一行

if(!playerList.contains(playerid)) {

您实际上是在将(playerid)与哈希表中的所有Player对象)进行比较。因此,比赛每次都失败。

您的ServletContext(以及它的侦听器)工作正常,因为您在每个 Servlet 调用上都正确接收了您的 Hashtable (playerList)。因此,要解决此问题,请使用Hashtable.containsKey()which 测试指定键的对象映射是否已存在。

其他意见

  • 将 Hashtable 称为列表有点用词不当。playerMap用作标识符,因为表将 kay 映射到playeridPlayer对象。

  • 无需同时注释侦听器(with @WebListener)以及在web.xml(with <listener>)中以声明方式配置它。仅使用两种方法中的一种。

  • HttpSession.setMaxInactiveInterval()(以秒为单位设置超时)不同,<session-timeout>以分钟为单位设置超时间隔。因此,如果您希望将 3 分钟的默认会话长度更改30003.
于 2013-05-14T05:48:24.800 回答