注册
以下代码实现网上购物商城的用户注册和登录功能。
register.jsp(在WebContent目录下创建)
<%@ 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;" method="post" action="/shop2/user">
<input type="hidden" name="method" value="register">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-6">
<input type="text" name="username" class="form-control" id="username"
placeholder="请输入用户名">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" id="inputPassword3"
placeholder="请输入密码">
</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" name="emial" class="form-control" id="inputEmail3"
placeholder="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" name="name" class="form-control" id="usercaption"
placeholder="请输入姓名">
</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="1">
男
</label> <label class="radio-inline"> <input type="radio"
name="sex" id="inlineRadio2" value="0">
女
</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" name="birthday" class="form-control">
</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>
login.jsp(在WebContent目录下创建)
<%@ 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>oracle商城信息展示</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>
</head>
<body>
<div class="container-fluid">
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<div class="container-fluid">
<div class="main_con">
<h2>公司简介</h2>
<hr/>
<div>
<p>中公教育集团创建于1999年,经过十余年潜心发展.</p>
<p>中公教育已由一家北大毕业生自主创业的信息技术与教育服务机构.</p>
<p>发展为教育服务业的综合性企业集团,</p>
<p>成为集合面授教学培训、网校远程教育、图书教材及音像制品的出版发行于一体的大型知识产业实体.</p>
</div>
</div>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</body>
</html>
User.java(JavaBean 在src/com.oracle.bean目录下创建)
package com.oracle.bean;
public class User {
private String uid;
private String username;
private String password;
private String name;
private String email;
private String telephone;
private String birthday;
private String sex;
private int state;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
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 getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public User(String uid, String username, String password, String name, String email, String telephone,
String birthday, String sex, int state) {
super();
this.uid = uid;
this.username = username;
this.password = password;
this.name = name;
this.email = email;
this.telephone = telephone;
this.birthday = birthday;
this.sex = sex;
this.state = state;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
UserServlet.java(在src/com.oracle.servlet目录下创建)
package com.oracle.servlet;
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 com.oracle.bean.User;
import com.oracle.service.UserService;
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if("register".equals(method)) {
register(request, response);
}else if("login".equals(method)) {
login(request, response);
}
}
public void login(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
String username = request.getParameter("username");
String password = request.getParameter("password");
UserService us = new UserService();
User user = us.login(username, password);
if(user!=null) {
//request.getRequestDispatcher("/index.jsp").forward(request, response);
response.sendRedirect("/shop/index.jsp");
}else {
request.setAttribute("msg", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
try {
request.getRequestDispatcher("/error.jsp").forward(request, response);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void register(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
Map<String, String[]> map = request.getParameterMap();
User user = new User();
BeanUtils.populate(user, map);
UUID uid = UUID.randomUUID();
user.setUid(uid.toString());
user.setState(0);
UserService us = new UserService();
us.register(user);
request.getRequestDispatcher("/login.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
UserService.java(在src/com.oracle.service目录下创建)
package com.oracle.service;
import java.sql.SQLException;
import com.oracle.bean.User;
import com.oracle.dao.UserDao;
public class UserService {
public void register(User user) throws SQLException {
// TODO Auto-generated method stub
UserDao ud = new UserDao();
ud.register(user);
}
public User login(String username, String password) throws SQLException {
// TODO Auto-generated method stub
UserDao ud = new UserDao();
User user = ud.login(username, password);
return user;
}
}
UserDao.java(在src/com.oracle.dao目录下创建)
package com.oracle.dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.oracle.bean.User;
import com.oracle.utils.DataSourceUtils;
public class UserDao {
public void register(User user) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
//编写sql
String sql = "insert into user values(?,?,?,?,?,?,?,?,?)";
//执行sql
qr.update(sql, user.getUid(), user.getUsername(), user.getPassword(), user.getName(), user.getEmail(), user.getTelephone(), user.getBirthday(), user.getSex(), user.getState());
}
public User login(String username, String password) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
//编写sql
String sql = "select * from user where username=? and password=? and state=?";
//执行sql
User user = qr.query(sql, new BeanHandler<>(User.class), username, password, 0);
return user;
}
}