2015-04-23 44 views
0

当前正在创建一个画布Javascript游戏,并将本地存储变量传递给我的数据库,即Score。分数正在“已过”。当我调试时,我看到localstorage值在那里。但它似乎并没有存储到数据库中。最好不使用Ajax?将本地存储值传递给服务器(Java,JSP)

function drawElapsedTime(){ 
     var elapsed=parseInt((new Date() - startTime)/1000); 
     g.save(); 
     g.beginPath(); 
     g.fillStyle="#008000"; 
     g.font="14px Verdana" 
     // draw the running time at half opacity 
     g.globalAlpha=0.50; 
     g.fillText(elapsed+" secs",canvas.width-75,25); 
     localStorage.setItem("score",elapsed); 
     g.restore(); 

    } 
var storedTime = localStorage.getItem("score"); 

<form action="UserActionServlet" method="post"> 


       <input type="submit" value="scoreObj" class="submit" /> 

    </form> 

命令:

String xscore = request.getParameter("score"); 

     int score = 0; 
     if (xscore != null) { 
      score = Integer.parseInt(xscore); 
     } 
System.out.println(xscore); 

回答

0

你是不是合格分数变量作为您的表单提交的参数。

<form action="UserActionServlet" method="post" onSubmit="submitForm(this.form)"> 
     <input type = "hidden" name = "score"/> 
     <input type="submit" value="scoreObj" class="submit" /> 
</form> 

<script> 
    function submitForm(f){ 
     f.score.value = storedTime ; 
     f.submit(); 
    } 
</script>