最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaWeb 網(wǎng)上書店 注冊和登陸功能案例詳解

 更新時間:2019年08月17日 09:23:49   作者:sun_jinhang  
這篇文章主要介紹了JavaWeb 網(wǎng)上書店 注冊和登陸功能,結(jié)合具體案例形式詳細分析了JavaWeb 網(wǎng)上書店 注冊和登陸功能具體實現(xiàn)步驟、操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了JavaWeb 網(wǎng)上書店 注冊和登陸功能。分享給大家供大家參考,具體如下:

工具:Eclipse + Navicat

源碼地址:https://github.com/Sunjinhang/JavaWeb

用戶實體:簡簡單單的六個屬性,編號、姓名、密碼、電話、郵箱、地址。

package Entity;
public class User {
    public User(String id, String userName, String password, String phone, String email, String address) {
        super();
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.phone = phone;
        this.email = email;
        this.address = address;
    }
    private String id;
    private String userName;
    private String password;
    private String phone;
    private String email;
    private String address;
    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 getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

用戶實現(xiàn)登陸注冊的一些方法:包含注冊、登陸、密碼MD5加密、編號隨機生成

package Service;
import java.security.MessageDigest;
import java.util.UUID;
import Dao.UserDao;
import Entity.User;
public class UserService extends UserDao{
    public void AddUser(User user) {
        user.setId(GetUId());
        user.setPassword(MD5Encode(user.getPassword()));
        Add(user);
    }
    public User ValidateLogin(String name,String password) {
        User user = Validate(name,MD5Encode(password));
        return user;
    }
    //自動給用戶生成編號
    public static String GetUId()
    {
        UUID uid = UUID.randomUUID();
        String id = uid.toString();
        id = id.replace("-", "");
        return id;
    }
    //給用戶密碼進行MD5加密
    public static String MD5Encode(String str)
    {
        StringBuffer code = new StringBuffer();
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        char[] charArr = str.toCharArray();
        byte[] byteArr = new byte[charArr.length];
        for(int i = 0;i < charArr.length; i++) {
            byteArr[i] = (byte)charArr[i];
        }
        byte[] md5Arr = md5.digest(byteArr);
        for(int i = 0;i < md5Arr.length; i++) {
            int value = (int)md5Arr[i] & 0xff;
            if(value < 16)
            {
                code.append("0");
            }
            code.append(Integer.toHexString(value));
        }
        return code.toString();
    }
}

注冊功能實現(xiàn):

靜態(tài)頁面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>注冊</title>
 </head>
 <body>
     <form action="${pageContext.request.contextPath }/RegisterServlet" method="post">
         <input type="text" placeholder="loginname" required="required" name="name"/>
         <br/>
         <br/>
         <input type ="password" placeholder="password" required="required" name="password"/>
         <br/>
         <br/>
         <input type ="password" placeholder="confirm password" required="required" name="confirmpassword"/>
         <br/>
         <br/>
         <input type ="text" placeholder="phone" required="required" name="phone"/>
         <br/>
         <br/>
         <input type ="text" placeholder="email" required="required" name="email"/>
         <br/>
         <br/>
         <input type ="text" placeholder="address" required="required" name="address"/>
         <br/>
         <br/>
         <input type ="submit" value="提交"/>
         <input type="button" value="返回登陸" οnclick="parent.location.href='${pageContext.request.contextPath }/client/head.jsp'">
     </form>
 </body>
</html>

代碼:

package Action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Entity.User;
import Service.UserService;
/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
  /**
   * @see HttpServlet#HttpServlet()
   */
  public RegisterServlet() {
    super();
    // TODO Auto-generated constructor stub
  }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        User user = new User("",name,password,phone,email,address);
        UserService userService = new UserService();
        try {
            userService.AddUser(user);
            request.setAttribute("message", "注冊成功");
            request.getRequestDispatcher("/message.jsp").forward(request, response);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

登陸功能實現(xiàn):

靜態(tài)頁面:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>前臺首頁</title>
 </head>
 <frameset rows="25%,*">
     <frame src="${pageContext.request.contextPath }/client/head.jsp" name="head">
 </frameset>
</html>

head.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>首頁頭</title>
 </head>
 <body style="text-align:center;">
  <h1>網(wǎng)上書店</h1>
  <br/>
  <div>
      <a href="${pageContext.request.contextPath }/client/IndexServlet?method=getAll" rel="external nofollow" target="body">首頁</a>
      <a href="${pageContext.request.contextPath }/client/listcart.jsp" rel="external nofollow" target="body">查看購物車</a>
      <a href="${pageContext.request.contextPath }/client/ClientListOrderServlet?userid=${user.id}" rel="external nofollow" target="body"">查看訂單</a>
  </div>
  <div style="float:right;">
      <c:if test="${user==null }">
      <form action="${pageContext.request.contextPath }/LoginServlet" method="post">
          用戶名:<input type="text" name="username" style="width:60px;">
          密碼:<input type="password" name="password" style="width:60px;">
          <input type="submit" value="登陸">
          <input type="button" value="注冊" οnclick="parent.location.href='${pageContext.request.contextPath }/client/register.jsp'">
      </form>
      </c:if>
      <c:if test="${user!=null }">
          歡迎您:${user.getUserName() } <a href="${pageContext.request.contextPath }/client/LoginOutServlet" rel="external nofollow" >注銷</a>
      </c:if>
  </div>
 </body>
</html>

代碼:

package Action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Entity.User;
import Service.UserService;
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(description = "處理登陸事項", urlPatterns = { "/LoginServlet" })
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
  /**
   * @see HttpServlet#HttpServlet()
   */
  public LoginServlet() {
    super();
    // TODO Auto-generated constructor stub
  }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserService service = new UserService();
        User user = service.ValidateLogin(username, password);
        if(user == null){
            request.setAttribute("message", "登陸失敗");
            request.getRequestDispatcher("/message.jsp").forward(request, response);
            return;
        }
        request.getSession().setAttribute("user", user);
        request.getRequestDispatcher("/client/head.jsp").forward(request, response);
    }
}

最終實現(xiàn)的效果:

主界面

 注冊界面:

登陸成功界面

 

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • idea不使用maven如何將項目打包

    idea不使用maven如何將項目打包

    使用IDEA 2021版本,不借助Maven進行打WAR包的步驟是:首先點擊Project Structure,然后點擊Artifacts,接著選擇需要的打包類型,設(shè)置好包的名稱,最后進行打包,這種方法適用于不使用Maven進行項目管理的情況
    2024-09-09
  • 如何解決Maven依賴無法導(dǎo)入的問題

    如何解決Maven依賴無法導(dǎo)入的問題

    本文介紹了如何通過在setting.xml中配置倉庫坐標和在IntelliJ IDEA中進行相關(guān)設(shè)置來提高Maven下載Jar包的速度,首先在setting.xml中找到mirrors標簽進行配置,然后在IntelliJ IDEA的設(shè)置中輸入特定的命令
    2024-10-10
  • jdbcTemplate使用方法實例解析

    jdbcTemplate使用方法實例解析

    這篇文章主要介紹了jdbcTemplate使用方法實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot 集成 activiti的示例代碼

    SpringBoot 集成 activiti的示例代碼

    這篇文章主要介紹了SpringBoot 集成 activiti的示例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • java 中模式匹配算法-KMP算法實例詳解

    java 中模式匹配算法-KMP算法實例詳解

    這篇文章主要介紹了java 中模式匹配算法-KMP算法實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 讓IntelliJ IDEA支持.vue文件的方法

    讓IntelliJ IDEA支持.vue文件的方法

    這篇文章主要介紹了讓IntelliJ IDEA支持.vue文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Spring與Struts整合之使用自動裝配操作示例

    Spring與Struts整合之使用自動裝配操作示例

    這篇文章主要介紹了Spring與Struts整合之使用自動裝配操作,結(jié)合實例形式詳細分析了Spring與Struts整合使用自動裝配具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2020-01-01
  • java根據(jù)本地IP獲取mac地址的方法

    java根據(jù)本地IP獲取mac地址的方法

    這篇文章主要為大家詳細介紹了java根據(jù)本地IP獲取mac地址的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Spring框架構(gòu)造注入操作實戰(zhàn)案例

    Spring框架構(gòu)造注入操作實戰(zhàn)案例

    這篇文章主要介紹了Spring框架構(gòu)造注入操作,結(jié)合具體實例形式分析了spring框架構(gòu)造輸入的相關(guān)定義與使用操作技巧,需要的朋友可以參考下
    2019-11-11
  • 如何寫好一個Spring組件的實現(xiàn)步驟

    如何寫好一個Spring組件的實現(xiàn)步驟

    這篇文章主要介紹了如何寫好一個Spring組件的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評論

和硕县| 司法| 新民市| 武威市| 东乌珠穆沁旗| 福贡县| 玛多县| 西畴县| 昭苏县| 安宁市| 沿河| 开远市| 邛崃市| 商河县| 瓦房店市| 东乌珠穆沁旗| 北宁市| 吐鲁番市| 太白县| 米易县| 包头市| 綦江县| 电白县| 襄城县| 宁城县| 禹州市| 广平县| 青川县| 交口县| 庆云县| 中超| 益阳市| 深泽县| 高密市| 弥勒县| SHOW| 黎川县| 丽江市| 城市| 崇文区| 永和县|