账号密码网站登录的开发

数据库设置

创建相关的数据库进行存储账号密码

 

1.设计一个网页,有表单,有两个输入框进行账号密码的输入,有登录按钮用来提交表单

zmLogin" method="post" enctype="multipart/form-data">



登录

 

2.添加一个script进行提交表单

 

3.创建一个Servlet类

@WebServlet("/user")public class Servlet extends HttpServlet{

private static final long serialVersionUID = 1L;//UserService类的对象

private UserService userService = new UserService();

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String func = request.getParameter("func");
switch(func) {
case "zmLogin":
zmLogin(request,response);
break;default:break;}
}
/*账号密码登录的表单
* 从表单中获取账号和密码
*调用service中的方法进行验证
*如果正确跳转到网页
*不正确刷新继续填写*/
private void zmLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

String username = request.getParameter("username");
String password = request.getParameter("password");

boolean isSuccess = userService.zmLogin(username,password);
if(isSuccess) {
//密码正确,跳转页面
response.sendRedirect("regist.jsp");
}else {
request.setAttribute("msg", "账号密码错误");

//错误跳转回登录页面
request.getRequestDispatcher("login1.jsp").forward(request, response);
}
}

 

4.创建一个Service类

/*拿到账号密码的值
* 将账号的值通过Dao的方法传输过去,查到该账号的相关信息
* 取出User对象中的密码值
*将对象的密码password1和客户填写的password进行对比
*如果两个相等则将isSuccess的值改为true
*传输回isSuccess的值*/
public boolean zmLogin(String username, String password) {
User user = userDao.zmLogin(username);
String password1 = user.getPassword();
boolean isSuccess = false;
if(password.equals(password1)) {
isSuccess = true;
}
return isSuccess;
}

5.创建一个Dao类

//账号密码验证登录
//通过账号查出对象的密码,封装到对象中,返回对象
public User zmLogin(String username) {
User user = null;
try {
user = qRunner.query("select password from user where username = ?",new BeanHandler(User.class),username);
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}

 

你可能感兴趣的:(账号密码网站登录的开发)