request对象和response对象(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-05-10 12:18 点击:次
p>
p> //以字节流用utf-8编码向客户端输出中文数据
p> private void test4(HttpServletResponse response) throws IOException{
p> String s = "你好!四";
p> response.setContentType("text/html;charset=UTF-8");
p> ServletOutputStream out = response.getOutputStream();
p> out.write(s.getBytes("UTF-8"));
p> }
p>
p> public void doPost(HttpServletRequest request, HttpServletResponse response)
p> throws ServletException, IOException {
p> doGet(request,response);
p> }
p>}
p>复制代码
p>分析:出现乱码的图解
p>
p>
p>
p>
p>
p>解决办法:
p>
p>1)通过更改浏览器的编码方式:IE/”查看”/”编码”/”UTF-8”(不可取)
p>
p>2)通过设置响应头告知客户端编码方式:response.setHeader(“Content-type”, “text/html;charset=UTF-8”);//告知浏览器数据类型及编码
p>
p>3)通过meta标签模拟请求头:out.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />".getBytes());
p>
p>4)通过以下方法:response.setContentType("text/html;charset=UTF-8");
p>
p>2.向客户端输出中文数据(字符)
p>
p>复制代码
p>package cn.lsl.response;
p>
p>import java.io.IOException;
p>import java.io.PrintWriter;
p>import javax.servlet.ServletException;
p>import javax.servlet.http.HttpServlet;
p>import javax.servlet.http.HttpServletRequest;
p>import javax.servlet.http.HttpServletResponse;
p>
p>public class ResponseDemo2 extends HttpServlet {
p>
p> public void doGet(HttpServletRequest request, HttpServletResponse response)
p> throws ServletException, IOException {
p> test3(response);
p> }
p>
p> //字符流输出中文数据
p> private void test1(HttpServletResponse response) throws IOException{
p> String s = "你好!一";
p> response.setCharacterEncoding("UTF-8"); //设置查的UTF-8(对内容进行编码,查utf-8)
p> response.setHeader("Content-Type", "text/html;charset=UTF-8");//通知浏览器以utf-8打开
p> PrintWriter out = response.getWriter();
p> out.write(s);//默认查的是ISO-8859-1码表
p> }
p> //字符流输出中文数据
p> private void test2(HttpServletResponse response) throws IOException{
p> String s = "你好!二";
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>