JavaWeb 网上书店 注册和登陆功能案例详解
人气:0本文实例讲述了JavaWeb 网上书店 注册和登陆功能。分享给大家供大家参考,具体如下:
工具: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; } }
用户实现登陆注册的一些方法:包含注册、登陆、密码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(); } }
注册功能实现:
静态页面:
<%@ 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(); } } }
登陆功能实现:
静态页面:
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>网上书店</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); } }
最终实现的效果:
主界面
注册界面:
登陆成功界面
更多java相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
您可能感兴趣的文章:
加载全部内容