JavaWeb 網(wǎng)上書店 注冊和登陸功能案例詳解
本文實(shí)例講述了JavaWeb 網(wǎng)上書店 注冊和登陸功能。分享給大家供大家參考,具體如下:
工具:Eclipse + Navicat
源碼地址:https://github.com/Sunjinhang/JavaWeb
用戶實(shí)體:簡簡單單的六個(gè)屬性,編號(hào)、姓名、密碼、電話、郵箱、地址。
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;
}
}
用戶實(shí)現(xiàn)登陸注冊的一些方法:包含注冊、登陸、密碼MD5加密、編號(hào)隨機(jī)生成
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;
}
//自動(dòng)給用戶生成編號(hào)
public static String GetUId()
{
UUID uid = UUID.randomUUID();
String id = uid.toString();
id = id.replace("-", "");
return id;
}
//給用戶密碼進(jìn)行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();
}
}
注冊功能實(shí)現(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();
}
}
}
登陸功能實(shí)現(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>前臺(tái)首頁</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 = "處理登陸事項(xiàng)", 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);
}
}
最終實(shí)現(xiàn)的效果:
主界面

注冊界面:

登陸成功界面

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- javaweb實(shí)現(xiàn)注冊登錄頁面
- JavaWeb之Servlet注冊頁面的實(shí)現(xiàn)示例
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能(服務(wù)器)
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能
- JavaWeb實(shí)戰(zhàn)之用Servlet+JDBC實(shí)現(xiàn)用戶登錄與注冊
- JavaWeb簡單用戶登錄注冊實(shí)例代碼(有驗(yàn)證碼)
- JavaWeb表單注冊界面的實(shí)現(xiàn)方法
- JavaWeb實(shí)現(xiàn)用戶登錄注冊功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- Servlet+JavaBean+JSP打造Java Web注冊與登錄功能
- Java?web實(shí)現(xiàn)簡單注冊功能
相關(guān)文章
Spring與Struts整合之使用自動(dòng)裝配操作示例
這篇文章主要介紹了Spring與Struts整合之使用自動(dòng)裝配操作,結(jié)合實(shí)例形式詳細(xì)分析了Spring與Struts整合使用自動(dòng)裝配具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-01-01
Spring框架構(gòu)造注入操作實(shí)戰(zhàn)案例
這篇文章主要介紹了Spring框架構(gòu)造注入操作,結(jié)合具體實(shí)例形式分析了spring框架構(gòu)造輸入的相關(guān)定義與使用操作技巧,需要的朋友可以參考下2019-11-11
如何寫好一個(gè)Spring組件的實(shí)現(xiàn)步驟
這篇文章主要介紹了如何寫好一個(gè)Spring組件的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

