每次进行ajax调用时都会加载JSP

时间:2015-12-25 07:48:15

标签: java ajax jsp

我正在创建Hang man,其中出现的文本字段的数字等于要猜到的单词中的字符数。有一个单独的文本字段,其中用户键入一个字符,然后通过单击按钮上的ajax调用进入servlet。这是我的jsp代码

<body>
        <%        //Input streams for file read
        File dictionary= new File("D:\\FatimaArshad05241/Dictionary.txt");
        FileReader fr= new FileReader(dictionary);
        LineNumberReader ln= new LineNumberReader(fr);
       RandomAccessFile raf = new RandomAccessFile(dictionary, "r");
        //counter to count number of lines
        int counter=0;
        //count number of lines
        while(ln.readLine()!=null){
        counter++;
        }
        //generate a random line number
       Random rnmber = new Random();
       int line=1+rnmber.nextInt(counter);
       String word="";
       int c=1;
       //position pointer to the top of file
       word= raf.readLine();
        raf.seek(0);
       //fetch the word
      while(raf!=null){

        if(c==line){
             //System.out.println("oh here");
             break;

        }
        else{

          word=  raf.readLine();

            }

         c++;

        }
      char[] array= new char[200];
      System.out.println("The word is");
      System.out.println(word);
      counter=0;
      for(int i=0; i<word.length();i++){
        array[i]= word.charAt(i);
        counter++;
      }
      %>
        <% for(int i=0; i<counter;i++){%>
            <input type="text" name="<%=i%>" id="<%=i%>"/>
            <%}%>
        <form>

                <br/>   
                 <br/> 
                  <br/> 
           <input type="text" name="t" id="t"/>
           <input type="button" name="a" id="a" value="enter"/>


       </form>


        <script>
            $("#a").click(
            function(e){
                var wordguessed= $("#t").val();
                $.GET("HangMan.java",{word:<%=word%>,wordguessed=wordguessed}, function(data,value){

                <%System.out.println("so u did it :p");%>

                alert(data);

            });

            }        
            );

        </script>


    </body>

这个问题是ajax成功,当按下按钮时,重新加载jsp页面并给出新单词。 servlet端的语句根本不执行。这是我的Servlet代码

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println("So the word guessed khhftuuuuuuuuuuygyhhgis");
           String word= request.getParameter("word");
           String wordguessed= request.getParameter("wordguessed");
           System.out.println("So the word guessed is"+wordguessed);

        }

我不希望再次重新加载JSP。我有什么遗失的东西吗?

0 个答案:

没有答案