写在前面
DemoWebPages 是一个只有三个页面的网站,功能简单且清晰。具体有登录,注册和菜单三个页面。写这个小型项目的目的,一是想复习一下一年前学到的web的知识,二是想给刚学习web的同学提供一点微不足道的资源。博主也不是大牛,只是想把这个项目的过程记录下来,要是能帮助到别的喜欢web的同学就更好了。这个项目我已经放在了github上,欢迎大家去下载,别忘了点个⭐️哦。在DBConnect里,只要把xxxxx分别改成你的数据库的名字,用户名和密码就可以跑了。😃 我把代码都放上来了,不要看着有点多就觉得害怕,其实都有规律的😀
github链接
进入正题
配置
我写的配置是我自己在写这个web所用到的,并不是说写web非用它们不可哈😊
- MySQL
- TomCat
- Eclipse
eclipse 需要配置 tomcat, 还要导入一个 mysql-connector-java 的jar包,具体怎么做的这里就不多说了,网上有很多详细的资料。这里也附上两篇写得不错的教程。
配置Tomcat
mysql驱动包
项目介绍
虽然DemoWebPages目前只有注册和登录两个页面,但是很多web也需要这两个功能和页面,所以我觉得用这个小项目来认识web会是一个不错的选择。DemoWebPages的一个主要逻辑是,员工也可以是顾客。带着这个中心思想,我们来看一下ER Model:
可以从图中知道,DemoWebPages有三个entity, 分别是person, employee和guest,而employee和guest是继承person的。用继承的关系是因为,无论是员工还是顾客,注册时所需填的信息有很大一部分是一样的,所以创建一个person,可以避免代码的重复。通过ER Model,我们可以知道在数据库中有哪些表,每张表的属性有哪些,以及主键。那这里很明显,我的数据库里有三张表,分别是person,guest 和employee。而这三张表的主键都是email。我是手动在数据库里创建了三张表,当然创建表这一操作也可以用代码实现。先附上创表的sql语句:
create table person(firstname char(10),lastname char(10),phone char(10),address char(20),email char(20) primary key,password char(20));
create table employee(email char(10) primary key,position char(10),location char(20));
create table guest(email har(10) primary key);
到这里,已经配置好了eclipse,也创好了表,可以写一些功能了。
具体操作
【包的创建】
如图,我创建了三个包,分别是connect,entities和servlet。那为什么要创建这3个包呢?我们先想想,我们是怎么使用web的。往简单去想,就是用户在注册页面注册成功后就可以进行其他操作,比如网上购物。那也就是说,这个web的后台拿到了这个用户的注册信息,然后把这个用户的资料存在数据库里。当用户进行一些操作比如购物时,就可以从表里调出这个用户的信息,然后为这个用户进行一些操作,比如积分。到这里我们大概知道了,首先肯定要有一个用户包,里面包括用户的各种属性,比如名字,电话。那对应的就是entities。所以,entities 也是和数据库里的表所对应的。然后,需要一个包负责连接数据库,并且进行数据的增删改查。比如说注册成功了,那就要把这个用户的资料添加到数据库里。那对应的就是connect。那最后,还需要一个包处理业务逻辑。比如说,注册成功后可以跳转到购物页面。那对应的就是servlet。
【entity】
从最简单的说起,entity对应数据库里的表。因为我设想中数据库会有三张表,所以这里也会有三个entity。上代码。
package entities;
/**
* The Person program implements a person entity.
*
* @author:
* @version: V1.0
* @since: 2021-01-2
**/
public class Person {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;
private String password;
/**
* This is the default constructor which creates a person with default attributes.
* @param Nothing.
* @return Nothing.
**/
public Person() {
firstName = "";
lastName = "";
address = "";
email = "";
phone = "";
password = "";
}
/**
* This is the constructor which creates a person.
* @param firstName The firstName of the new guest.
* @param lastName The lastName of the new guest.
* @param phone The phone of the new guest.
* @param address The address of the new guest.
* @param email The email of the new guest.
* @param password The password of the new guest.
* @return Nothing.
**/
public Person(String firstName,String lastName,String phone,String address,String email,String password ) {
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.address = address;
this.email = email;
this.password = password;
}
/**
* This function returns the email of person.
* @param Nothing.
* @return String Ruturns the email of person.
**/
public String getEmail() {
return email;
}
/**
* This function sets the email of person.
* @param email The email of a person.
* @return Nothing.
**/
public void setEmail(String email) {
this.email = email;
}
/**
* This function returns the password of person.
* @param Nothing.
* @return String Ruturns the password of person.
**/
public String getPassword() {
return password;
}
/**
* This function sets the password of person.
* @param password The password of a person.
* @return Nothing.
**/
public void setPassword(String password) {
this.password = password;
}
/**
* This function returns the first name of person.
* @param Nothing.
* @return String Ruturns the first name of person.
**/
public String getFirstName() {
return firstName;
}
/**
* This function sets the first name of person.
* @param firstName The first name of a person.
* @return Nothing.
**/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* This function returns the last name of person.
* @param Nothing.
* @return String Ruturns the last name of person.
**/
public String getLastName() {
return lastName;
}
/**
* This function sets the last name of person.
* @param lastName The last name of a person.
* @return Nothing.
**/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* This function returns the address of person.
* @param Nothing.
* @return String Ruturns the address of person.
**/
public String getAddress() {
return address;
}
/**
* This function sets the address of person.
* @param address The address of a person.
* @return Nothing.
**/
public void setAddress(String address) {
this.address = address;
}
/**
* This function returns the phone number of person.
* @param Nothing.
* @return String Ruturns the phone number of person.
**/
public String getPhone() {
return phone;
}
/**
* This function sets the phone number of person.
* @param phone The phone number of a person.
* @return Nothing.
**/
public void setPhone(String phone) {
this.phone = phone;
}
}
package entities;
/**
* The Employee program implements an employee entity.
*
* @author:
* @version: V1.0
* @since: 2021-01-2
**/
public class Employee extends Person{
private String email;
private String position;
private String location;
/**
* This is the default constructor which creates an employee with default attributes.
* @param Nothing.
* @return Nothing.
**/
public Employee() {
email="";
position = "";
location = "";
}
/**
* This is the constructor which creates an employee.
* @param email The email of the new employee.
* @param position The position of the new employee.
* @param location The location of the new employee.
* @return Nothing.
**/
public Employee(String email,String position,String location) {
this.email = email;
this.position = position;
this.location = location;
}
@Override
/**
* This function returns the email of employee.
* @param Nothing.
* @return String Ruturns the email of employee.
**/
public String getEmail() {
return email;
}
@Override
/**
* This function sets the email of employee.
* @param email The email of an employee.
* @return Nothing.
**/
public void setEmail(String email) {
this.email = email;
}
/**
* This function returns the position of employee.
* @param Nothing.
* @return String Ruturns the position of employee.
**/
public String getPosition() {
return position;
}
/**
* This function sets the position of employee.
* @param position The position of an employee.
* @return Nothing.
**/
public void setPosition(String position) {
this.position = position;
}
/**
* This function returns the location of employee.
* @param Nothing.
* @return String Ruturns the location of employee.
**/
public String getLocation() {
return location;
}
/**
* This function sets the location of employee.
* @param location The location of an employee.
* @return Nothing.
**/
public void setLocation(String location) {
this.location = location;
}
}
package entities;
/**
* The Guest program implements a guest entity.
*
* @author:
* @version: V1.0
* @since: 2021-01-2
**/
public class Guest extends Person{
private String email;
/**
* This is the default constructor which creates a guest with default attributes.
* @param Nothing.
* @return Nothing.
**/
public Guest() {
email="";
}
/**
* This is the constructor which creates a guest.
* @param email The email of the new guest.
* @return Nothing.
**/
public Guest(String email) {
this.email = email;
}
@Override
/**
* This function returns the email of guest.
* @param Nothing.
* @return String Ruturns the email of guest.
**/
public String getEmail() {
return email;
}
@Override
/**
* This function sets the email of guest.
* @param email The email of a guest.
* @return Nothing.
**/
public void setEmail(String email) {
this.email = email;
}
}
【connect】
正如前面所说,这里主要实现数据库的连接,以及对数据库中的表进行增删改查的工作。我这里只写了插入和查找两个功能,其它功能比如删除也是同理写的。在DBConnect里,只要把xxxxx分别改成你的数据库的名字,用户名和密码就可以跑了。
package connect;
import java.sql.*;
/**
* The DBConnect program implements a mySql connection simply
* connect the database and close it.
*
* @author:
* @version: V1.0
* @since: 2021-01-2
**/
public class DBConnect {
private Connection db = null;
private PreparedStatement preparedstatement = null;
private Statement statement = null;
private ResultSet resultset = null;
/**
* This is the constructor which loans the driver and connects the database.
* @param Nothing.
* @return Nothing.
* @exception Exception e on serve connection error.
**/
public DBConnect() {
final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost:3306/XXXX";
final String USER = "XXXXX";
final String PASS = "XXXXX";
try {
Class.forName(JDBC_DRIVER); // loan the driver
db = DriverManager.getConnection(DB_URL,USER,PASS);
} catch (Exception e) {
System.out.println("Server connection error.");
e.printStackTrace();
closeDB();
}
}
/**
* This function returns the connection.
* @param Nothing.
* @return connection This returns the connection.
**/
public Connection getConnection() {
return db;}
/**
* This function closes the connection.
* @param Nothing.
* @return Nothing.
* @exception SQLException e on serve closing error.
**/
public void closeDB() {