package ResponseDemo;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo2 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String data="中国 ";
//response的Writer输出数据的问题输出数据的问题
//该句代码等价于其后面两句
response.setContentType("text/html;charset=GB2312");
//response.setCharacterEncoding("GB2312");
//response.setHeader("Content-type", "text/html;charset=ISO-8859-1");
PrintWriter out=response.getWriter();
out.write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
web.xml的配置信息:
<servlet>
<servlet-name>Demo2</servlet-name>
<servlet-class>ResponseDemo.Demo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo2</servlet-name>
<url-pattern>/servlet/Demo2</url-pattern>
</servlet-mapping>