网页回传字符变乱码

J2EE 码拜 9年前 (2015-09-30) 1092次浏览

<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn”%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<%
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>My JSP “”index.jsp”” starting page</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>    
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”This is my page”>
<!–
<link rel=”stylesheet” type=”text/css” href=”styles.css”>
–>
  </head>
  
  <body>
    商品详细信息
<table border=”1″ cellpadding=”0″ width=”400px” cellspacing=”0″ id=”save_text” align=”center”>
<tr>
<td >
商品名称:
</td>
<td>
${requestScope.goods.gname}
</td>
</tr>
<tr>
<td>
商品售价:
</td>
<td>
${requestScope.goods.gprice}
</td>
</tr>
<tr>
<td colspan=”2″>
<img src=”${pageContext.request.contextPath}/image/${fn:substring(requestScope.goods.gpic,fn:indexOf(requestScope.goods.gpic,””?””)+1,-1)}”/>
</td>
</tr>
<tr>
<td>
商品类型:
</td>
<td>
${requestScope.goods.category.ctype}
</td>
</tr>
<tr>
<td>
商品简单介绍:
</td>
<td>
${requestScope.goods.gremark}
</td>
</tr>
<tr>
<td>
商品详细介绍:
</td>
<td>
${requestScope.goods.gxremark}
</td>
</tr>
<tr>
<td colspan=”2″>
<c:url value=”sorderAction_addSorder.action” var=”addSorder”>
<c:param name=”sname” value=”${requestScope.goods.gname}” />
<c:param name=”sprice” value=”${requestScope.goods.gprice}” />
<c:param name=”snumber” value=”1″ />
<c:param name=”goods.gid” value=”${requestScope.goods.gid}” />
</c:url>
<a href=”${addSorder}”>购买</a>
</td>
</tr>
</table>
  </body>
</html>
————————————————————————————————
@SuppressWarnings(“serial”)
public class SorderAction extends BaseAction<Sorder> {

//添加购物项到购物车中
public String addSorder(){
Forder forder = (Forder) session.get(“forder”);
//添加购物项
forder = sorderService.addSorder(forder, model);
//添加购物总价
forder.setFtotal(forderService.cluFtotal(forder));

session.put(“forder”, forder);
return “showCar”;
}

public String removeSorder(){
int gid = model.getGoods().getGid();
Forder forder = (Forder) session.get(“forder”);
forder = sorderService.removeSorder(forder, gid);
forder.setFtotal(forderService.cluFtotal(forder));
session.put(“forder”, forder);
jsonMap.put(“ftotal”, forder.getFtotal());
return “json1″;
}
}

————————————————————————————————————————————–
showCar.jsp:
<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt” %>
<%
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%>”>
    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
    <title>My JSP “”index.jsp”” starting page</title>
<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>    
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”This is my page”>
<!–
<link rel=”stylesheet” type=”text/css” href=”styles.css”>
–>
  
<script type=”text/javascript” src=”${pageContext.request.contextPath}/js/jquery-1.8.2.js”></script>
<script type=”text/javascript”>
$(function(){
$(“.delBtn”).click(function(){
var gid=$(this).attr(“id”);
$.post(“/myshop/sorderAction_remoceSorder.action”,
{“goods.gid”:gid},
function(result){
$(“#ftotal”).text(result.ftotal);
}
);
$(this).parents(“tr:frist”).remove();
});
});
</script>

  </head>
  
  <body>
    <table>
     <tr>
     <td>
     商品名称
     </td>
<td>
     商品价格
     </td>
<td>
     商品数量
     </td>
<td>
     操作商品
     </td>
     </tr>
<c:forEach items=”${sessionScope.forder.sorders}” var=”sorder”>
<tr>
<td>
${sorder.sname}
</td>
<td>
${sorder.sprice}
</td>
<td>
<input type=”text” value=”${sorder.snumber}” size=”2″/>
</td>
<td>
<input type=”button” value=”删除” class=”delBtn” id=”${sorder.goods.gid}”>
</td>
</tr>
</c:forEach>
    </table>
<div id=”ftotal”>
${sessionScope.forder.ftotal}
</div
  </body>
</html>

在购物车中中文商品名称出现乱码,麻烦各路高手帮帮解决乱码,已经快弄哭了。

解决方案:70分
1、针对Post方式提交的请求如果出现乱码,可以每次在request解析数据时设置编码格式:

request.setCharacterEncoding(“utf-8”);
也可以使用编码过滤器来解决,最常用的方法是使用Spring提供的编码过滤器:

在Web.xml中增加如下配置(要注意的是它的位置一定要是第一个执行的过滤器):

<filter>
<filter-name>charsetFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
该过滤器要做的其实就是强制为所有请求和响应设置编码格式:

request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
2、针对Get方式的乱码问题,由于参数是通过URL传递的,所以上面通过request设置的编码格式是不起作用的,此时可以在每次发生请求之前对URL进行编码:例如:Location.href=”/encodeURI”(“http://localhost/test/s?name=中文&sex=女”);

当然也有更简便的方法,那就是在服务器端配置URL编码格式:

修改tomcat的配置文件server.xml:
             <Connector URIEncoding=”UTF-8″ 
                 port=”8080″   maxHttpHeaderSize=”8192″
               maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
               connectionTimeout=”20000″ disableUploadTimeout=”true” />

只需增加 URIEncoding=”UTF-8″  这一句,然后重启tomcat即可。

原文链接:http://blog.csdn.net/rchm8519/article/details/41930393


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明网页回传字符变乱码
喜欢 (0)
[1034331897@qq.com]
分享 (0)