一,完成数据库编写
DROP DATABASE if EXISTS mydb;CREATE DATABASE mydb;use mydb;CREATE TABLE ta_user(uid int primary key auto_increment,username varchar(32),password varchar(32),phone varchar(32),address varchar(32));INSERT INTO ta_user(username,password,phone,address)VALUES('张三','123','12323231','北京');INSERT INTO ta_user(username,password,phone,address)VALUES('李四','456','35343434','上海');INSERT INTO ta_user(username,password,phone,address)VALUES('王五','789','33234324','河南');SELECT * from ta_user;
二,网页前端显示部分(HTML部分)
注:注意文件创建地方
分层思想:创建一些包将一大堆代码分散到各个包中,可以使代码简洁,容易后期维护
- 主页面
首页 去登陆 - 登录页面
登录页面 账号:
密码:
没有账号?前往注册返回首页 - 注册页面
注册 账号:
密码:
号码:
地址:
有账号?前往登录返回首页 - 错误页面
Title 错误返回登录页面 - 登陆成功后跳转页面
商品 苹果 2 橘子 6 香蕉 5
三,后端控制部分(Java部分)
bean包用来放属性的构造方法
package com.smrsar.bean;public class User {private Integer uid;private String username;private String password;private String phone;private String address;public User() {}@Overridepublic String toString() {return "User{" +"uid=" + uid +", username='" + username + '\'' +", password='" + password + '\'' +", phone='" + phone + '\'' +", address='" + address + '\'' +'}';}public Integer getUid() {return uid;}public void setUid(Integer uid) {this.uid = uid;}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 getAddress() {return address;}public void setAddress(String address) {this.address = address;}public User(Integer uid, String username, String password, String phone, String address) {this.uid = uid;this.username = username;this.password = password;this.phone = phone;this.address = address;}}
package com.smrsar.bean;public class Userz {private Integer uid;private String username;private String password;public Userz() {}@Overridepublic String toString() {return "Userz{" +"uid=" + uid +", username='" + username + '\'' +", password='" + password + '\'' +'}';}public Integer getUid() {return uid;}public void setUid(Integer uid) {this.uid = uid;}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 Userz(Integer uid, String username, String password) {this.uid = uid;this.username = username;this.password = password;}}
dao包用来放Java代码(注册部分的JDBC代码,分层方法:构建成一个方法,在Servlet中直接调用)
package com.smrsar.dao;import com.smrsar.bean.User;import com.smrsar.util.JDBCUtil;import java.sql.Connection;import java.sql.PreparedStatement;public class UserDaoImpl {public int register (User user){Connection con = null;PreparedStatement pstm = null;int row = 0;try {//1,获取数据库连接con=JDBCUtil.GetCon();//2,定义sql语句String sql = "insert into ta_user(username,password,phone,address)values (" />
JDBC工具类构造方法用来调用
package com.smrsar.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class JDBCUtil {private static String driver = "com.mysql.cj.jdbc.Driver";private static String url = "jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC";private static String username = "root";private static String password = "root";public static Connection con = null;public static Connection GetCon() {try {Class.forName(driver);con = DriverManager.getConnection(url, username, password);} catch (Exception e) {e.printStackTrace();}return con;}public static void Close(ResultSet rs, PreparedStatement pstm, Connection con) {try {if (rs != null) {rs.close();}if (pstm != null) {pstm.close();}if (con != null) {con.close();}} catch (Exception e) {e.printStackTrace();}}}
登录和注册servlet部分代码
package com.smrsar.servlet;import com.smrsar.bean.Userz;import com.smrsar.util.JDBCUtil;//import com.smrsar.util.JDBCUtil;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 java.io.IOException;import java.sql.*;@WebServlet("/login")//使用这种方式可以不在xml文件中配置Servlet类也能实现跳转public class Login extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置编码req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");resp.setContentType("ext/html;charset=UTF-8");//请求获取数据String username = req.getParameter("username");String password = req.getParameter("password");Connection con = null;PreparedStatement pstm = null;ResultSet rs = null;Userz login = null;try {//调用已编写的JDBC工具类内容con = JDBCUtil.GetCon();//定义sql语句String sql = "select * from ta_user where username=? and password=?"; //获取预处理对象pstm = con.prepareStatement(sql);//传参pstm.setObject(1, username);pstm.setObject(2, password);//执行查询rs = pstm.executeQuery();if (rs.next()) {login = new Userz();login.setUid(rs.getInt("uid"));login.setUsername(rs.getString("username"));login.setPassword(rs.getString("password"));}} catch (Exception e) {e.printStackTrace();} finally {//调用已编写的JDBC工具类内容JDBCUtil.Close(rs, pstm, con);}//判断是否为空,跳转到对应页面if (login != null) {resp.sendRedirect("goods.jsp");} else {resp.sendRedirect("error.jsp");}}}
package com.smrsar.servlet;import com.smrsar.bean.User;import com.smrsar.dao.UserDaoImpl;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 java.io.IOException;@WebServlet("/register")public class Register extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1,设置编码req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");resp.setContentType("ext/html;charset=UTF-8");//2,获取请求参数String username = req.getParameter("username");String password = req.getParameter("password");String phone = req.getParameter("phone");String address = req.getParameter("address");//3,封装,把前面的变量封装到User对象中User user = new User();user.setUsername(username);user.setPassword(password);user.setPhone(phone);user.setAddress(address);System.out.println(user);//4,实例化对象后调用使用UserDaoImpl userDao=new UserDaoImpl();int row=userDao.register(user);//5,作出响应if (row > 0) {//成功跳转到登录页面resp.sendRedirect("login.jsp");} else {//注册失败跳转到注册页resp.sendRedirect("register.jsp");}}}