<%@ include file="top.jsp" %>
<%@ include file="bottom.jsp" %>
Style.css
body{
margin:0px;
padding:0px;
font-size: 9pt;
}
#box{
margin:0 auto auto auto;
width:800px;
clear:both;
background-color: #FFFFFF;
}
a:hover {
font-size: 9pt;
color: #FF4400;
}
a {
font-size: 9pt;
text-decoration: none;
color: #3C404D;
}
wghFunction.js
//判定用户名
function checkeUser(user){
var str=user;
//在JavaScript中,正则表达式只能使用首字母开头
var Expression=/^[A-Z][a-z]{3,15}$/;
var objExp=new RegExp(Expression); //创建正则表达式对象
if(objExp.test(str)==true){ //通过正则表达式验证
return true;
}else{
return false;
}
}
//判断密码
function checkePwd(user){
var str=user;
var Expression=/^[A-Za-z]{1}([A-Za-z0-9]|[._]){8,30}$/;
var objExp=new RegExp(Expression); //创建正则表达式对象
if(objExp.test(str)==true){ //通过正则表达式验证
return true;
}else{
return false;
}
}
//验证E-mail地址
function checkemail(email){
var str=email;
var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
var objExp=new RegExp(Expression); //创建正则表达式对象
if(objExp.test(str)==true){ //通过正则表达式进行验证
return true;
}else{
return false;
}
}
2.后端部分
User.java
package com.bean;
public class User {
private int id = 0;// 用户ID
private String username = "";// 用户名
private String pwd = "";// 密码
private String email = "";// E-mail地址
private String question = "";// 密码提示问题
private String answer = "";// 密码提示问题答案
private String city = "";// 所在地
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
CustomDao.java(接口)
package com.dao;
import com.bean.*;
import com.connection.*;
public interface CustomerDao {
public void add(User u);//添加用户
}
CustomDaoer.java
package com.dao;
import java.sql.SQLException;
import com.bean.User;
import com.connection.DButil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class CustomerDaoer implements CustomerDao {
@Override
public void add(User u) {
// TODO Auto-generated method stub
Connection conn=DButil.open();
String sql="insert into user(username,pwd,email,city,question,answer) values(?,?,?,?,?,?)";
try {
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
pst.setString(1, u.getUsername());
pst.setString(2, u.getPwd());
pst.setString(3, u.getEmail());
pst.setString(4, u.getCity());
pst.setString(5, u.getQuestion());
pst.setString(6, u.getAnswer());
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
DButil.close(conn);
}
}
}
ConnectLogin.java
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.connection.DButil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class ConnectLogin {
public int login(String username,String pwd)
{
int ID=0;
Connection conn=DButil.open();
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select id from user where username=? and pwd=?";
try {
pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,pwd);
rs=pstmt.executeQuery();
if(rs.next())
{
ID=rs.getInt("id");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ID;
}
}
Register.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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 com.bean.User;
import com.dao.CustomerDao;
import com.dao.CustomerDaoer;
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
public Register() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");
String username = request.getParameter("user"); // 获取用户名
String pwd = request.getParameter("pwd"); // 获取密码
String email = request.getParameter("email"); // 获取E-mail地址
String city = request.getParameter("city"); // 获取地址
String question = request.getParameter("question"); // 获取密码提示问题
String answer = request.getParameter("answer"); // 获取密码提示问题答案
PrintWriter out = response.getWriter();
if(username!=""&&pwd!=""&&email!=""&&city!=""&&question!=""&&answer!="")
{
CustomerDao dao=new CustomerDaoer();
User u=new User();
u.setUsername(username);
u.setPwd(pwd);
u.setEmail(email);
u.setCity(city);
u.setQuestion(question);
u.setAnswer(answer);
dao.add(u);
RequestDispatcher view=request.getRequestDispatcher("ReSuss.jsp");
view.forward(request, response);
}
else{
RequestDispatcher view=request.getRequestDispatcher("ReFail.jsp");
view.forward(request, response);
}
}
}
login.java
package com.servlet;
import com.dao.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet {
private static final long serialVersionUID = 1L;
public login() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");
String username=request.getParameter("username");
String password=request.getParameter("pwd");
PrintWriter out=response.getWriter();
ConnectLogin dao=new ConnectLogin();
int ID=dao.login(username, password);
if(ID!=0)
{
RequestDispatcher view=request.getRequestDispatcher("LoSuss.jsp");
view.forward(request, response);
}
else
{
RequestDispatcher view=request.getRequestDispatcher("LoFail.jsp");
view.forward(request, response);
}
}
}
测试截图: