[求助]逻辑错误,初学者求教!

IKARI_DALI 2006-02-27 11:25:42
我做了一个注册页面,如果userl中没有查询到与reg.htm表单提交的username相同的数据行则在userl中增加这个数据,将这行信息保存到session中并转到index.jsp页面中通过session显示刚刚添加的username,现在能够正常添加数据,但是当我尝试在表单中添加已经存在的username后同样会转到index.htm并显示username........当然数据库中原来的数据行并没有被覆盖,各位大虾帮我看看,应该怎么改啊,先谢谢了~急

reg.htm
<HTML>
<HEAD>
<TITLE>reg</TITLE>
</HEAD>
<BODY>
<TABLE width="90%" align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">
<TR>
<TD align="center" bgcolor="#CCCC99">注册截面</TD>
</TR>
<TR>
<TD><TABLE align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">
<FORM ACTION="regservlet">
<TR>
<TD>姓名:</TD>
<TD bgcolor="#CCCC99"><input type="text" name="userName" size=25></TD>
</TR>
<TR>
<TD bgcolor="#CCCC99"><input type="password" name="userPwd" size=25></TD>
</TR>
<TR>
<TD colspan=3><TABLE align="center" width="100%" cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">
<TR>
<TD align="center" bgcolor="#CCCC99"><input type="submit" value="注册"></TD>
<TD align="center" bgcolor="#CCCC99"><input type="reset" value="重新填写"></TD>
</TR>
</TABLE></TD>
</TR>
</FORM>
</TABLE></TD>
</TR>
</TABLE>
</BODY>
</HTML>

RegServlet.java
package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import bean.*;

public class RegServlet extends HttpServlet
{
private static final String CONTENT_TYPE="text/html;charset=GB2312";
public void init() throws ServletException
{}

public void doGet(HttpServletRequest requ,HttpServletResponse resp) throws ServletException,IOException
{
boolean flag=false;
String userName=requ.getParameter("userName");
String userPwd=requ.getParameter("userPwd");
DataBean db=new DataBean();
UserBean ub=new UserBean();
ub.setUsername(userName);
ub.setUserpwd(userPwd);
db.adduserBean(ub);
flag=db.adduserBean(ub);
if(flag=true)
{
requ.getSession().setAttribute("user",ub);
resp.sendRedirect("index.jsp");
}
else
{resp.sendRedirect("reg.htm");}

}

public void doPost(HttpServletRequest requ, HttpServletResponse resp)
throws ServletException, IOException
{
doGet(requ,resp);
}

public void destroy()
{
}
}

UserBean.java
package bean;

//user Bean

public class UserBean
{
private String userName;
private String userPwd;
public UserBean()
{
}

public void setUsername(String userName)
{
this.userName=userName;
}

public void setUserpwd(String userPwd)
{
this.userPwd=userPwd;
}

public String getUsername()
{
return userName;
}

public String getUserpwd()
{
return userPwd;
}
}

DataBean.java
package bean;

import java.sql.*;
import bean.*;
import java.util.*;

//DataBaseBean

public class DataBean
{
private Connection conn=null;
private ResultSet res=null;
private java.sql.PreparedStatement prepar=null;
private java.sql.CallableStatement proc=null;

public DataBean()//connection
{
String CLASSFORNAME="com.mysql.jdbc.Driver";
String URL="jdbc:mysql://localhost:3306/ikaribbs?useUnicode=true&characterEncoding=GB2312";
String USER="ikaridali";
String PW="TRSTTT";
try
{
Class.forName (CLASSFORNAME);
conn=java.sql.DriverManager.getConnection(URL,USER,PW);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public boolean addUserBean(UserBean ub)//add user
{
boolean flag = false;
String sql = "insert into admin values(?,?)";
if (hasUser(ub.getUsername()))
{
return false;
}
try
{
prepar = conn.prepareStatement(sql);
prepar.setString(1, ub.getUsername());
prepar.setString(2, ub.getUserpwd());
int result = prepar.executeUpdate();
if (result > 0)
{
flag = true;
}
else
{
flag = false;
}

}
catch (Exception ex)
{
flag = false;
ex.printStackTrace();
}

return flag;
}

public boolean hasUser(String userName)//check
{
boolean flag=true;
String sql="select count(*) from userl where userName=?";
try
{
PreparedStatement prepar=conn.prepareStatement(sql);
prepar.setString(1,userName);
res=prepar.executeQuery();
res.next();
int result=res.getInt(1);
if (result>0)
{
flag=true;
}
else
{
flag=false;
}
}
catch(Exception ex)
{
ex.printStackTrace();
flag=true;
}
return flag;
}
}

index.jsp
<%@page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,bean.*,servlet.*"%>
<html><body>
<%
UserBean ub=(UserBean)session.getAttribute("user");
try
{
out.print(ub.getUsername());
}
catch(Exception e)
{
out.print("失败");
}
%>
</body></html>

很不好意思又放那么大段代码上来烦大家,但是我真的很想把它学好~所以.......还是谢谢了!

另外:
if (hasUser(ub.getUsername()))
{
return false;
}
像这一段我还不是很明白,hasUser(ub.getUsername())能够返回的是什么值???
而像这样在if()的()没有条件的结构该如何理解?{}中是它的条件吗?
还有经常看到某个变量指向的是一个方法如关于上述的DataBean的addUserBean方法可以有如下语句:
DataBean db = new DataBean();
UserBean ub = new UserBean();
ub.setUserName(userName);
ub.setUserPwd(userPwd);
boolean result = db.addUserBean(ub);
还是不明白这样写的依据是什么,为什么上例变量result能够返回addUserBean()中的flag变量?

这个页面的完成时间已经很紧迫了,大家一定要救救我~可能是基础不是很好,也找不到合适的参考,只好请大家帮忙了,告诉我多少都行,对我都有帮助!
...全文
52 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
panzi667 2006-02-28
不晓得,
关注~
回复
IKARI_DALI 2006-02-28
仔细看了一下,发现是错在:
if(flag=true)
{requ.getSession().setAttribute("user",ub);
resp.sendRedirect("index.jsp");
}
else
{resp.sendRedirect("reg.htm");}


flag=true相当于是给flag赋值,所以程序当然无论如何都会运行requ.getSession().setAttribute("user",ub);和resp.sendRedirect("index.jsp"); 的,现在将if(flag=true)改成if(flag==true)或者if(flag),但是发觉结果却变成无论如何都无法转到index.jsp,自己感觉可能是错在DataBean.java的addUserBean()的这里:
{
prepar = conn.prepareStatement(sql);
prepar.setString(1, ub.getUsername());
prepar.setString(2, ub.getUserpwd());
int result = prepar.executeUpdate();
if (result > 0)
{
flag = true;

但是实在找不出错误,还是请大虾帮忙了~
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-02-27 11:25
社区公告
暂无公告