request对象和response对象
来源:未知 责任编辑:责任编辑 发表时间:2014-05-10 12:18 点击:次
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象
p>
p>HttpServletResponse
p>
p>HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。
p>
p>1.向客户端输出中文数据(字节)
p>
p>复制代码
p>package cn.lsl.response;
p>import java.io.IOException;
p>import javax.servlet.ServletException;
p>import javax.servlet.ServletOutputStream;
p>import javax.servlet.http.HttpServlet;
p>import javax.servlet.http.HttpServletRequest;
p>import javax.servlet.http.HttpServletResponse;
p>
p>public class ResponseDemo1 extends HttpServlet {
p>
p> public void doGet(HttpServletRequest request, HttpServletResponse response)
p> throws ServletException, IOException {
p> test5(response);
p> }
p>
p> //输出整数:最好转换成字符串输出
p> private void test5(HttpServletResponse response) throws IOException{
p> int i = 98;
p> response.getOutputStream().write((i+"").getBytes());
p> }
p>
p> //以字节流用默认编码向客户端输出中文数据:没有乱码
p> //默认的是GBK
p> private void test1(HttpServletResponse response) throws IOException{
p> String s = "你好!一";
p> ServletOutputStream out = response.getOutputStream();
p> out.write(s.getBytes());
p> }
p>
p> //以字节流用utf-8编码向客户端输出中文数据
p> //以UTF-8编码发送数据,浏览器(默认用GB2312)会出现乱码
p> private void test2(HttpServletResponse response) throws IOException{
p> String s = "你好!二";
p> //通知客户端查看UTF-8码表
p> response.setHeader("Content-Type", "text/html;charset=UTF-8");
p> ServletOutputStream out = response.getOutputStream();
p> out.write(s.getBytes("UTF-8"));
p> }
p>
p> //以字节流用utf-8编码向客户端输出中文数据
p> private void test3(HttpServletResponse response) throws IOException{
p> String s = "你好!三";
p> ServletOutputStream out = response.getOutputStream();
p> out.write("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>".getBytes("UTF-8"));
p> out.write(s.getBytes("UTF-8"));
p> }
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>