既然统一用UTF-8
直接使用:
request.setCharacterEncoding("UTF-8");
就好了啊。
直接使用:
request.setCharacterEncoding("UTF-8");
就好了啊。
解决方案 »
- jsp乱码问题
- Ext.data.jsonStore动态加载的问题
- js脚本问题
- 多选列表框中,如何触发双击事件
- 用Java虚拟机(msjavx86.exe和webeq3.jar包)做的网页,显示数学公式
- 怎么用java实现验证码(完整版)
- 怎样把上传的图片或文件直接保存到数据库中(MSSQL)?
- 怎样速成jsp,给分100
- 这个流的问题怎么没有人会?大家救急!
- 新手求助!!!myeclipse使用XMLWriter的时候,为什么XMLWriter提示构造方法没定义??
- 在javabean中如何把两个不同的表里的内容返回到jsp中
- 使用DataInputStream 的read方法读取文件,知道读取完成了?
这里没高手了吗?还是不肯告诉我啊?分不够我可以加
1.web.xml中配置一个filter
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<display-name>Set CharacterEncoding Filter</display-name>
<filter-class>com.oceansoft.szcx.common.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8[也可以该成别的编码]</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
过滤器写法
/*
* SetCharacterEncodingFilter.java
*
* Created on 2003-9-12
*
*/
package com.oceansoft.szcx.common;
import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;/**
* @author Realfish
* 2003-9-12
*/
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() { this.encoding = null;
this.filterConfig = null; }
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException { // Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
} // Pass control on to the next filter
chain.doFilter(request, response); }
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false; }
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) { return (this.encoding); }
}2.页面上写上
<%@ page contentType="text/html;charset=UTF-8" language="java" %>3.数据库编码UTF-8就可以了。问题:
1.所有post的提交有效,get的可能有时会不行。
2.我都是基于struts的。其余的应该也可以。
public static String toGbkString(String s) { StringBuffer sb = new StringBuffer();
if (s != null) {
byte[] b = new byte[3];
int temp;
int index;
try {
while (s.length() != 0) {
index = s.indexOf("%");
if (index == -1)
return sb.append(s).toString();
if (index != 0) {
sb.append(s.substring(0, index));
}
if (index + 9 <= s.length()) {
temp =
Integer.parseInt(
s.substring(index + 1, index + 3),
16)
- 256;
b[0] = Byte.parseByte(String.valueOf(temp));
temp =
Integer.parseInt(
s.substring(index + 4, index + 6),
16)
- 256;
b[1] = Byte.parseByte(String.valueOf(temp));
temp =
Integer.parseInt(
s.substring(index + 7, index + 9),
16)
- 256;
b[2] = Byte.parseByte(String.valueOf(temp)); sb.append(new String(b, "utf-8"));
index += 9;
s = s.substring(index);
} else {
System.out.println("数据格式错误");
return null;
}
}
} catch (UnsupportedEncodingException ue) {
System.out.print(
"can't convert to gb2312 : " + ue.getMessage());
} catch (NumberFormatException ne) {
System.out.print(
"can't convert to gb2312 : " + ne.getMessage());
}
}
return sb.toString();
}
但是我发现没地方用这个方法啊,比方说一个连接 test.jsp?name=%4E%6A%84 当我在test.jsp里面执行request.getParameters(name)竟然取得的不是%4E%6A%84,而是中文乱码
对于其难点搞了好长时间,现在基本都能解决了,主要表现在表单页面的提交与数据库中的数据字节转换的问题。可以通过一些相关的setContentType进行设置、用setCharacterEncoding对数据进行统一编码转换、和用getBytes对字符进行转换都可以。
<html>
<head>
<title>Welcome</title>
</head>
<body>
<%
if(request.getParameter("name")!=null) {
String s = new String(request.getParameter("name").getBytes("iso-8859-1"));
out.println(s);
}%>
<form name="form1" method="post" action="/Test/test">
<input name="name" type="text">
<input name="submit" type="submit" value="提交">
</form>
</body>
</html>Servlet:
package servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Convert;
/**
* @author WuBin
*
*
*
*/
public class Test extends HttpServlet { protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String s = new String(request.getParameter("name").getBytes("iso-8859-1"));
System.out.print(s);
response.sendRedirect("/Test/index.jsp?name=" + java.net.URLEncoder.encode(s , "gb2312"));
}
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
你的这种方法确实是可以解一些乱码,但是碰到原来就是%这种特殊情况还是会出错。
String abc = request.getParameter("abc");
abc = new String(abc.getBytes("gbk"), "iso-8859-1");
其中的 gbk 换成 utf-8 也可以。
如果以上的转换不成功,把 gbk 和 iso-8859-1 换一下位置再试试。
angelheart()的方法能行~~~我马上就结贴
基本上不会出现你说的情况,因为只对中文处理,就算中文里混标点符号也没关系的