哎有出问题了救救我吧,我为什么我总会在这种问题上纠缠不清
这是一段JAVA类代码,用struts做的一个东西的登录功能如下:
public class UserAction extends MappingDispatchAction { UserDaoIMPL dao = new UserDaoIMPL(); // 用户登录
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;

Cookie cookie = new Cookie("userName",userForm.getUsername());// 1

cookie.setMaxAge(60*60*24*365); response.addCookie(cookie);//  2

userForm = dao.login(userForm.getUsername(), userForm.getPassword()); //查找用户是否存在login.jsp 页面username代表用户名如下:
<%@ page language="java" pageEncoding="UTF-8"%>
....
<html:text property="username" value="${cookie.userName.value}"/></td>就几行代码完成了cookie功能,在登录的时候,我输入非中文字符的时候可以登录进去,用中文登录的时候报错如下:
严重: Servlet.service() for servlet ActionServlet threw exception
java.lang.IllegalArgumentException: Control character in cookie value, consider BASE64 encoding your value
at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:365)
at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:358)
at org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:268)
at org.apache.catalina.connector.Response.addCookieInternal(Response.java:976)
at org.apache.catalina.connector.Response.addCookie(Response.java:945)
at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:343)
at cn.com.tarena.mvc.action.StudentAction.setLoginStatus(StudentAction.java:55)
at cn.com.tarena.mvc.action.StudentAction.login(StudentAction.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
已经添加乱码过滤器如下:   还有web也加了...
public class EncodingFilter implements Filter { public void destroy() {
// TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding("utf-8");
arg2.doFilter(arg0, arg1); } public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub }
将utf-8改成别的编码还是错的
当我把以上注释那点的 1和2处去掉的时候,用中文和非中文都能登录成功,请大家支招,谢谢!

解决方案 »

  1.   

    将要保存的值进行URLEncoder.encode(value,"utf-8")编码。在提取时,同样进行解码
      

  2.   

    哈哈……其实你的过滤式对的啦……但是要注意,cookie好像只能存取英文吧!你可以查查!一定是cookie的问题啦!
      

  3.   

    问题解决
    这样来:
    public ActionForward login(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {UserForm userForm = (UserForm) form;
    try {
    String username = new String(userForm.getUsername().getBytes("ISO-8859-1"), "gb2312" );[/color[color=#FF0000]]//原来乱码可以这样解决!


    Cookie cookie = new Cookie("userName",username);

    cookie.setMaxAge(60*60*24*365); response.addCookie(cookie);
    userForm = dao.login(userForm.getUsername(), userForm.getPassword());
      

  4.   

    <html:form action="/login" >这个标签的默认mothed是属性是post吧?我就这么写的,还有个疑问:是不是说使用utf-8编码比用gb2312编码或GBK更好呢?听说UTF-8可以识别很多种语言,而GBK只能识别英文和中文的?但是我用UTF-8的时候问题总是出在乱码上,改成GB2312稍微好转一点。
      

  5.   

    用utf-8怎么改都会有乱码?请问为什么啊?
     String username = new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8" ); //传过来的username是中文值的时候显示"???"乱码
    页面都是utf-8
    request.getParameter("username")username参数是这样传的:
    <a href="tomodify.asp?username=${studentList.username}" >${studentList.username}</a>
    添加过滤器了,在tomcat的service文件也加了URIEncoding="UTF-8"了啊。改成GB2312问题是能解决,

      

  6.   

    1浏览器发出请求的默认编码是utf-8;
    2tomcat默认解释的是iso8859-1;uriencoding就是对他的修改
    3.如果有过滤器,那你又可以设置。
    4.struts中有一个类,继承RequestProcessor类设置。
    5。在程序中显示设置。
    以上是uri的编码过程。
      

  7.   

    一般我是两句一起用的
    request.setContentType="text/html;charset=UTF -8"

    String username = new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8" )