JSP中utf-8中文乱码问题。。。


我就不明白了。。我写了两个jsp文件,编码均为utf-8,一个就能正常显示中文,另一个则不能。。。求解啊

正常显示中文的jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>站内检索索引建立页面</title>
<script type="text/javascript">
    function checkForm(form){
        if(form.toIndexPage.value==""||form.toIndexPage.value==null){
            alert("请输入要建立索引的网站!");
            form.toIndexPage.focus();
            return false;
        }
        else
            return true;
    }
</script>
</head>
<body>
<form action="<%=request.getContextPath()%>/IndexServlet" 
    METHOD="post" onsubmit="return checkForm(this)">  <%--查询表单  10行--%>
<B>输入要建立索引的网站:</B>
<INPUT TYPE=text NAME="toIndexPage" SIZE=30 VALUE="">
<INPUT TYPE=submit VALUE="查询">
</form>
</body>
</html>

乱码显示中文的jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.util.*,java.text.*" pageEncoding="utf-8" %>
<% request.setCharacterEncoding("utf-8"); %>
<!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" />
<title>站内检索系统检索页面</title>
<style type="text/css">
<!--
.STYLE1 {
    font-family: "仿宋";
    font-size: 18px;
}
.STYLE2 {
    font-family: "黑体";
    font-weight: bold;
}
-->
</style>
</head>

<body>
<div>
  <div align="center" class="STYLE2">
    <h1>站内检索系统</h1>
  </div>
</div>
<div>
  <p align="center" class="STYLE1">索引建立成功:<%=request.getAttribute("WebName") %></p>
  <p align="center" class="STYLE1">请输入检索内容:
    <input type="text" name="textfield" />
    <input type="submit" name="Submit" value="检索" />
  </p>
</div>
<div>
  <div align="center">当前时间:<%String mydate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());%>
     
<%=mydate%></div>
</div>
</body>
</html>

java utf-8 编码 JSP JavaScript

5631353 11 years, 9 months ago

大概是因为一个是utf-8编码,一个是utf-8 +bom的编码,两种的区别在视觉上是看不出来的。你放到eclipse中统一改成utf-8的就好了。

纯爷们之梦 answered 11 years, 9 months ago

Your Answer