servlet中部分代码是这样的,并且在之前已经设置了 request.setCharacterEncoding("GB2312");
- Java code
request.setAttribute("testContent", testContent); System.out.print(request.getAttribute("testContent").toString()); getServletConfig().getServletContext().getRequestDispatcher( "/jsp/student/stu_initial_ability.jsp").forward(request, response);
jsp中代码
- HTML code
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" ><link rel="stylesheet" href="<%= request.getContextPath()%>/mycss.css" type="text/css">输出信息的代码<td><%String testContent=request.getAttribute("testContent").toString(););%> <%=testContent%></td>
获取的testContent中中文显示为?,英文正常显示,在整个页面中的其他汉字是可以正常显示的,只有testContent的为乱码,求高手帮助
------解决方案--------------------------------------------------------
处理中文乱码
(1)在html页面中使用
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
要求浏览器以指定的编码打开该html文件,提交表单数据时按照该编码进行
(2)在Servlet中处理时使用
request.setCharacterEncoding(“编码”);该编码应和提交表单数据时使用的编码一致
要放在request.getParameter(“参数名”)之前
response.setContentType(“text/html;charset=utf-8”);
要放在response.getWriter();之前
作用是:a、设定响应所采用的编码 b、告诉浏览器打开该内容时所采用的编码
(3)在JSP中处理时使用
<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
(4)数据库要采用支持中文的编码来保存数据
mysql中使用:CREATE DATABASE heshi DEFAULT CHARACTER SET utf8;
(5)对于jdbc驱动要支持编码转换
mysql中使用:
jdbc:mysql://192.168.1.2:3306/heshi?useUnicode=true&characterEncoding=utf8
我是以utf-8编码为例,因为现在差不多通用性较好,如果出现乱码,很可能是5步骤中的一步出错,再者,ec
lipse要设置一致的文本编码;