这个小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"%><!DOCTYPE html><html><head></head><meta name="viewport" content="width=device-width, initial-scale=1"><title>会员注册</title><link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /><script src="js/jquery-1.11.3.min.js" type="text/javascript"></script><script src="js/bootstrap.min.js" type="text/javascript"></script><!-- 引入自定义css文件 style.css --><link rel="stylesheet" href="css/style.css" type="text/css" /><style>body {margin-top: 20px;margin: 0 auto;}.carousel-inner .item img {width: 100%;height: 300px;}font {color: #3164af;font-size: 18px;font-weight: normal;padding: 0 10px;}</style></head><body><!-- 引入header.jsp --><jsp:include page="/header.jsp"></jsp:include><div class="container"style="width: 100%; background: url('image/regist_bg.jpg');"><div class="row"><div class="col-md-2"></div><div class="col-md-8"style="background: #fff; padding: 40px 80px; margin: 30px; border: 7px solid #ccc;"><font>会员注册</font>USER REGISTER<form class="form-horizontal" style="margin-top: 5px;"action="/WWW/register" method="post"><div class="form-group"><label for="username" class="col-sm-2 control-label">用户名</label><div class="col-sm-6"><input type="text" class="form-control" id="username"placeholder="请输入用户名" name="username"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">密码</label><div class="col-sm-6"><input type="password" class="form-control" id="inputPassword3"placeholder="请输入密码" name="password"></div></div><div class="form-group"><label for="confirmpwd" class="col-sm-2 control-label">确认密码</label><div class="col-sm-6"><input type="password" class="form-control" id="confirmpwd"placeholder="请输入确认密码"></div></div><div class="form-group"><label for="inputEmail3" class="col-sm-2 control-label">Email</label><div class="col-sm-6"><input type="email" class="form-control" id="inputEmail3"placeholder="Email" name="email"></div></div><div class="form-group"><label for="usercaption" class="col-sm-2 control-label">姓名</label><div class="col-sm-6"><input type="text" class="form-control" id="usercaption"placeholder="请输入姓名" name="name"></div></div><div class="form-group opt"><label for="inlineRadio1" class="col-sm-2 control-label">性别</label><div class="col-sm-6"><label class="radio-inline"> <input type="radio"name="sex" id="inlineRadio1" value="male">男</label> <label class="radio-inline"> <input type="radio"name="sex" id="inlineRadio2" value="female">女</label></div></div><div class="form-group"><label for="date" class="col-sm-2 control-label">出生日期</label><div class="col-sm-6"><input type="date" class="form-control" name="birthday"></div></div><div class="form-group"><label for="date" class="col-sm-2 control-label">验证码</label><div class="col-sm-3"><input type="text" class="form-control"></div><div class="col-sm-2"><img src="./image/captcha.jhtml" /></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><input type="submit" width="100" value="注册" name="submit"style="background: url('./images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;"></div></div></form></div><div class="col-md-2"></div></div></div><!-- 引入footer.jsp --><jsp:include page="/footer.jsp"></jsp:include></body></html>

注册页面是使用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<String, String[]> 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"%><!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"><title>会员登录</title><link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /><script src="js/jquery-1.11.3.min.js" type="text/javascript"></script><script src="js/bootstrap.min.js" type="text/javascript"></script><!-- 引入自定义css文件 style.css --><link rel="stylesheet" href="css/style.css" type="text/css" /><style>body {margin-top: 20px;margin: 0 auto;}.carousel-inner .item img {width: 100%;height: 300px;}.container .row div {/* position:relative;float:left; */}font {color: #666;font-size: 22px;font-weight: normal;padding-right: 17px;}</style></head><body><!-- 引入header.jsp --><jsp:include page="/header.jsp"></jsp:include><div class="container"style="width: 100%; height: 460px; background: #FF2C4C url('images/loginbg.jpg') no-repeat;"><div class="row"><div class="col-md-7"><!--<img src="./image/login.jpg" width="500" height="330" alt="会员登录" title="会员登录">--></div><div class="col-md-5"><divstyle="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;"><font>会员登录</font>USER LOGIN<div> </div><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo") %>><form class="form-horizontal" action="/WWW/login" method="post"><div class="form-group"><label for="username" class="col-sm-2 control-label">用户名</label><div class="col-sm-6"><input type="text" class="form-control" id="username"placeholder="请输入用户名" name="username"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">密码</label><div class="col-sm-6"><input type="password" class="form-control" id="inputPassword3"placeholder="请输入密码" name="password"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">验证码</label><div class="col-sm-3"><input type="text" class="form-control" id="inputPassword3"placeholder="请输入验证码"></div><div class="col-sm-3"><img src="./image/captcha.jhtml" /></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><div class="checkbox"><label> <input type="checkbox"> 自动登录</label> <label> <inputtype="checkbox"> 记住用户名</label></div></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><input type="submit" width="100" value="登录" name="submit"style="background: url('./images/login.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;"></div></div></form></div></div></div></div><!-- 引入footer.jsp --><jsp:include page="/footer.jsp"></jsp:include></body></html>

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>(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;}