jsp中为什么使用中文会出现乱码

来源:百度知道 编辑:UC知道 时间:2024/06/21 08:02:10
tr><td width=200 bgcolor=yellow>使用者:</td>
<td><%=request.getRemoteAddr()%></td>
</tr>
这样应该是没有错,但是"使用者"三个字显示不出来,而是乱码,这到底是怎么回事?

你没有进行中文编码,在jsp文件中加入
<%@page contentType ="text/html; charset=gbk"%>
就可以了,gbk 是中文简体编码

中文乱码一般有这两种错误,1是页面编码方式没有设置,默认为utf-8的,用<%@page contentType="text/html" pageEncoding="GBK"%>改为中文编码方式,2是在request中接收中文数据出现乱码,你的应该是这种情况,用下面的指令修改即可
<% request.setCharacterEncoding("gb2312");%>,
String bookname=request.getParameter("name").trim();
bookname=new String(bookname.getBytes("ISO8859_1"),"GBK");

楼主是页面编码设置错了吧
<%@page contentType="text/html" pageEncoding="UTF-8"%>
用pageEncoding来指定一个支持中文编码的就可以了

当然,也可以是
<%@page contentType="text/html" pageEncoding="GB2312"%>
<%@page contentType="text/html" pageEncoding="GBK"%>
这些了
我在netbeans 里面测试了,可行