servlet乱码 解决方法 2种方法
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种方法的更多相关文章
- yii2.0高级框架配置时打开init.bat秒退的解决方法 (两种方法)
第一种: 这几天刚接触到yii2.0框架,在配置advanced版本时运行init.bat初始化文件时老是闪退: 用cmd运行该文件时显示:The OpenSSL PHP extension is r ...
- servlet 乱码解决方法
一. servlet 发送的html 页面中文乱码 解决方法, 1.加入如下代码 response.setCharacterEncoding("UTF-8"); 2.在html页面 ...
- hive权威安装出现的不解错误!(完美解决)两种方法都可以
以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...
- Servlet乱码解决
后端收前端 1.post乱码 可以通过 request.setCharacterEncoding("utf-8"); 这样在后端可以接收中文 2.get乱码(手动解决) 可以通过 ...
- Cisco设备IOS的恢复方法 两种方法
如果不小心把Router或者Switch的IOS删除了,特别是Flash中的IOS和ROM中的Mini IOS都没有了的话,连启动都不行的话,有什么方法恢复它呢?答案是方法不只一种,而是两种.其实是我 ...
- 【转】这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [转]这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- thymeleaf Exception processing template "xxx": Exception parsing document: template="xxx", line 6 - column 3报错解决的几种方法
我是在SpringBoot项目使用Thymeleaf作为模板引擎时报的错误 controller代码非常简单,如下所示: @RequestMapping("/abc") publi ...
- “fatal error C1010”错误解决的三种方法
尝试写了一个简单的类文件,但在编译的时候提示错误,具体错误信息如下: fatal error C1010: unexpected end of file while looking for preco ...
随机推荐
- 深入理解Javascript window对象
首先看我们的源代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- ueditor插入自定义内容和样式
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点 通过UEditor提供的API接口可以很方便的读写操作内容并设置编辑器里的样式 页 ...
- Linux中盘符的两种挂载方法
相信接触过Linux系统的人对于mount命令都不陌生,今天是农历2017年的最后一个工作日,趁着时光还在,就说一下两种不同的挂载方法吧. 课前小知识: 命令格式:mount [-t vfstype] ...
- (转)最近一个项目中关于NGUI部分的总结(深度和drawCall)
在自己最近的一个项目中,软件的界面部分使用了NGUI来进行制作.在制作过程中,遇到了一些问题,也获取了一些经验,总结下来,作为日后的积累. 1.NGUI图集的使用. 此次是第一个自己正儿八经的制作完整 ...
- retrofit+RXjava二次封装
接入说明:项目中已集成RXjava,RXandroid.Retrofit,为避免包冲突,不须要再次接入. 就可以直接使用RXjava,Retrofit的所有api. github地址:https:// ...
- 如何用简单例子讲解 Q - learning 的具体过程?
作者:牛阿链接:https://www.zhihu.com/question/26408259/answer/123230350来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Install SVN (Subversion) Server on Fedora 20/19, CentOS/Red Hat (RHEL) 6.5/5.10
Install SVN (Subversion) Server on Fedora 20/19, CentOS/Red Hat (RHEL) 6.5/5.10 Updated by JR on Mar ...
- iOS开发技巧 - 使用UISlider来调整值的范围
(Swift) import UIKit class ViewController: UIViewController { var slider: UISlider! func sliderValue ...
- vSphere Data Protection – a new backup product included with vSphere 5.1
August 27, 2012 By Vladan SEGET This new backup product replaces VMware Data Recovery, which has bee ...
- 使用com.jayway.jsonpath.JsonPath包进行JSON的快速解析、设置值需要注意的性能提升方法
一.包地址 1.Maven:http://mvnrepository.com/artifact/com.jayway.jsonpath/json-path <!-- https://mvnrep ...