java - Servlet context is not working -
please trying store player object in hashtable stored in servletcontext. when class called supposed check if player (object) in playerlist (hashtable) stored in servlet context. each time call class shows player(object) isn't in playerlist (hashtable) creates new player object. have tried including
this.getservletcontext().setattribute("playerlist",playerlist); this.getservletconfig().getservletcontext().setattribute("playerlist",playerlist);
but still not working.
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 have put it"); } else { player=(player)playerlist.get(playerid); system.out.println("this player there 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) { } }
in web.xml have
<listener> <listener-class>web.initializegamecontext</listener-class> </listener>
edit: complete 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>
answer
hashtable.contains()
tests if key maps specified value in hashtable.
so, within servlet @ line
if(!playerlist.contains(playerid)) {
you're comparing key (playerid) values (player
objects) in hashtable. hence, match failing every time.
your servletcontext
(as listener) works fine since you're receiving hashtable (playerlist) on every servlet call correctly. so, to fix problem use hashtable.containskey()
tests if object mapping exists specified key.
other observations
- its kind of misnomer call hashtable list. use
playermap
identifier since table mapsplayerid
kayplayer
value object. - there's no need both annotate listener (with
@weblistener
) configure declaratively inweb.xml
(with<listener>
). use 1 of 2 approaches. - unlike
httpsession.setmaxinactiveinterval()
(which sets timeout in secs)<session-timeout>
sets timeout interval in mins. so, if wanted default session length of 3 mins change3000
3
.
Comments
Post a Comment