日期:2014-05-17  浏览次数:20705 次

新手问个servlet小问题 求高手指教
代码 :
package com.login.servlet;
import javax.servlet.http.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.io.*;
public class HelloServlet extends HttpServlet {
  public void init ( ServletConfig config ) throws ServletException{
super.init(config);
  }
  public void doGet(HttpServletRequest req, 
  HttpServletResponse resp)
  throws ServletException, IOException {
resp.setContentType("text/html;charset=GB2312");
System.out.println("<html>");
System.out.println("<head><title>CN2=HelloServlet</title><head>");
System.out.println("<body>");
System.out.println("hello XXXXX <br>");
System.out.println("大家好");
System.out.println("</body>");
System.out.println("</html>");
System.out.close();
  }
  public String getServletInfo(){
return "com.login.servlet Information";
  }
  public void destory(){}
}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>HelloServlet</servlet-name>
  <servlet-class>com.login.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>HelloServlet</servlet-name>
  <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
</web-app>


浏览器打开窗口:http://localhost:8080\Hello_Web\HelloServlet

页面为什么是空白的。但Console下输出这个:
<html>
<head><title>CN2=HelloServlet</title><head>
<body>
hello XXXXX <br>
大家好
</body>
</html>


------解决方案--------------------
输出到网页用 PrintWrite 而不是用system.out