为什么用中文名字登陆时,登陆失败,提示没有这个用户,用英文字母的名字登陆正常我使用mysql-front建立一个数据库和数据表
字符集为:GBK
表的字符集也为:GBK然后在WEB-INF/web.xml中加入了这么一段代码 <filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>从数据库里读出来的中文字符能够正常显示问题在:::::::我用中文名字登陆时,登陆失败,提示没有这个用户,用英文字母的名字登陆正常
我在登陆的jsp文件中使用了struts中的html标签,代码如下:<%@ page contentType="text/html;charset=GBK"%>
<%@  taglib  uri="/tags/struts-bean"  prefix="bean"  %>  
<%@  taglib  uri="/tags/struts-html"  prefix="html"  %>  
<%@  taglib  uri="/tags/struts-logic"  prefix="logic"  %>  
<html>
<head>
<title>login.jsp</title>
<META http-equiv=Content-Type content="text/html; charset=GBK">
</head>
<body bgcolor="#FFFFFF">
<font color=red><html:errors/></font>
<html:form  action="/struts/showinput" focus="userid"  onsubmit="return validateUserForm(this)"> 
<table>
<tr>
<td>用户名:</td><td><html:text property="userid" maxlength="10"/><html:errors property="userid"/></td>
</tr>
<tr>
<td>密码:</td><td><html:password property="password" maxlength="10"/><html:errors property="password"/></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit>提交</html:submit>&nbsp;  
    <html:reset>重设</html:reset>&nbsp;<html:cancel >取消</html:cancel></td>
</tr>
<tr>
<td></td>
<td>最后登录时间:<bean:write name="userForm" property="latestOnline"/></td>
</tr>
</table>
</html:form>
<html:javascript dynamicJavascript="true" staticJavascript="true" formName="userForm"/>
</body>
</html>业务处理的代码为public class LogonAction extends Action{
     private  String userid;
     private  String password;
 public ActionForward execute(ActionMapping mapping,
  ActionForm form,
HttpServletRequest request,
HttpServletResponse response
 
  )throws Exception{
 
  UserForm actionform=(UserForm)form;
  userid=actionform.getUserid();
  actionform.setLatestOnline(actionform.getLatestOnline());
  password=actionform.getPassword();
  UserLog.setuserid(userid);
  UserLog.setpassword(password);
  Test ts=new Test();
  ResultSet rs;
  rs=ts.executeQuery("select * from testtable where user='"+userid+"' and password='"+password+"'");
int i=0;
  while(rs.next())
{
   i++;
}
  rs.close();
  ts.closecon();
  if(i>0)
  {
  return mapping.findForward("success");
 
  }
  else
  {
     return mapping.findForward("fail");
  }
 
 }  
}

解决方案 »

  1.   

    给你个事例参考下。记得要结贴给分。name=new String(request.getParameter("name").trim().getBytes("ISO8859_1"),"gb2312");
      

  2.   

    这类问题都是字符集转换问题,为什么不自己试一下啊。
    也可以把JSP页面的编码为ISO8859-1来解决的
      

  3.   

    request.setCharacterEncoding("gb2312");
    或者是
    <fmt:requsetEncoding value="gb2312"/>
    或者是用web-mxl影射
      

  4.   

    看看它是不是支持GBK呀!!
    把他换成ISO8859-1。或其他 的试试
      

  5.   

    若userid为主键,数据库有可能不支持中文。
    建议给每个用户一序号为主键
      

  6.   

    还是不行啊,有没有解决struts里中文登陆的例子我加了字符处理的代码结果还是一样useid是用户的名字UserForm actionform=(UserForm)form;
    userid=new String(actionform.getUserid().trim().getBytes("ISO8859-1"),"gb2312");
    或者:username=new String(actionform.getUsername().trim().getBytes("ISO8859-1"));或username=new String(actionform.getUsername().trim().getBytes("GBK"));在UserForm这个类里我也改了/*
     * 创建日期 2006-5-23
     *
     * TODO 要更改此生成的文件的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    package com;import java.io.UnsupportedEncodingException;
    import java.util.Date;import org.apache.struts.action.ActionForm;/**
     * @author Administrator
     *
     * TODO 要更改此生成的类型注释的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    public class UserForm extends ActionForm{

    private static final long serialVersionUID=1L;
    private String userid;
    private String password;
    private Date latestOnline = null;//最后登录时间
    public UserForm(){
    String userid=null;
        String password=null;
       
    }
    public String getUserid(){

    return userid;


    }
    public void setUserid(String userid){

    try {
    this.userid=new String(userid.getBytes("ISO8859-1")); //---这里进行了字符的转换
    } catch (UnsupportedEncodingException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }

    }

    public String getPassword(){

         return password;

    }
    public void setPassword(String password){

    this.password=password;

    }
    public Date getLatestOnline() {
    return latestOnline;
    }
    public void setLatestOnline(Date latestOnline) {
    this.latestOnline = latestOnline;
    }
    /*
    public ActionErrors validate(ActionMapping mapping,HttpServletRequest request ){

    ActionErrors errors=new ActionErrors();
    ActionMessage am;
    if(userid==""||userid.trim().equals(""))
    {
    am=new ActionMessage("LogonAction.fail.userid",userid);
    errors.add("userid",am);

    }

    if(password==""||password.trim().equals(""))
    {
    am=new ActionMessage("errors.required",password);
    errors.add("password",am);

    }
    return errors;



    }
    */

    }
      

  7.   

    public class UserActionForm
        extends ActionForm {
      private String password;
      private String userId;
      private String userName;
    ....
    }
    public class LoginAction
        extends Action {
    ....
     PreparedStatement pStat = con.prepareStatement(
           "select userName from mystruts_tab where userId=? and password=?");
       pStat.setString(1, userActionForm.getUserId());
       pStat.setString(2, userActionForm.getPassword());
       ResultSet rs = pStat.executeQuery();
       if (rs.next()){
         System.out.println(rs.getString("userName"));
         return mapping.findForward("success");   }
       else
         return mapping.findForward("fail");
     }
    ....}
    供参考,userId别设中文,userName可以为中文.
    判断 userId and password
      

  8.   

    问题已经解决,问题出在数据库上,设置一下,my.ini就可以了