---------------------- android培训、java培训、期待与您交流! ----------------------
用jsp做了一个登录和注册(连接数据库的),主要用到的技术是servlet,加上数据库访问者DAO,DAO里面有着验证用户的方法来实现用户登录,还有注册用户和验证用户是否被注册的方法,代码详解如下(部分):
DAO里登录方法:
public boolean validateLogin(String userName,String userPwd){
boolean bool=false;
java.sql.PreparedStatement ps=null;
java.sql.ResultSet rs=null;
String sql="select * from userTable where name=? and pwd=?";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, userPwd);
rs=ps.executeQuery();
if(rs.next())
bool=true;// 有相同的用户名
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBManager.closeDBObject(conn);
}
return bool;
}
servlet控制用户登录:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
response.setCharacterEncoding("gbk");
String name=request.getParameter("username");
String pwd=request.getParameter("userpwd");
UserDAO dao=new UserDAO();
boolean bool = dao.validateLogin(name, pwd);
if(bool==true){
HttpSession sess12=request.getSession();
Vector vec= (Vector)sess12.getAttribute("logins");
if(vec!=null){
response.sendRedirect("resLogin.jsp");
}else{
HttpSession ses=request.getSession();
ses.setAttribute("loginUser", name);
Vector loginVec=new Vector();
loginVec.addElement(name);
HttpSession sess=request.getSession();
sess.setAttribute("logins", loginVec);
PointDAO dao3=new PointDAO();
List l=dao3.getPoint(name);
HttpSession sess2=request.getSession();
sess2.setAttribute("mypoint", l);
response.sendRedirect("getAllImg");
}
}else{
response.sendRedirect("loginError.jsp");
}
}
DAO里用户注册方法:
public int regUser(String username,String userpwd,String usertel,String useraddress,String userpeople){
int info=0;
PreparedStatement ps=null;
String sql="insert into userTable values(?,?,?,?,?)";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, userpwd);
ps.setString(3, usertel);
ps.setString(4, useraddress);
ps.setString(5, userpeople);
info=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBManager.closeDBObject(conn);
}
return info;
}
DAO里验证用户是否被注册方法:
public boolean validateReg(String userName){
boolean bool=true;
java.sql.PreparedStatement ps=null;
java.sql.ResultSet rs=null;
String sql="select * from userTable where name=?";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, userName);
rs=ps.executeQuery();
if(rs.next())
bool=false;// 有相同的用户名
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBManager.closeDBObject(conn);
}
return bool;
}
servlet控制用户注册:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
response.setCharacterEncoding("gbk");
String userName=request.getParameter("username");
String userPwd=request.getParameter("userpwd1");
String userTel=request.getParameter("usertel");
String userAddress=request.getParameter("useraddress");
String userLove=request.getParameter("userlove");
HttpSession sess=request.getSession();
sess.setAttribute("name", userName);
UserDAO dao=new UserDAO();
boolean bool=dao.validateReg(userName);
if(bool){
UserDAO dao1=new UserDAO();
int info=0;
info=dao1.regUser(userName, userPwd,userTel,userAddress,userLove);
System.out.print("regServlet");
if(info!=0){
PointDAO dao3=new PointDAO();
int i=dao3.insertPoint(userName, 0);
response.sendRedirect("regSuccess.jsp?name=${name}");
}else{
response.sendRedirect("regError.jsp");
}
}else{
response.sendRedirect("reged.jsp?name=${name}");//用户名已被注册,跳转到已被注册用户页面,并把用户名作为参数传给页面
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------