日期:2014-05-16  浏览次数:20361 次

JSP乱码问题小整理
一、编写说明
最近一个星期在开发一个小型购物商城网站(源自课程设计),从项目规模大小、时间效率、个人基础以及学习成本等综合考虑采用jsp+servlet+mybatis+mysql架构方式。开发的第三天遇到了jsp乱码问题,这个问题纠结了半天多,过程中积累了不少经验。
二、下载链接
1、mysql下载地址:http://dev.mysql.com/downloads/
2、tomcat下载地址:http://tomcat.apache.org/download-60.cgi
3、mybatis(java版本)下载地址:http://www.mybatis.org/java.html
4、mysql访问软件navicat lite下载:http://www.onlinedown.net/softdown/87700_2.htm
三、简单描述
  jsp页面、mysql数据库、IE浏览器、tomcat容器以及mybatis映射文件等的编码方式都设置为UTF-8形式了。从jsp的form表单提交中文字段保存到数据库后出现了乱码问题。产生疑惑为:统一了编码方式为什么还会出现乱码问题呢。
四、相关内容如下 
备注说明:// 注释是为了整理问题写上去的,实际代码不同。

 1、jsp页面test.jsp
	
	// jsp保存格式吗,设置为utf-8格式
	<%@ page language="java" pageEncoding="utf-8"%>

	// jsp解码格式,设置为utf-8,不设置也没问题吗
	// 百度网上资料说,如果不设置此处,页面的中文显示有问题,试了把好像并没问题呢。何解,待研究。
	<%@ page contentType="text/html;charset=utf-8"%>

	<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>

	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	  <head>
	    <base href="<%=basePath%>">
	    <title>jsp乱码问题</title>
	    
	    // 提示浏览器采用utf-8编码格式处理
	    <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
	  </head>
	    <body>
		//提交到TestServlet.java类处理表单
		<form action="testServlet" method="post">
		 <table>
		   <tr>
		     <td>
			 用户昵称*
		     </td>
		     <td>
			 <input type="text" name="username" />
		     </td>
		   </tr>

		   <tr>
		     <td>
			  
		     </td>
		     <td>
			 <input type="submit" value="提交测试"/>
		     </td>
		   </tr>
		 </table>
		</form>
	    </body>
	</html>
 
2、tomcat下web.xml配置如下
  <?xml version="1.0" encoding="UTF-8"?>
  <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<servlet>
		<servlet-name>testServlet</servlet-name>
		<servlet-class>com.test.TestServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>testServlet</servlet-name>
		<url-pattern>/testServlet</url-pattern>
	</servlet-mapping>

  </web-app>
  3、TestServlet.java如下
  // 在servlet层出现了乱码问题
package com.test;

import java.io.IOException;

import javax.servlet.ServletException;

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

public class TestServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 处理get提交方式
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 处理post提交方式
		String username = request.getParameter("username");
		// servlet层测试乱码
		System.out.println("servlet = "+username); // 显示成了乱码
	}
}

4、初步判定
    通过的servlet-web层打印出字符串就显示成了乱码格式、那么再通过dao层插入到数据库后肯定是乱码的,问题定位在jsp页面到servlet代码之间,就要考虑jsp、IE、tomcat容器的编码方式了。前面已经说明了,全部设置成了utf-8格式了,所以进一步查看了网上许多解决方案,解决方案比较多比较乱,针对我遇到的问题,我从中提取和学习了下面的一些知识内容。

5、提取内容
    一、表单post方式提交——中文字段出现乱码,