Servlet上下文不起作用

时间:2013-05-13 18:00:18

标签: java servlets servletconfig servletcontextlistener

请问我正在尝试将一个玩家对象存储在HashTable中,然后将其存储在ServletContext中。当调用类时,应该检查播放器(对象)是否已经存在于Servlet上下文中的playerList(HashTable)中。但每次我调用类时它都会显示播放器(对象)不在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>

1 个答案:

答案 0 :(得分:0)

答案

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

所以,在这行的Servlet中

if(!playerList.contains(playerid)) {

您实际上是将 (playerid)与所有 进行比较({{1}你的Hashtable中的对象)。因此,每次比赛失败。

你的Player(以及它的监听器)工作正常,因为你正确地在每个Servlet调用上收到你的Hashtable(playerList)。因此, 解决问题 使用ServletContext测试指定键的对象映射是否已存在。

其他观察

  • 将Hashtable称为列表是一种误称。使用Hashtable.containsKey()作为标识符,因为该表将playerMap kay映射到playerid值对象。

  • 无需同时注释侦听器(使用Player),也无需在@WebListener(使用web.xml)中以声明方式对其进行配置。仅使用两种方法中的一种。

  • <listener>(以秒为单位设置超时)不同,HttpSession.setMaxInactiveInterval()以分钟为单位设置超时间隔。因此,如果您希望默认会话长度为3分钟,请将<session-timeout>更改为3000