注冊,登陸的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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM