展开全部
jsp登录注册页面都需要查询和插入数据库的,还要检查注册信e68a84e8a2ad62616964757a686964616f31333339653763息存不存在。
完整例子如下:
用户信息的bean:
package chen;
public class UserBean
{
private String userid;
private String password;
public void setUserId(String userid)
{
this.userid=userid;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUserId()
{
return this.userid;
}
public String getPassword()
{
return this.password;
}
}
提交数据库的bean:
package chen;
import com.mysql.jdbc.Driver;
import java.sql.*;
public class UserRegister
{
private UserBean userBean;
private Connection con;
//获得数据库连接。
public UserRegister()
{
String url="jdbc:mysql://localhost/"+"chao"+"?user="+"root"+"&password="+"850629";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//设置待注册的用户信息。
public void setUserBean(UserBean userBean)
{
this.userBean=userBean;
}
//进行注册
public void regist() throws Exception
{
String reg="insert into userinfo(userid,password) values(?,?)";
try
{
PreparedStatement pstmt=con.prepareStatement(reg);
pstmt.setString(1,userBean.getUserId());
pstmt.setString(2,userBean.getPassword());
pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
}
提交注册数据进入数据库:
import="chen.*" %>
用户信息注册页面String userid =request.getParameter("userid");
String password = request.getParameter("password");
userBean.setUserId(userid);
userBean.setPassword(password);
System.out.println(userid+password);
%>
regist.setUserBean(userBean);
out.println(userid);
regist.regist();
out.println("注册成功");}
catch(Exception e){
out.println(e.getMessage());
}
%>
登陆验证页面:
String userid1=new String(request.getParameter("userid"));
String password1=new String(request.getParameter("password"));
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/chao","root","850629");
Statement stmt=con.createStatement();
String sql="select * from userinfo where userid='"+userid1+"';";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
{String password=new String(rs.getString("password"));
if(password.equals(password1))
{session.setAttribute("userid1",userid1);
response.sendRedirect("sucess.jsp");
}
else
{response.sendRedirect("login.jsp");
}
}
else
{response.sendRedirect("login.jsp");
}
%>
登陆页面:
输入用户名: | |
输入密码: | |
注册页面:
注册新用户
用户名: |
密码: |
登陆成功页面:
sucess
登录成功,欢迎您!
本回答由电脑网络分类达人 李孝忠推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起