String u1=request.getParameter("username");
String u=new String(u1.getBytes("iso-8859-1"),"utf-8");
String p=request.getParameter("userpasswd");
UserBeanCl ubc=new UserBeanCl();
if(ubc.CheckUser(u,p)){
request.getSession().setAttribute("pass",u);
request.getRequestDispatcher("main.jsp").forward(request, response);
}else{
request.getRequestDispatcher("Login.jsp").forward(request, response);
}转中文是这个方法吗? 这样还是不能解决哦 有其他办法吗? tomcat里的配置文件我改了下 也不行哦 上次 做的一个例子, 也是用这个方法 可以的, 但是我现在打开那个例子 也变成乱码了, 奇`~~~~~~怪~~~~~~ 哪位大虾帮我分析一下 可能在哪里出错了呀
String u=new String(u1.getBytes("iso-8859-1"),"utf-8");
String p=request.getParameter("userpasswd");
UserBeanCl ubc=new UserBeanCl();
if(ubc.CheckUser(u,p)){
request.getSession().setAttribute("pass",u);
request.getRequestDispatcher("main.jsp").forward(request, response);
}else{
request.getRequestDispatcher("Login.jsp").forward(request, response);
}转中文是这个方法吗? 这样还是不能解决哦 有其他办法吗? tomcat里的配置文件我改了下 也不行哦 上次 做的一个例子, 也是用这个方法 可以的, 但是我现在打开那个例子 也变成乱码了, 奇`~~~~~~怪~~~~~~ 哪位大虾帮我分析一下 可能在哪里出错了呀
import java.io.IOException;
/**
* 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题
*/
public class CharacterEncodingFilter
implements Filter
{
protected FilterConfig filterConfig = null;
protected String encoding = "";
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
if(encoding != null)
servletRequest.setCharacterEncoding(encoding);
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy()
{
filterConfig = null;
encoding = null;
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
} 在web.xml中加入Filter的配置,如下:
<filter> <filter-name>EncodingAndCacheflush</filter-name>
<filter-class>EncodingAndCacheflush</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingAndCacheflush</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
要传递参数的时候最好使用form进行传参,如果使用链接的话当中文字符的时候过滤器转码是不会起作用的,还有就是页面上 form的method也要设置为post,不然过滤器也起不了作用。
你页面的字符编码是不是iso-8859-1,如果不是有可能是因为你用错误的编码格式将字符串变为字节编码,在用UTF-8转就怎么转怎么错!
另外加个过滤器,或者在servlet中加一句
request.setCharacterEncoding("utf-8");
更改客户端的请求数据为utf-8编码。
String ss=new String(s.getBytes("ISO8859_1"));
http://blog.csdn.net/crazylaa/archive/2009/12/24/5066784.aspx
服务器从request中获得的是以什么字符集编码的字节流。
服务器以什么字符集进行解码在服务器端生存字符串。
如果你那个公式可以通用,那sun自己不就可以在内部解决掉了。还需要你来硬编码干什么
写个过滤器
Jsp页面加上
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Html页面加上
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
String u=new String(u1.getBytes("iso-8859-1"),"utf-8");//这里你可以试一下别的编码之间的转换.比如ISO8859-1->GBK,UTF8->GBK,总是能够转换正确的.不过,最好是参考LS各位说的,用过滤器,页面统一编码,数据库统一编码