用jquery的post方法请求后台数据的中文乱码问题
今天写了一个post请求后台数据的方法,所有页面都是用utf-8编码的,主要页面大体是这样的:
Ajax请求页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8" isELIgnored="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>个人主页</title> <link rel="stylesheet" type="text/css" href="css/index.css"/> <script type="text/javascript" src="js/jquery-1.9.0.js"></script> <script type="text/javascript"> $(document).ready(function(){
$(".leftPrivyMenu li").click(function(){ var btnInfo=$(this).attr("url"); $.post(btnInfo,null,function(res){ $("#ajaxDiv").html(res); });
}); }); </script> </head> <body> <div> <div class="w topdiv"> <a href="login.jsp">登录</a> | <a href="register.jsp">注册</a> | 2014年3月27日 80:20 星期四 | <a href="#">注销</a></div><!--topdiv end--> <div class="w bannerdiv">欢迎访问校友录管理系统</div> <div class="w neckdiv"><a href="index.jsp">首页</a> | <a href="postsPage.jsp">帖子动态</a> | <a href="academy.jsp">班级信息</a> | <a href="privypage.jsp">个人主页</a> | <a href="classesManage.jsp">班级管理</a> | <a href="contactUs.jsp">联系我们</a> | </div> <div class="currentPage w"><span>当前位置》个人主页 </span></div> <div class="w maindiv"> <div> <div class="privyInfo"> <div class="leftPrivyMenu"> <ul> <li url="web/privyInfo.jsp">个人信息</li> <li url="web/editHead.jsp">编辑头像</li> <li url="web/classInfo.jsp">所在班级</li> <li url="SchoolmateServlet">我的校友录</li> <li url="web/classPhoto.jsp">班级相册</li> <li url="web/becomemanager.jsp">申请成为班级管理员</li> </ul> </div> <div class="privyMainInfo" id="ajaxDiv"> <!--此处为局部刷新的div块--> </div> </div>
</div> </div><!--maindiv end-->
</div> </body> </html>
|
请求的servlet代码如下:
package ynni.servlet;
import java.io.IOException; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.List;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import ynni.dao.UsersDao; import ynni.daoimpl.UsersDaoImpl; import ynni.entity.Users;
import net.sf.json.JSONArray; /* * 实现校友录的业务逻辑servlet * */ public class SchoolmateServlet extends HttpServlet { List<Users> list = new ArrayList<Users>();//要返回的校友信息 UsersDao userDao = new UsersDaoImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置编码 request.setCharacterEncoding("utf-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); list = userDao.getUserList(); /*JSONArray jsonArray2 = JSONArray.fromObject( list ); out.print(jsonArray2);*/ request.setAttribute("userList", list); request.getRequestDispatcher("/web/schoolmates.jsp").forward(request, response); }
} |
Web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Schoolmate</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>SchoolmateServlet</display-name> <servlet-name>SchoolmateServlet</servlet-name> <servlet-class>ynni.servlet.SchoolmateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SchoolmateServlet</servlet-name> <url-pattern>/SchoolmateServlet</url-pattern> </servlet-mapping> </web-app> |
请求的显示页面如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>我的校友录</title> <link rel="stylesheet" type="text/css" href="css/index.css"/> </head> <body> <div class="schoolmatesdiv"> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <th>校友名称</th> <th>校友学号</th> <th>校友qq号</th> <th>微信账号</th> <th>通信电话</th> <th>现居地址</th> <th>邮政编码</th> <th>Email</th> </tr> <c:forEach items="${userList }" varStatus="i" var="it"> <tr> <td>${it.userId }</td> <td>${it.userstuNo }</td> <td>${it.userQq }</td> <td>${it.userWeixin }</td> <td>${it.userPhone }</td> <td>${it.userAddr }</td> <td>${it.userZip }</td> <td>${it.userEmail }</td> </tr> </c:forEach> </table> <div class="checkPage"><a href="#">上一页</a> 7/16 <a href="#">下一页</a></div> </div> </body> </html>
|
页面样子大概如下:
Tomcat中Server.xml文件中的设置编码如下:
<ConnectorURIEncoding="utf-8"connectionTimeout="20000" port="8080"protocol="HTTP/1.1"redirectPort="8443" useBodyEncodingForURI="false"/> |
看起来我的页面上该用“utf-8”的地方都用了“utf-8”了,似乎是没有错的,我在网上看了多种方法,什么过滤器啊的,但是发现都不行,
其实今天我也是小白了,突然发现自己犯了一个最二的错误,原来是忘记了在servlet中设置response响应对象的编码方式,我将servlet中的代码修改如下:
package ynni.servlet;
import java.io.IOException; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.List;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import ynni.dao.UsersDao; import ynni.daoimpl.UsersDaoImpl; import ynni.entity.Users;
import net.sf.json.JSONArray; /* * 实现校友录的业务逻辑servlet * */ public class SchoolmateServlet extends HttpServlet { List<Users> list = new ArrayList<Users>();//要返回的校友信息 UsersDao userDao = new UsersDaoImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置编码 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); list = userDao.getUserList(); /*JSONArray jsonArray2 = JSONArray.fromObject( list ); out.print(jsonArray2);*/ request.setAttribute("userList", list); request.getRequestDispatcher("/web/schoolmates.jsp").forward(request, response); }
} |
多加了这么一行,发现自己的错误没了,看来以后写代码真的得注意啊;
这种二的问题,我想一般人不会犯把,但调试了半天,还是想把它贴出来,希望能帮到需要的人;呵呵