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 ...
随机推荐
- php 获取远程图片长宽和大小
/***获取远程图片的宽高和体积大小 ** @param string $url 远程图片的链接 * @param string $type 获取远程图片资源的方式, 默认为 curl 可选 frea ...
- GIT 如何合并另一个远程Git仓库的文件到本地仓库里某个指定子文件夹并不丢失远程提交记录?
问题背景: 最近在重新整理手中的一个项目,目前该项目分为PC项目,手机项目,某第三方接口项目,第三方接口服务项目和手机项目 因为之前规划的原因,原来的四个项目是分两个解决方案来管理的 ...
- GPUImage API文档之GPUImageFramebufferCache类
GPUImageFramebufferCache类负责管理GPUImageFramebuffer对象,是一个GPUImageFramebuffer对象的缓存. 方法 - (GPUImageFrameb ...
- [Canvas]更多的球
欲观看动态效果请点此下载代码并用Chrome或者Firefox打开. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> ...
- TinyCore Nginx server with php-cgi and vsftpd
http://blog.matchgenius.com/tinycore-nginx-server-with-php-cgi-and-vsftpd/ Create fast testing serve ...
- Android缓存处理
Android缓存: 採用缓存,能够进一步大大缓解数据交互的压力,又能提供一定的离线浏览.下边我简略列举一下缓存管理的适用环境: 1. 提供网络服务的应用 2. 数据更新不须要实时更新.哪怕是3-5分 ...
- Ubuntu/Centos 系统上安装与配置Apache
一.在线安装: Ubuntu:sudo apt-get install apache2 Centos: sudo yum install apache2 二.安装后的位置: 1.服务地址:/etc/i ...
- java zxing生成二维码
package zxing.test; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; i ...
- Word转PDF非常好用的软件——pdfFactory Pro
pfdFactory Pro把word转为pdf的操作步骤: 1.打开将要转换的word的文档: 2.文件--->打印: 弹出如下对话框: 单击确定后弹出:
- MobX快速入门教程(重要概念讲解)
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7372119.html 一:Mobx工作流程图 二:MobX涉及到的概念 1:状态state 组件中的数据. 2 ...