jsp中文显示乱码解决方案
一、JSP页面显示乱码
二、表单提交中文时出现乱码
三、数据库连接
大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着您,我现在把我在JSP开发中遇到
的中文乱码的问题及解决办法写出来供大家参考。
一、JSP页面显示乱码
下面的显示页面(display.jsp)就出现乱码:
<html>
<head>
<title>JSP的中文处理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
out.print("JSP的中文处理");
%>
</body>
</html>
对不同的WEB服务器和不同的JDK版本,处理结果就不一样。原因:服务器使用的编码方式不同和浏览器
对不同的字符显示结果不同而导致的。解决办法:在JSP页面中指定编码方式(gb2312),即在页面的第一
行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除乱码了。完整页面如下
:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>JSP的中文处理</title>
<meta http-equiv="C ......
get
jsp url ${param.serialID}
action jsp String commentId = ServletActionContext.getRequest().getParameter("commentId");
---------------------------------------
action ServletActionContext.getRequest().setAttribute("searchContent", searchContent);
jsp action <s:property value="searchContent"></s:property>
---------------------------------------
function hanshu(num)
{
window.location.href="<%=request.getContextPath()%>/a.action?num="+num;
}
<a href="javascript:hanshu('${param.url参数名}')"></a>
=====================
post
//使用trim()
String.prototype.trim=function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
function hanshu()
{
......
get
jsp url ${param.serialID}
action jsp String commentId = ServletActionContext.getRequest().getParameter("commentId");
---------------------------------------
action ServletActionContext.getRequest().setAttribute("searchContent", searchContent);
jsp action <s:property value="searchContent"></s:property>
---------------------------------------
function hanshu(num)
{
window.location.href="<%=request.getContextPath()%>/a.action?num="+num;
}
<a href="javascript:hanshu('${param.url参数名}')"></a>
=====================
post
//使用trim()
String.prototype.trim=function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
function hanshu()
{
......
<SCRIPT LANGUAGE="javascript">
//指定页面区域内容导入Word
function AllAreaWord() {
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
//window.close();
}
</SCRIPT>
应用举例:
<table width="100%" align=center border="0" cellpadding="0" cellspacing="0" id = "PrintA" ......
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> ......
<html:html>
<HEAD>
<tiles:insert attribute="header" />
</HEAD>
<body>
<tiles:insert attribute="top" />
<table width="100%" height="400" border="0" align="center"
cellpadding="0" cellspacing="0">
<tr>
<td width="178" height="450" valign="top" class="gray" id="mtd"><!--left start-->
<tiles:insert attribute="menu" /> <!--left end--></td>
<td width="1" class="gray1"></td>
<td valign="top" width="99%" class="mainTableContent"><!--main start--> <tiles:insert attribute="main" /> <!--main end-->
<br>
</td>
</tr>
</table> ......
<table width=100% border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td height="1" class="gray1"></td>
</tr>
</table>
<tiles:insert attribute="footer" />
</body>
</html:html> ......