public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
test1(resp);
}
  
  //方法1:
public void test1(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {
resp.setHeader("Content-type", "text/html;charset=utf-8");
String data = "中国";
OutputStream output = resp.getOutputStream();
//程序以什么编码输出,那么一定要设置浏览为相对应的编码打开.
output.write(data.getBytes("utf-8"));
}
  //方法2:
//模拟meta标签,设置charset为utf-8,这个方法也行. //用html技术中的meta标签模拟了一个http响应头,来控制浏览器的行为
public void test2(HttpServletResponse response) throws Exception{
String data = "中国_第二个";
OutputStream output= response.getOutputStream();
output.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());
output.write(data.getBytes());
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

//********************************情况2:*************************************

public class ResponseDemo2 extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
test1(response); } public void test1(HttpServletResponse response) throws IOException {
// 方法1:
// 要设置response,所使用的码表,以控制reponse以什么码表向浏览器写入数据
// response.setCharacterEncoding("utf-8");
// 同时设置浏览器以何种码表打开,指定浏览以什么 码表打开服务器发送的数据 
// response.setHeader("content-type", "text/html;charset=utf-8");
//  方法2:
response.setContentType("text/html;charset=utf-8"); String data = "中国";
PrintWriter outputStream = response.getWriter();
outputStream.write(data);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response); } }

servlet乱码 解决方法 2种方法的更多相关文章

  1. yii2.0高级框架配置时打开init.bat秒退的解决方法 (两种方法)

    第一种: 这几天刚接触到yii2.0框架,在配置advanced版本时运行init.bat初始化文件时老是闪退: 用cmd运行该文件时显示:The OpenSSL PHP extension is r ...

  2. servlet 乱码解决方法

    一. servlet 发送的html 页面中文乱码 解决方法, 1.加入如下代码 response.setCharacterEncoding("UTF-8"); 2.在html页面 ...

  3. hive权威安装出现的不解错误!(完美解决)两种方法都可以

    以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...

  4. Servlet乱码解决

    后端收前端 1.post乱码 可以通过 request.setCharacterEncoding("utf-8");  这样在后端可以接收中文 2.get乱码(手动解决) 可以通过 ...

  5. Cisco设备IOS的恢复方法 两种方法

    如果不小心把Router或者Switch的IOS删除了,特别是Flash中的IOS和ROM中的Mini IOS都没有了的话,连启动都不行的话,有什么方法恢复它呢?答案是方法不只一种,而是两种.其实是我 ...

  6. 【转】这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. [转]这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. thymeleaf Exception processing template "xxx": Exception parsing document: template="xxx", line 6 - column 3报错解决的几种方法

    我是在SpringBoot项目使用Thymeleaf作为模板引擎时报的错误 controller代码非常简单,如下所示: @RequestMapping("/abc") publi ...

  9. “fatal error C1010”错误解决的三种方法

    尝试写了一个简单的类文件,但在编译的时候提示错误,具体错误信息如下: fatal error C1010: unexpected end of file while looking for preco ...

随机推荐

  1. Android -- isInEditMode

    解释 Indicates whether this View is currently in edit mode. A View is usually in edit mode when displa ...

  2. 【Java】阿里巴巴Java开发手册(纪念版)

    下载地址:(最新纪念版 2017-11-30 V1.3.1) <阿里巴巴Java开发手册>是阿里巴巴集团技术团队的集体智慧结晶和经验总结,经历了多次大规模一线实战的检验及不断的完善,系统化 ...

  3. Word Embedding与Word2Vec

    http://blog.csdn.net/baimafujinji/article/details/77836142 一.数学上的“嵌入”(Embedding) Embed这个词,英文的释义为, fi ...

  4. Fixing the JavaScript typeof operator

    https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/javascript 类 ...

  5. LynxFly科研小四轴横空出世,开源,F4,WIFI --(转)

    首先是一大堆的感谢,太多人的帮助,感谢不完了----首先要说明,这个PCB工程的出现要感谢论坛上的台湾大哥 john800422 开源了自己的飞控板的工程文件,我这样的没啥基础的小弟们才能学会如何制板 ...

  6. SSH连接linux时,长时间不操作就断开的解决方案

    转自:http://www.cnblogs.com/jifeng/archive/2011/06/25/2090118.html 修改/etc/ssh/sshd_config文件,找到 ClientA ...

  7. C++实现一个web服务器, 弱智版服务器

    监听本地的8888端口, 当在浏览器中访问这个地址的时候, 返回一堆HTML数据, 这种方式返回的数据不稳定,不同浏览器解析不同, 因为我们没有定义返回文件类型: #include <stdli ...

  8. 如何设置iPhone的手机铃声?【来自星星的你】

    如果大家需要已经截取好的手机铃声,可以给我留言,写下邮箱号码. 谢谢. ---------------------------------------------------------------- ...

  9. LintCode: Number of Airplanes in the Sky

    C++ (1)把interval数组中的所有start和所有end放在同一个数组中,然后进行排序,遇到start就起飞一架飞机,遇到一架end就降落一架飞机,所以start有个+1属性,end有个-1 ...

  10. Using a Microsoft Account to Logon and Resulting Internet Communication in Windows 8

    Using a Microsoft Account to Logon and Resulting Internet Communication in Windows 8 此主题尚未评级 - 评价此主题 ...