注册,登陆的Servlet、dao、service、domain、jsp页面、
登陆的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"> <div style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;"> <font>会员登录</font>USER LOGIN
//提示验证码,密码或用户名错误的地方 <div style="color:red"><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo") %></div>
<form class="form-horizontal" action="/Market/LoginServlet" 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" name="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" class="form-control" id="inputPassword3" name="password" placeholder="请输入密码"> </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="请输入验证码" name="checkcode"> </div> <div class="col-sm-3"> <img src="/Market/CheckImgServlet" onclick="checkImg(this)" /> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="checkbox"> <label> <input type="checkbox"> 自动登录 </label> <label> <input type="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> <script type="text/javascript"> function checkImg(obj){ obj.src="/Market/CheckImgServlet?time="+new Date().getTime(); } </script>
登陆的Servlet代码:
package com.oracle.demo01; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import service.UsersService; public class LoginServlet extends HttpServlet { private UsersService usersService=new UsersService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决中文乱码 request.setCharacterEncoding("utf-8"); //获取用户名和密码 String username=request.getParameter("username"); String password=request.getParameter("password"); //获取验证码 String checkcode=request.getParameter("checkcode"); //获取Session域中的验证码 HttpSession session=request.getSession(); String code=(String)session.getAttribute("checkcode_session"); if(!checkcode.equals(code)){ request.setAttribute("loginInfo","验证码错误" ); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } //获取Service的方法 int count=usersService.loginUser(username, password); if(count>0){ response.sendRedirect(request.getContextPath()); }else{ //请求转发 request.setAttribute("loginInfo", "用户名或密码错误"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
注册的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> <script type="text/javascript"> function checkImg(){ obj.src = "/Store/CheckImgServlet?time ="+Math.random(); } </script> <!-- <script type="text/javascript"> function checkname(obj){ var name=document.getElementById("username").value; location.href="/Market/CheckUsernameServlet?" } </script> --> </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 <div style="color:red"><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo") %></div> <form class="form-horizontal" style="margin-top: 5px;" action="/Market/RegisterServlet" 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="sex1" value="male"> 男 </label> <label class="radio-inline"> <input type="radio" name="sex" id="sex2" 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" name="checkCode" placeholder="请输入验证码"> </div> <div class="col-sm-2"> <img src="/Market/CheckImgServlet" onclick="checkImg(this)" /> </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> <script type="text/javascript"> function checkImg(obj){ obj.src="/Market/CheckImgServlet?time="+new Date().getTime(); } </script>
注册的Servlet代码
package com.oracle.demo01; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.UUID; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.beanutils.BeanUtils; import domain.Users; import service.UsersService; public class RegisterServlet extends HttpServlet { private UsersService userService=new UsersService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("utf-8");//解决中文乱码 Map<String,String[]> map = request.getParameterMap(); Users user=new Users(); //BeanUtils工作原理:将map中的数据根据key与实体中的属性的对应关系封装,只要key的名字与实体中的属性名字一样,就能封装到实体中 try { BeanUtils.populate(user, map); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } //手动封装没有的数据 //UUID---------随机不重复的32位字符串---java代码生成后是36位 user.setUid(UUID.randomUUID().toString()); String username=request.getParameter("username"); //获取验证码 String checkcode=request.getParameter("checkCode"); //获取Session域中的验证码 HttpSession session=request.getSession(); String code=(String)session.getAttribute("checkcode_session"); if(!checkcode.equals(code)){ request.setAttribute("loginInfo","验证码错误,请重新输入" ); request.getRequestDispatcher("/register.jsp").forward(request, response);//跳转 return; } //调用Service的注册方法 int row = userService.register(user); if(row>0){ response.sendRedirect(request.getContextPath()+"/login.jsp"); }else{ response.sendRedirect(request.getContextPath()+"/register.jsp"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
注册的domain代码:
package domain; public class Users { 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; private String code; 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 String getCode() { return code; } public void setCode(String code) { this.code = code; } @Override public String toString() { return "Users [uid=" + uid + ", username=" + username + ", password=" + password + ", name=" + name + ", email=" + email + ", telephone=" + telephone + ", birthday=" + birthday + ", sex=" + sex + ", state=" + state + ", code=" + code + "]"; } }
注册的Dao代码
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import domain.Users; import tools.JDBCUtils; public class UsersDao { //注册 public int register(Users user) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="insert into users(uid,username,password,name,email,birthday,sex) values(?,?,?,?,?,?,?)"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1,user.getUid()); pst.setString(2, user.getUsername()); pst.setString(3, user.getPassword()); pst.setString(4, user.getName()); pst.setString(5, user.getEmail()); pst.setString(6, user.getBirthday()); pst.setString(7, user.getSex()); int row = pst.executeUpdate(); return row; } //登陆 public int loginUser(String username,String password) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from users where username=? and password=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, username); pst.setString(2, password); ResultSet rs=pst.executeQuery(); int count=0; while(rs.next()){ count=rs.getInt(1); } JDBCUtils.close(conn, pst, rs); return count; } }
注册的service代码:
package service; import java.sql.SQLException; import dao.UsersDao; import domain.Users; public class UsersService { private UsersDao userDao=new UsersDao(); //注册 public int register(Users user){ int row=0; try { row=userDao.register(user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return row; } //登陆 public int loginUser(String username,String password){ int count=0; try { count=userDao.loginUser(username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return count; } }
web。xml代码
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Market</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <servlet-name>CheckImgServlet</servlet-name> <servlet-class>yanzhengma.CheckImgServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckImgServlet</servlet-name> <url-pattern>/CheckImgServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <servlet-name>RegisterServlet</servlet-name> <servlet-class>com.oracle.demo01.RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/RegisterServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.oracle.demo01.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>CheckUsername</display-name> <servlet-name>CheckUsername</servlet-name> <servlet-class>com.oracle.demo01.CheckUsername</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckUsername</servlet-name> <url-pattern>/CheckUsername</url-pattern> </servlet-mapping> </web-app>