当我们通过一个表单验证来连接数据库时,最关键的就是要确保浏览器的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的相关编码问题了,赶快自己试一试吧!