从servlet用forward方式重定向到JSP中文乱码 5
servlet中的代码publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)thr...
servlet中的代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request. setCharacterEncoding("utf-8");
response.setContentType("text/xml; charset=utf-8");
//response.getWriter().print( " <script> location.href= '"+request.getContextPath()+"/servlet/xxxxxxServlet'; </script> ");
//PrintWriter out = response.getWriter();
String cpdx=(String)request.getAttribute("cpdx");
System.out.println((String) request.getAttribute("cpdx"));
Show show=new Show();
List<XlsDto> listcp=new ArrayList<XlsDto>();
listcp= show.showCplist(cpdx);
request.setAttribute("listcp", listcp);
//response.sendRedirect("./Ceping.jsp");
request.getRequestDispatcher("./Ceping.jsp").forward(request,response);
}
以response.sendRedirect("./Ceping.jsp");跳转不会出现乱码,但是不能将listcp中的值提交到前台
以request.getRequestDispatcher("./Ceping.jsp").forward(request,response);方式提交,出现中文乱码,在控制台上显示正常,到JSP上就乱码了
JSP的代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div align="center">
<div id="top"></div>
<div id="navigaiong">
<a href="index.jsp"><font color="#ffffff">首页</font></a> | <a
href="Rule.jsp"><font color="#ffffff">规则导入</font></a> | 检查记录 |
用户登录
</div>
<div id="center">
<br><br>
<table border="0.05" width="870px">
<tr>
<td width="100px">测评对象</td>
<td width="100px">安全控制点</td>
<td width="300px">测评指标</td>
<td width="270px">结果记录</td>
<td width="100px">符合程度</td>
</tr>
<c:forEach items="${listcp}" var="listcp">
<tr>
<td> ${listcp.cpdx}</td>
<td> ${listcp.control}</td>
<td> ${listcp.cpzb}</td>
<td> </td>
<td> </td>
<tr>
</c:forEach>
</table>
</div>
<div id="blow"></div>
</div>
</body>
</html> 展开
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request. setCharacterEncoding("utf-8");
response.setContentType("text/xml; charset=utf-8");
//response.getWriter().print( " <script> location.href= '"+request.getContextPath()+"/servlet/xxxxxxServlet'; </script> ");
//PrintWriter out = response.getWriter();
String cpdx=(String)request.getAttribute("cpdx");
System.out.println((String) request.getAttribute("cpdx"));
Show show=new Show();
List<XlsDto> listcp=new ArrayList<XlsDto>();
listcp= show.showCplist(cpdx);
request.setAttribute("listcp", listcp);
//response.sendRedirect("./Ceping.jsp");
request.getRequestDispatcher("./Ceping.jsp").forward(request,response);
}
以response.sendRedirect("./Ceping.jsp");跳转不会出现乱码,但是不能将listcp中的值提交到前台
以request.getRequestDispatcher("./Ceping.jsp").forward(request,response);方式提交,出现中文乱码,在控制台上显示正常,到JSP上就乱码了
JSP的代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div align="center">
<div id="top"></div>
<div id="navigaiong">
<a href="index.jsp"><font color="#ffffff">首页</font></a> | <a
href="Rule.jsp"><font color="#ffffff">规则导入</font></a> | 检查记录 |
用户登录
</div>
<div id="center">
<br><br>
<table border="0.05" width="870px">
<tr>
<td width="100px">测评对象</td>
<td width="100px">安全控制点</td>
<td width="300px">测评指标</td>
<td width="270px">结果记录</td>
<td width="100px">符合程度</td>
</tr>
<c:forEach items="${listcp}" var="listcp">
<tr>
<td> ${listcp.cpdx}</td>
<td> ${listcp.control}</td>
<td> ${listcp.cpzb}</td>
<td> </td>
<td> </td>
<tr>
</c:forEach>
</table>
</div>
<div id="blow"></div>
</div>
</body>
</html> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询