Web超简单入门(附带项目的讲解)

本文通过一个包含登录、注册和菜单页面的DemoWebPages项目,介绍了Web开发的基础,包括数据库配置、Java Web应用和JSP页面。项目已在GitHub公开,适合初学者了解Web开发流程。
摘要由CSDN通过智能技术生成

写在前面

DemoWebPages 是一个只有三个页面的网站,功能简单且清晰。具体有登录,注册和菜单三个页面。写这个小型项目的目的,一是想复习一下一年前学到的web的知识,二是想给刚学习web的同学提供一点微不足道的资源。博主也不是大牛,只是想把这个项目的过程记录下来,要是能帮助到别的喜欢web的同学就更好了。这个项目我已经放在了github上,欢迎大家去下载,别忘了点个⭐️哦。在DBConnect里,只要把xxxxx分别改成你的数据库的名字,用户名和密码就可以跑了。😃 我把代码都放上来了,不要看着有点多就觉得害怕,其实都有规律的😀
github链接

进入正题

配置

我写的配置是我自己在写这个web所用到的,并不是说写web非用它们不可哈😊

  1. MySQL
  2. TomCat
  3. 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() {
   
		
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您好!对于您提到的JavaWeb市管理系统,可以使用MySQL作为数据库进行数据存储和管理。MySQL是一个开源的关系型数据库管理系统,广泛应用于Web开发中。 在JavaWeb项目中使用MySQL,首先需要安装MySQL数据库,并创建相应的数据库和表。可以使用JDBC(Java Database Connectivity)来连接MySQL数据库并进行数据操作。JDBC是Java提供的一种用于与数据库进行交互的API。 您可以使用以下步骤来使用MySQL开发JavaWeb市管理系统: 1. 安装MySQL数据库:从MySQL官方网站下载并安装适合您操作系统的MySQL版本。 2. 创建数据库和表:使用MySQL客户端工具(如MySQL Workbench)连接到数据库服务器,创建一个新的数据库,并根据系统需求设计相应的表结构,如商品表、用户表等。 3. 导入JDBC驱动:将MySQL提供的JDBC驱动(如mysql-connector-java.jar)导入到项目的classpath中,以便在项目中使用JDBC连接MySQL数据库。 4. 编写Java代码:在JavaWeb项目中,通过编写Java代码使用JDBC连接和操作MySQL数据库。可以使用Java数据库访问技术如JDBC或者ORM框架如Hibernate等,根据需求选择合适的方式。 5. 实现功能:根据市管理系统的需求,编写相应的Java代码实现商品管理、用户管理、订单管理等功能。通过JDBC与MySQL数据库进行数据的增删改查操作。 当然,以上只是一个简单的概述,开发JavaWeb市管理系统还需要考虑到安全性、性能优化、用户界面等方面。希望对您有所帮助!如果您有具体的问题或者需要更详细的帮助,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值