为什么用中文名字登陆时,登陆失败,提示没有这个用户,用英文字母的名字登陆正常我使用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>
<html:reset>重设</html:reset> <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");
}
}
}
字符集为: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>
<html:reset>重设</html:reset> <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");
}
}
}
解决方案 »
- 请教有没有现成的方法将本页面保存成为word格式
- 在数据库里的增加新的字段后,数据无法显示!!
- 如何在jsp页面上单击按钮的时候弹出输入对话框
- arraylist.remove();为什么会出错,来看看!
- (@_@)这个问题提了都没有人解答呀!高手在哪???????我想大家也会遇到这个问题的?????
- 我现在遇到了一个棘手的问题数据库preparedstatement存储的中文问题!!很急急急急急急急急急急急急急得!有人能够解决我员送出150分!!
- preparedstatement用insert时候后面自动补空格,如何解决?
- 数字格式化的问题
- 关于jsp中捕获异常的基本问题,请指教。谢谢
- 如何刷新java Servlet项目中的页面?
- 为什么要这么转?关于文件下载的文件名的(utf8、gbk、8859-1)。
- 急~~~~网页表格导入数据库问题
也可以把JSP页面的编码为ISO8859-1来解决的
或者是
<fmt:requsetEncoding value="gb2312"/>
或者是用web-mxl影射
把他换成ISO8859-1。或其他 的试试
建议给每个用户一序号为主键
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;
}
*/
}
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