html - JSP shows wrong (old) values -
following problem:
i using servlet controller, jsb model , java beans information. here code:
html/jsp:
<table summary="diese tabelle zeigt informationen zum aktuellen spiel"> <jsp:usebean id="game" class="model.spiel" scope="session"/> <tr><th id="leaderlabel" class="label">führender</th><td id="leader" class="data"><%= game.getleader() %></td></tr> <tr><th id="roundlabel" class="label">runde</th><td id="round" class="data"><%= game.getround() %></td></tr> <tr><th id="timelabel" class="label">zeit</th><td id="time" class="data"><%= game.gettime() %></td></tr> <tr><th id="computerscorelabel" class="label">würfelergebnis <em>super c</em></th><td id="computerscore" class="data"><%= game.getcomputerscore() %></td></tr> </table>
html servlet call:
<a id="dice" href="gameservlet?action=wurfeln" tabindex="4"> <img id="diceimage" src="img/wuerfel1.png" alt="würfel mit einer eins" /> </a>
servlet:
if(action.equals("wurfeln")){ // right game session spiel game; httpsession session = request.getsession(true); if(sessions.getgame(session)==null) { sessions.newsessions(session); game = sessions.getgame(session); } else { game = sessions.getgame(session); } // runt methods on game game.setspielerscore(); //spieler würfelt game.setcomputerscore(); //computer würfelt // load new jsb requestdispatcher dispatcher = getservletcontext().getrequestdispatcher("/table.jsp"); dispatcher.forward(request, response); }
java bean: (game)
package model; import java.util.random; public class spiel { private int runde; private string zeit; private int spieler_feld; private int spieler_wurfelergebnis; private string spieler_name; private int computer_feld; private int computer_wurfelergebnis; private string computer_name; private random x; public spiel() { this.runde = 1; this.zeit = "00:00"; this.spieler_feld = 1; this.computer_feld = 1; this.spieler_name = "super mario"; this.computer_name = "super c"; this.x = new random(); this.spieler_wurfelergebnis = 0; this.computer_wurfelergebnis = 0; } public void setround(int round) { this.runde = round; } public void settime(string time) { this.zeit = time; } public string gettime() { return this.zeit; } public int getround() { return this.runde; } public void newround() { this.runde += 1; } public string getleader() { if(this.spieler_feld > this.computer_feld) { return this.spieler_name; } else if(this.spieler_feld < this.computer_feld){ return this.computer_name; } else { return "beide"; } } public void setspielerscore() { //spieler würfelt //this.spieler_wurfelergebnis = this.x.nextint(3)+1; this.spieler_wurfelergebnis = 3; } public void setcomputerscore() { //computer würfelt //this.computer_wurfelergebnis = this.x.nextint(3)+1; this.computer_wurfelergebnis = 2; } public int getspielerscore() { return this.spieler_wurfelergebnis; } public int getcomputerscore() { return this.computer_wurfelergebnis; } }
java bean: (session)
package model; import java.util.arraylist; import java.util.hashmap; import java.util.list; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; public class sessions { hashmap<httpsession, spiel> sessions = new hashmap<httpsession, spiel>(); public void newsessions(httpsession s) { if(!sessions.containskey(s)) { spiel newgame = new spiel(); sessions.put(s, newgame); } } public spiel getgame(httpsession s) { spiel spiel = sessions.get(s); if (spiel != null) { return spiel; } else { return null; } } }
so problem experiencing new load of jsp in servlet (request dispatcher .... ) should show updated values in new jsp. since game.setspielerscore()
changes variables, continues show values set constructer call.
i have 3 possible ideas why happens:
1) wrong in jsp code, possible doesnt gather values of right object.
2) wrong session javabeans, doesnt give right game session or something.
3) doesn`t update variables of game object correctly
the game
attribute needs set in request.
if(action.equals("wurfeln")){ // right game session spiel game; httpsession session = request.getsession(true); if(sessions.getgame(session)==null) { sessions.newsessions(session); game = sessions.getgame(session); } else { game = sessions.getgame(session); } // runt methods on game game.setspielerscore(); //spieler würfelt game.setcomputerscore(); //computer würfelt request.setattribute("game", game);//set game in request. // load new jsb requestdispatcher dispatcher = getservletcontext().getrequestdispatcher("/table.jsp"); dispatcher.forward(request, response); }
jsp
<jsp:usebean id="game" class="model.spiel" scope="request"/>
Comments
Post a Comment