web项目开发,网站登录页面常用逻辑设计,登录失败之后如何处理 ...

摘要:  这个小Domo的大概思路就是,有一个注册表单页面(register.jsp),注册表单注册提交的数据提交给个servlet(register),这个servlet将注册的表单写入数据库,若写入成功则重定向到登陆页面(login.jsp),此时可以按照 ...

这个小Domo的大概思路就是,有一个注册表单页面(register.jsp),注册表单注册提交的数据提交给个servlet(register),这个servlet将注册的表单写入数据库,若写入成功则重定向到登陆页面(login.jsp),此时可以按照刚才注册的登陆,登陆也是一个表单,它也提交到一个servlet(login)若用户名密码输入正确则跳转到首页(index.jsp)表示登陆成功,若登陆失败则在登陆页面(login.jsp)上显示密码错误

代码演示

注册页面代码 register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>会员注册
会员注册USER REGISTER

注册页面是使用bootStrap的,默认没有name,我们要为其加上name

register

package com.huanfeng.cn;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.sql.SQLException;import java.util.Map;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.dbutils.QueryRunner;public class RegisterServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");Map parameterMap = request.getParameterMap();User user=new User();try {BeanUtils.populate(user,parameterMap);user.setId(UUID.randomUUID().toString());} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}regist(user);response.sendRedirect(request.getContextPath()+"/login.jsp");}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}public void regist(User user) {QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());String sql="insert into user values(?,?,?,?,?,?,?,?,?,?)";try {qr.update(sql,user.getId(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),null,user.getBirthday(),user.getSex(),null,null);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

核心代码

web项目开发,网站登录页面常用逻辑设计,登录失败之后如何处理

web项目开发,网站登录页面常用逻辑设计,登录失败之后如何处理

login.jsp登陆页面

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>会员登录
会员登录USER LOGIN
<%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo") %>>
web项目开发,网站登录页面常用逻辑设计,登录失败之后如何处理

login.jsp登陆之后会跳转到login这个servlet,这个servlet会去数据库查询,如果有,则登陆成功,如果没有则回到login.jsp,此时

web项目开发,网站登录页面常用逻辑设计,登录失败之后如何处理

起作用显示密码找不到

login

package com.huanfeng.cn;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;public class login extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");String password =request.getParameter("password");User user=login(username,password);if(user!=null) {response.sendRedirect("/index.jsp");}else {request.setAttribute("loginInfo","密码错误");request.getRequestDispatcher("/login.jsp").forward(request,response);}}private User login(String username, String password) {QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());String sql="select * from user where username=? and password=?";try {User user=qr.query(sql, new BeanHandler(User.class),username,password);return user;} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException();}}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}

核心代码

web项目开发,网站登录页面常用逻辑设计,登录失败之后如何处理

数据库代码

CREATE TABLE `user` (`uid` VARCHAR(3mybaseuser) NOT NULL,`username` VARCHAR(20) DEFAULT NULL,`password` VARCHAR(20) DEFAULT NULL,`name` VARCHAR(20) DEFAULT NULL,`email` VARCHAR(30) DEFAULT NULL,`telephone` VARCHAR(20) DEFAULT NULL,`birthday` VARCHAR(20) DEFAULT NULL,`sex` VARCHAR(10) DEFAULT NULL,`state` INT(11) DEFAULT NULL,`code` VARCHAR(64) DEFAULT NULL,PRIMARY KEY (`uid`));

UserBean

package com.huanfeng.cn;public class User {private String id;public User() {super();}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}private String username;private String password;private String name;private String email;private String sex;private String birthday;}

如果文章对你有帮助,欢迎点击上方按钮打赏作者

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

返回顶部