用requestDispatcher.forward(request,response)重定向后的页面为乱码

nujehhejun 2009-06-08 11:35:26
用requestDispatcher.forward(request,response)重定向后的页面为乱码

javax.servlet.RequestDispatcher dis=request.getRequestDispatcher("/viewbook.jsp");
dis.forward(request,response);
在viewbook.jsp中已经加了页面指令:<%@ page language="java" import="java.util.*,hejun.*" pageEncoding="gb2312"%>
可是重定向后viewbook.jsp显示的是乱码,为什么呀?

高手指点下呀,谢谢啦
...全文
347 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
stonegreen72 2010-08-01
  • 打赏
  • 举报
回复
response.setCharacterEncoding("gb2312");//加上此句!
PrintWriter out = response.getWriter();
nujehhejun 2009-06-09
  • 打赏
  • 举报
回复
我在viewbook.jsp文件前面加了<%request.setCharacterEncoding("gb2312"); %>
不知道为什么还是不起作用呀
kennydkkk 2009-06-09
  • 打赏
  • 举报
回复



这是Request 和Response 的区别



request是发送时的编码设置
而response是响应时的编码设置


这个问题应该是response
sangshusen_1988 2009-06-09
  • 打赏
  • 举报
回复
楼主最好把编码给统一下,只用一种编码,
这样出了问题也容易排除
laorer 2009-06-09
  • 打赏
  • 举报
回复
你的文件的格式是什么? 是 utf8 还是 gb2312?

在转向之前,加个这个试试
request.setCharacterEncoding("gb2312");
yooooyiiiiiiiiii 2009-06-09
  • 打赏
  • 举报
回复
2个页面的编码必需一致。都为gb2312。
设置charset<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
servlet中request和response都设置Encoding。

nujehhejun 2009-06-09
  • 打赏
  • 举报
回复
怎么样用page指令设置request.setCharacterEncoding("UTF-8")呀?本人刚学JSP,好多不懂,这和<%request.setCharacterEncoding("gb2312"); %>的功能不是一样的吗
hwhack 2009-06-09
  • 打赏
  • 举报
回复
你应该在page指令里去设置request.setCharacterEncoding("UTF-8");
nujehhejun 2009-06-09
  • 打赏
  • 举报
回复
这是我的servlet:
public class BookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
BookBean bookBean=new BookBean();
PageBean pageCtl=bookBean.listData((String)request.getParameter("jumpPage"));
request.setAttribute("pageCtl",pageCtl);
}
catch(Exception e){
e.printStackTrace();
}

javax.servlet.RequestDispatcher dis=request.getRequestDispatcher("/viewbook");
dis.forward(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request,response);
}

//下面是viewbook.jps
<%@ page language="java" import="java.util.*,hejun.*"%>
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<%@include file="trans.jsp" %>
<jsp:useBean id="pageCtl" class="hejun.PageBean" scope="request"/><html>
<head>
<title>分页浏览</title>
</head>

<body>
<table border=1>
<tr><td>书号</td><td>书名</td><td>出版社</td><td>价格</td></tr>
<%
java.util.Collection v=pageCtl.getResult();
Iterator it=v.iterator();
while(it.hasNext())
{
Book book=(Book)it.next();
%>
<tr>
<td align="center" width="95"><%=trans(book.getBookId())%></td>
<td align="center" width="95"><%=trans(book.getBookName())%></td>
<td align="center" width="95"><%=trans(book.getPublisher()) %></td>
<td align="center" width="95"><%=book.getPrice() %></td>
</tr>
<%} %>
</table>
<%if(pageCtl.maxPage!=1){ %>
<form name="PageForm" action="/Advanced_DB_Programming/servlet/BookServlet" method="post">
<%@include file="/pageman.jsp" %>
</form>
<%}%>
</body>
</html>

这是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>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>BookServlet</servlet-name>
<servlet-class>hejun.BookServlet</servlet-class>
</servlet>

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

<servlet>
<servlet-name>ViewBookJSP</servlet-name>
<jsp-file>/viewbook.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>ViewBookJSP</servlet-name>
<url-pattern>/viewbook</url-pattern>
</servlet-mapping>
</web-app>

为什么转到viewbook.jsp后显示的是乱码,就边中间表格的表头也是乱码
LazyCat2222 2009-06-08
  • 打赏
  • 举报
回复
那你在开始的时候的时候还是要设置编码吧,reques.setChar...("GBK");不太记得那方法了。
很多方法可以设置编码问题。。。要么用过滤器

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧