[Solved] Servlet context is not working


Answer

Hashtable.contains() Tests if some key maps into the specified value in this hashtable.

So, within your Servlet at this line

if(!playerList.contains(playerid)) {

you’re actually comparing a key (playerid) with all the values (Player objects) in your Hashtable. Hence, the match is failing every time.

Your ServletContext (as well as its listener) works fine since you’re receiving your Hashtable (playerList) on every Servlet call correctly. So, to fix the problem use Hashtable.containsKey() which tests if an object mapping already exists for the specified key.

Other observations

  • Its kind of a misnomer to call a Hashtable a list. Use playerMap as an identifier since the table maps a playerid kay to a Player value object.
  • There’s no need to both annotate the listener (with @WebListener) as well as configure it declaratively in web.xml (with <listener>). Use only one of the two approaches.
  • Unlike HttpSession.setMaxInactiveInterval() (which sets timeout in secs) <session-timeout> sets the timeout interval in mins. So, if you wanted a default session length of 3 mins change 3000 to 3.

solved Servlet context is not working