eclipse中已经设置为utf-8.页面显示前面都没乱
后面就乱了,不知道为啥,改变过tomcat的编码模式,还是不行
网上各种方法解决不了啊。求大神。

解决方案 »

  1.   

    servlet中做如下操作
    //设置解码格式
            response.setContentType("text/html");
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");jsp中,首行如下写法:
    <%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" pageEncoding="utf-8"%>检查utf-8的拼写。还不行,请发下jsp和servlet的代码吧。
      

  2.   

    在JSp开头<%@ page language="java" contentType="text/html; 
    charset=UTF-8" pageEncoding="UTF-8"%>还有在页面内加入
    request.setCharacterEncoding("utf-8");
    例如input.jsp:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"contentType="text/html;charset=utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'input.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        <div align="center">
        <form method="post" action="SaveName.jsp">
          您的姓名:<input type="text" name="username" size="20">
          <p><input type="submit" value="提交" >&nbsp;<input type="reset" value="重置">
          </p>
          </form>
          </div>
      </body>
    </html>
    下面是SavaName.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'SaveName.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
       <%
       
       request.setCharacterEncoding("utf-8");
       String name = request.getParameter("username");//获取上一页面输入的用户名

       out.println("hello!"+name);
       %>
      </body>
    </html>
    效果:提交后
      

  3.   

    先通过控制台检查是在哪个层乱码了,
    先看看传输到servlet的时候试试不是乱码,一般都是这里乱码。
    如果怎么都解决不了,可以用以下办法:
    假设乱码字符串为str1
    str2 = new String (str1.getBytes() ,"utf-8");
    直接把乱码字符串重编码