一步曲(html+servlet+SQL+Bean+Dao+加密(Base64))
设计思路:
1:编写前台页面jsp:register.jsp,使得用户能够实行注册操作
2:编写servlet:register.java,能够验证用户编写的信息进行合法化验证和注册
3:为了保护用户注册的数据能够好管理,进行必要的封装
4:在数据库操作方面就交给一个java类进行与数据库进行交互和操作
5:为了用户密码的安全,有必要对用户的密码进行加密(系统自带的加密技术)。
前期准备:
mysql 的jdbc驱动包
eclipse软件(已配备JDK)
tomcat软件
mysql软件
html:register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>注册界面</title>
</head>
<body>
<center>
<h1>注册界面</h1>
<hr>
<form action="Register" method="post" name="register" onsubmit="javascript:
if(document.register.username.value==''){
alert('请输入您的姓名!');
return false;
}
if(document.register.sex.value==''){
alert('请选择您的性别!');
return false;
}
if(document.register.userpwd.value==''){
alert('请输入您的密码!');
return false;
}
if(document.register.ID_number.value==''){
alert('请输入您的身份证号码!');
return false;
}
if(document.register.phone.value==''){
alert('请输入您的手机号码!');
return false;
}
if(document.register.email.value==''){
alert('请输入您的邮箱!');
return false;
}
if(document.register.home_address.value==''){
alert('请输入您的家庭住址!');
return false;
}
">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="sex" value="男" checked>男 <input
type="radio" name="sex" value="女">女</td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="userpwd"></td>
</tr>
<tr>
<td>身份证</td>
<td><input type="text" name="ID_number" maxlength="19"></td>
</tr>
<tr>
<td>手机:</td>
<td><input type="text" name="phone" size="11" maxlength="11">(11位)</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>住址:</td>
<td><input type="text" name="home_address"></td>
</tr>
<tr>
<td>状态:</td>
<td><input type="text" name="info_status" readOnly="readonly"
value="待审"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="reset" value="重置"> <input
type="submit" value="提交"></td>
</tr>
</table>
</form>
${msg}</center>
</body>
</html>
servlet:Register.java
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import dao.UserDao;
import util.Base64;
import bean.User;
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
public Register() {
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String sex = request.getParameter("sex");
String userpwd = request.getParameter("userpwd");
String ID_number = request.getParameter("ID_number");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String home_address = request.getParameter("home_address");
String info_status = request.getParameter("info_status");
/* 如果信息不完整提示信息不完整并且返回注册页面 */
if (username.equals("") || sex.equals("") || userpwd.equals("")
|| ID_number.equals("") || phone.equals("") || email.equals("")
||home_address.equals("")|| info_status.equals("")) {
request.setAttribute("msg", "Please add all message to submit!");
request.getRequestDispatcher("register.jsp").forward(request,
response);
} /* 如果注册信息完整继续执行注册环节 */
else {
String pwd_encrypt = "admin";
pwd_encrypt = Base64.getBase64(userpwd);
if (pwd_encrypt.equals("admin")) {
System.out.println(username + "'s password encrtpt failed!!");
request.setAttribute("msg", username
+ "'s password encrtpt failed!!");
request.getRequestDispatcher("register.jsp").forward(request,
response);
}
User user = new User(username, sex, userpwd, pwd_encrypt,
ID_number, phone, email, home_address, info_status);
int result = UserDao.setUser(user);
if (result > 0) {
System.out.println("Register user success!");
request.setAttribute("msg", "Register user success!");
request.getRequestDispatcher("login.jsp").forward(request,
response);
} else {
System.out.println("Register user failed!");
request.setAttribute("msg", "Register user failed!");
request.getRequestDispatcher("register.jsp").forward(request,
response);
}
}
}
}
Bean:User
package bean;
public class User {
private int uid;
private String username;
private String sex;
private String userpwd;
private String pwd_encrypt;
private String ID_number;
private String phone;
private String email;
private String home_address;
private String info_status;
public User() {
}
public User(String username, String sex, String userpwd,
String pwd_encrypt, String iD_number, String phone, String email,
String home_address, String info_status) {
this.username = username;
this.sex = sex;
this.userpwd = userpwd;
this.pwd_encrypt = pwd_encrypt;
ID_number = iD_number;
this.phone = phone;
this.email = email;
this.home_address = home_address;
this.info_status = info_status;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getPwd_encrypt() {
return pwd_encrypt;
}
public void setPwd_encrypt(String pwd_encrypt) {
this.pwd_encrypt = pwd_encrypt;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setID_number(String iD_number) {
ID_number = iD_number;
}
public String getID_number() {
return ID_number;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setHome_address(String home_address) {
this.home_address = home_address;
}
public String getHome_address() {
return home_address;
}
public void setInfo_status(String info_status) {
this.info_status = info_status;
}
public String getInfo_status() {
return info_status;
}
}
Dao:UserDao.java
package dao;
import java.sql.*;
import java.util.LinkedList;
import bean.User;
import connect.jdbcUtil;
public class UserDao {
public static int setUser(User user) {
String sql = "insert into `user`(username,userpwd,pwd_encrypt,sex,ID_number,phone,email,home_address,info_status) values(?,?,?,?,?,?,?,?,?)";
String parm[] = { user.getUsername(), user.getUserpwd(),
user.getPwd_encrypt(), user.getSex(), user.getID_number(),
user.getPhone(), user.getEmail(), user.getHome_address(),
user.getInfo_status() };
int x = jdbcUtil.doUpdate(sql, parm);
return x;
}
}
加密(Base64):Base64.java
package util;
import java.util.*;
import sun.misc.*;
public class Base64 {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
String pwd = cin.next();
String miwen = getBase64(pwd);
System.out.println("密文(" + miwen.length() + "位):" + miwen);
String mingwen = FromBase64(miwen);
System.out.println("明文(" + mingwen.length() + "位):" + mingwen);
}
}
public static String InBase64(String str) {
String result = getBase64(str);
return result;
}
public static String OutBase64(String str) {
String result = FromBase64(str);
return result;
}
// 加密
public static String getBase64(String str) {
byte[] b = new byte[20];
String s = null;
try {
b = str.getBytes("utf-8");
} catch (Exception e) {
e.printStackTrace();
}
if (b != null) {
s = new BASE64Encoder().encode(b);
}
return s;
}
// 解密
public static String FromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
数据库连接:jdbcUtil.java
package connect;
import java.sql.*;
public class jdbcUtil {
public static String url ="jdbc:mysql://localhost:3306/project?userUnicode=true&characterEncoding=UTF-8";
public static String user = "root";
public static String password = "78912312";
public static String driver = "com.mysql.jdbc.Driver";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs, Statement sm, Connection conn) {
try {
if (rs != null)
rs.close();
if (sm != null)
sm.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static ResultSet doQuery(String sql) {
Connection conn = getConnection();
if (conn == null)
return null;
Statement sm = null;
ResultSet rs = null;
try {
sm = conn.createStatement();
rs = sm.executeQuery(sql);
return rs;
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs == null && sm != null) {
close(null, sm, conn);
}
}
return rs;
}
public static int doUpdate(String sql, String[] paras) {
Connection conn = getConnection();
if (conn == null)
return 0;
PreparedStatement psm = null;
int result = 0;
try {
psm = conn.prepareStatement(sql);
for (int i = 0; i < paras.length; i++) {
psm.setString(i + 1, paras[i]);
}
result = psm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(null, psm, conn);
}
return result;
}
}
web.xml:(增加以下内容)
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>servlet.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/Register</url-pattern>
</servlet-mapping>
SQL:project.sql (MySQL)
结果:
文档结构图: