使用XMLHttprequest从servlet接收单行文本

时间:2013-12-31 10:02:37

标签: ajax jsp servlets

我正在尝试从JSP向servlet发送XMLHttp请求,并在从服务器收到的alert()中显示消息。但我基本上收到带有一些html标签的<script> <script/>部分。

这是我的jsp代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org    /TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>

 <script>
    var xmlHttp;

    function createXMLHttpRequest() {
      if (window.ActiveXObject) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
      }
    }
   function startRequest() {
   createXMLHttpRequest();
   xmlHttp.onreadystatechange = handleStateChange;
   xmlHttp.open("post", "testServlet", true);
   // var msg="Hello world";
   xmlHttp.send(null);
  }
   function handleStateChange(){
    if(xmlHttp.readyState == 4){
      if(xmlHttp.status == 200){
        alert("The server replied with: " +xmlHttp.responseText);
      }
    }
   }
 </script>
 </head>
 <body>
<form action="#">
<input type="button" value="Press ME!!"
 onclick="startRequest();"/>
</body>
</html>

这是我的servlet代码:

 package pk1;

 import java.io.IOException;
 import java.io.PrintWriter;

 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

  @WebServlet("/testServlet")
  public class testServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    public testServlet() {
     super();
    }


protected void doGet( HttpServletRequest request,                            

     HttpServletResponse response)throws ServletException, IOException {

    test(response);
 }


protected void doPost(HttpServletRequest request,   

    HttpServletResponse response) throws ServletException, IOException {

    test(response);
}

public void test(HttpServletResponse response) throws IOException{
    response.setContentType("text/plain");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setCharacterEncoding("UTF-8");

     String data = "This is a response from testServlet";
    PrintWriter out=response.getWriter();
    out.write(data);

    out.flush();
    out.close();

    }

}

由于我使用Tomcat7,我想我不需要指定URL映射?那我在这里做错了什么?请让我知道。

1 个答案:

答案 0 :(得分:2)

添加此行

function startRequest() {
   createXMLHttpRequest();
   xmlHttp.onreadystatechange = handleStateChange;
   xmlHttp.open("post", "testServlet", true);
   xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');   // add here 
   xmlHttp.send(null);

并且在你的servlet中首先尝试使用doPost,

protected void doPost(HttpServletRequest request,   

    HttpServletResponse response) throws ServletException, IOException {    
     PrintWriter out = response.getWriter();    
     out.print("Hi ");    
}

希望它有所帮助!通知是否有效