在TestAjax.java(位置:webapps/servlets/,已编译)中:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String data = "Hello World";
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(data);
}
在 HTML(JavaScript) 中:</p>
function showHelloWorld(inputForm) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = xhr.responseText;
alert(data);
}
}
xhr.open('GET', '../../examples/servlets/TestAjax.class', true);
xhr.send(null);
}
在 web.xml 中:
<servlet>
<servlet-name>TestAjax</servlet-name>
<display-name>TestAjax</display-name>
<servlet-class>TestAjax</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestAjax</servlet-name>
<url-pattern>/servlet/TestAjax</url-pattern>
</servlet-mapping>
症状:
执行 showHelloWorld() 时,警告框显示如下:
����2,
!()VCodeLineNumberTabledoGetR(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V Exceptions"# SourceFile TestAjax.java* Hello World * text/plain$%&UTF-8'&()*+&TestAjaxjavax/ servlet/http/HttpServletjava/io/IOExceptionjavax/servlet/ServletException&javax/servlet/http/HttpServletResponsesetContentType(Ljava/lang/String;)VsetCharacterEncoding getWriter()Ljava/io/PrintWriter;java/io/PrintWriterwrite!
*�� FN,�,�,�-��?@ABC
请注意其中有“Hello World”,但正确的响应应该只有“Hello World”,响应中不应该有所有这些废话。
我检查了代码和部署一百万次,仍然没有发现任何线索。
请对这个问题有所了解,我将非常感激!