时间:2021-05-19
本文实例讲述了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程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1、网上书店与传统书店相比较,有哪些优点:网上书店相比较来说更便捷、而且可以内更快的、更准确的找到自己容所需要的书、还便宜。2、网上书店有哪些缺点:质量不能保证
网上书店将来可能取代实体书店,因为网上书店方便快捷,书种齐全,现在实体书店就比以前少多了,将来可能没有实体书店。电商提成方案扣除推广和产品成本1、打开手机淘宝2
亚马逊网上书店的特点如下:1、亚马逊网上书店的创立背景特别亚马逊网上书店的创始人意识到网络营销的广阔前景,于是辞去华尔街对冲基金经理的职务,创办亚马逊公司。继而
新华书店是全国范围的最大的书店,目前已有部分省、市开通了新华书店网上书店,如浙江、上海、沈阳等。网上书店最老的应该是当当,目前亚马逊、苏宁易购等平台均有书籍网购
我觉得网上书店有可能会取代现实的实体书店,因为现在网购已经成为大趋势,而且网上书店更加方便,也更加的便宜。农产品销售微信小程序和微信商城之间的区别1、操作方法不