linux怎么查看本机内存大小
211
2023-06-19
JavaWeb 网上书店 注册和登陆功能案例详解
本文实例讲述了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"%>
代码:
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"%>
head.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
用户名:
密码:
欢迎您:${user.getUserName() } 注销
代码:
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程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~