该页面的的编码格式是:gb2312,code如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %>
<!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=gb2312" />
但是页面的汉字,包括表格的表头的汉字都是问号,数据库里的中文数据不乱码,从数据库取到页面的中文数据在控制台打印出来不乱码,但是显示后全是?,请各位大侠指点一下 ,需要贴什么代码我再贴上来....先谢谢
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %>
<!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=gb2312" />
但是页面的汉字,包括表格的表头的汉字都是问号,数据库里的中文数据不乱码,从数据库取到页面的中文数据在控制台打印出来不乱码,但是显示后全是?,请各位大侠指点一下 ,需要贴什么代码我再贴上来....先谢谢
public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() {
this.encoding = null;
this.filterConfig = null;
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
chain.doFilter(request, response);
} public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
} protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class CharFilter implements Filter {
private String charset = "utf-8"; public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response,
FilterChain arg2) throws IOException, ServletException {
request.setCharacterEncoding(charset);
arg2.doFilter(request, response);
} public void init(FilterConfig arg0) throws ServletException {
charset = arg0.getInitParameter("charset");
}
}
web.xml文件里 <filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>CharFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
jsp页面 code:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" errorPage="" %>
<!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" />
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
再加上14楼的过滤器,还是不行...........
code:<!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" />
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<title>??????</title>
<link rel="stylesheet" rev="stylesheet" href="css/common.css" type="text/css" media="all" />
<script type="text/javascript">
function adddept(){
location.href="servlet/DeptService?code=alldept&dep=ad";
}
function deldept(deptid){
var flog = confirm("????????");
if(flog==true){
location.href="servlet/DeptService?code=deladept&deptid="+deptid;
}
}
</script>
<body>
<div id="man_zone">
<div align="right">"<input type="button" value="??????"/></div>
<br/>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" class="table_style">
<tr align="center">
<td width="233"><span class="left-title">??????</span></td>
<td width="236"><span class="left-title">??????</span></td>
<td width="236"><span class="left-title">???????</span></td>
<td width="170"><span class="left-title">?? ??</span></td>
</tr>
<tr align="center">
<td>null</td>
<td>??????</td>
<td></td>
<td><a href="DeptService?code=deladept&deptid=62">???</a> <a href="#" onClick="deldept(62)">???</a></td>
</tr>
<tr align="center">
<td>00001</td>
<td>??????</td>
<td></td>
<td><a href="DeptService?code=deladept&deptid=21">???</a> <a href="#" onClick="deldept(21)">???</a></td>
</tr>
<tr align="center">
<td>00002</td>
<td>?????</td>
<td></td>
<td><a href="DeptService?code=deladept&deptid=22">???</a> <a href="#" onClick="deldept(22)">???</a></td>
</tr>
</table>
</div>
</body>
</html>
一个问号代表一个汉字.期待详细解答......
MyEclipse右键工程选属性,把编码改成你页面的编码,里面的几个编码都试试 --!
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain arg2) throws IOException, ServletException {
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
arg2.doFilter(request, response);
}
这样就解决了,但是引起这样原因我还是不知道,哪位高手能解答,希望给一下,谢谢
加了一句response.setCharacterEncoding("UTF-8");就解决了