深入体验JavaWeb开发内幕——关于JEE中的response、resquest应答、请求头的编码问题的解决

当我们通过一个表单验证来连接数据库时,最关键的就是要确保浏览器的resquest端和服务器的response端能够统一编码,这样请求和返回的信息才能够正确,否则就会出现乱码问题。

        如:

       

        当编码不统一时返回信息:

     

统一编码后:

   

当然这样的问题还会出现在resquest端,当我们的resquest和response端编码不统一时就会出现如上的编码错误。

如:通过浏览器提交请求,通过resquest的相关方法获取到请求值之后输出结果如图。

在服务端输出:

统一编码后输出:

好了现在先来看关于response应答时的乱码问题的处理:

例如通过以不同的方式输出统一个”上海“ 字样时会有不同的结果,借此我们来解决所遇到的乱码问题。

问题一:

MyServlet.java

package net.csdn.servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		testEncoding1(response);

	}
方法一:

 public void testEncoding1(ServletResponse response) throws IOException{
    	String content = "上海";
    	OutputStream os = response.getOutputStream();
    	//这里默认的写入到response相应头中的编码方式为GB2312这正好要与浏览器默认编码方式相同
    	//所以可以正常解析
    	os.write(content.getBytes());
    }

如:

但我们如果指定response的输入编码集时:

方法二:

 public void testEncoding2(ServletResponse response) throws IOException{
    	String content = "上海";
    	OutputStream os = response.getOutputStream();
    	//这里由于指定了的写入到response相应头中的编码方式为utf-8而浏览器默认编码方却仍是GB2312
    	//所以解析出现乱码
    	os.write(content.getBytes("utf-8"));
 }

解决方式:

  public void testEncoding3(ServletResponse response) throws IOException{
    	String content = "上海";
    	
    	OutputStream os = response.getOutputStream();
    	//这里由于指定了的写入到response相应头中的编码方式为utf-8而浏览器默认编码方却仍是GB2312
    	//所以解析出现乱码
    	os.write(content.getBytes("utf-8"));
    	//解决方法:为响应信息设置响应头告诉浏览器以utf-8的方式来解析编码或者在浏览器一端将编码方式
    	//改为utf-8也可解决该问题
    	response.setContentType("text/html;charset = utf-8");
    }

方法三:

public void testEncoding4(HttpServletResponse response) throws IOException{
    	String content = "上海";
//    	response.setContentType("text/html;charset = utf-8");
    	
    	OutputStream os = response.getOutputStream();
    	//这里由于指定了的写入到response相应头中的编码方式为utf-8而浏览器默认编码方却仍是GB2312
    	//所以解析出现乱码
    	os.write(content.getBytes("utf-8"));
}

解决:

public void testEncoding4(HttpServletResponse response) throws IOException{
    	String content = "上海";
    	
    	OutputStream os = response.getOutputStream();
    	//这里由于指定了的写入到response相应头中的编码方式为utf-8而浏览器默认编码方却仍是GB2312
    	//所以解析出现乱码
    	os.write(content.getBytes("utf-8"));
    	//解决方法:为响应信息设置响应头告诉浏览器以utf-8的方式来解析编码或者在浏览器一端将编码方式
    	//改为utf-8也可解决该问题
//    	response.setContentType("text/html;charset = utf-8");
    	//以另一种方式设置响应头告知浏览器的解码方式
    	 response.setHeader("Content-Type", "text/html;charset = utf-8");
    }

可正确输出”上海“

方法四及解决:

 public void testEncoding5(ServletResponse response) throws IOException{
        String content = "上海";
    
        OutputStream os = response.getOutputStream();
    
        os.write(content.getBytes("utf-8"));
        //不是发送响应头而是向浏览器输出一段<meta />标签来告知浏览器按照相应的utf-8来解析
        os.write("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>".getBytes());
    }


方法五及解决:

public void testEncoding6(ServletResponse response) throws IOException{
    	String content = "上海";
    	 
    	//使用的默认编码集是iso8859-1而浏览器的编码却是utf-8故出现乱码问题
    	
    	PrintWriter pw = response.getWriter();
        pw.println(content);
       
    
    }
同样会出现乱码


解决:

public void testEncoding6(ServletResponse response) throws IOException{
    	String content = "上海";
    	 //解决方式同上:
        //设置响应头的编码方式
        response.setCharacterEncoding("UTF-8");
    	//通过响应头告诉浏览器正确的解码方式
        response.setContentType("text/html;charset = UTF-8");

    	//使用的默认编码集是iso8859-1而浏览器的编码却是utf-8故出现乱码问题
    	
    	PrintWriter pw = response.getWriter();
        pw.println(content);
       
    
    }

方法六及解决:

 public void testEncoding7(ServletResponse response) throws IOException{
    	int content = 14;
    	
    	//使用的默认编码集是iso8859-1而浏览器的编码却是utf-8故出现乱码问题
    	
    	PrintWriter pw = response.getWriter();
    	pw.write(content);
    	
    }

如图:

解决:

  public void testEncoding8(ServletResponse response) throws IOException{
        int content = 14;
        //解决方式同上:
        //设置响应头的编码方式
        response.setCharacterEncoding("UTF-8");
        //通过响应头告诉浏览器正确的解码方式
        response.setContentType("text/html;charset = UTF-8");
        
        //使用的默认编码集是iso8859-1而浏览器的编码却是utf-8故出现乱码问题
        PrintWriter pw = response.getWriter();
        pw.println(content);

        
    }

关于request:

问题:

UserServlet.java

package com.user.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.userbean.User;
import com.userdao.UserDao;
import com.userimpl.UserImpl;

public class UserServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		System.out.println(password);
		UserDao ud = new UserImpl();
		boolean b = ud.findUser(username, password);
		if(b == false){
			response.setContentType("text/html;charset=UTF-8 ");
			response.getWriter().write("用户名或密码有误,请重新确认输入!");
		}else{
			response.setContentType("text/html;charset=UTF-8 ");
			response.getWriter().write("登陆成功!");
		}

		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		    doGet(request, response);
	}
}
如图:

解决后:

package com.user.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.userbean.User;
import com.userdao.UserDao;
import com.userimpl.UserImpl;

public class UserServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
          //为request指定编码
             request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		System.out.println(password);
		UserDao ud = new UserImpl();
		boolean b = ud.findUser(username, password);
		if(b == false){
			response.setContentType("text/html;charset=UTF-8 ");
			response.getWriter().write("用户名或密码有误,请重新确认输入!");
		}else{
			response.setContentType("text/html;charset=UTF-8 ");
			response.getWriter().write("登陆成功!");
		}

		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		    doGet(request, response);
	}
}

好了,现在你已经知道了如何去处理resquest和response的相关编码问题了,赶快自己试一试吧!



  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值