当前位置:编程学习 > JAVA >>

初学J2EE遭遇乱码问题


eclipse中已经设置为utf-8.页面显示前面都没乱
后面就乱了,不知道为啥,改变过tomcat的编码模式,还是不行
网上各种方法解决不了啊。求大神。 --------------------编程问答-------------------- 这信息应该是你输入的吧?
你是通过查数据库还是前台页面接收还是别的方式? --------------------编程问答--------------------
引用 1 楼 AA5279AA 的回复:
这信息应该是你输入的吧?
你是通过查数据库还是前台页面接收还是别的方式?

前台页面接受。肯定不是数据库,这个刚开始学还没用到数据库。 --------------------编程问答-------------------- 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的代码吧。 --------------------编程问答-------------------- 在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="提交" > <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>

效果:提交后 --------------------编程问答-------------------- 先通过控制台检查是在哪个层乱码了,
先看看传输到servlet的时候试试不是乱码,一般都是这里乱码。
如果怎么都解决不了,可以用以下办法:
假设乱码字符串为str1
str2 = new String (str1.getBytes() ,"utf-8");
直接把乱码字符串重编码
--------------------编程问答--------------------
引用 4 楼 muyexia521 的回复:
在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="提交" > <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>

效果:提交后

谢谢,原来是最后那没设置好
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,