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如下
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); // 显示成了乱码}} (1)转码方式 String username = request.getParameter("username");// 进行转码操作处理username = new String(username.getBytes("iso-8859-1"),"utf-8");// servlet层测试乱码System.out.println("servlet = "+username); (2)设置编码格式 // 在请求的开始设置为utf-8格式 request.setCharacterEncoding("utf-8"); (3)过滤器方式web.xml配置文件中添加上filter配置<filter><filter-name>JspEncodingFilter</filter-name><filter-class>com.test.JspEncodingFilter</filter-class><init-param><param-name>jspEncoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>JspEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>对应的类如下:package com.test;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class JspEncodingFilter implements Filter {private String jspEncoding = null;public void destroy() {}public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {arg0.setCharacterEncoding(jspEncoding); // 设置编码格式arg1.setContentType("text/html;charset="+jspEncoding);arg2.doFilter(arg0, arg1);}public void init(FilterConfig arg0) throws ServletException {this.jspEncoding = arg0.getInitParameter("jspEncoding");}}// 其他乱码问题待以后遇到再研究学习。// tomcat下get方式提交解决方案等等。