做一个简单登录页面的步骤

1新建一个Maven工程

选择自己想要创建的模板

一个简单的登录页面(思路和代码资源都有)_sql

2加载完成以后设置最新的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>

3建立自己的项目结构

勾选和检查下面这些选项,然后启动Tomcat

一个简单的登录页面(思路和代码资源都有)_sql_02

自己的域名、Dao直接和数据库进行操作、service操作Dao里面已经做好的操作、servlet调用service,servlet写好以后要用wed.xml进行注册

4配置自己的Tomcat并启动

一个简单的登录页面(思路和代码资源都有)_xml_03

一个简单的登录页面(思路和代码资源都有)_sql_04

Tomcat正常启动就进行下一步

4自己设计一个用户表单,一般简单一点

注意字符编码级,和主键的设置

一个简单的登录页面(思路和代码资源都有)_sql_05

5在idea进行数据库的连接

按照下面的顺序去进行连接

一个简单的登录页面(思路和代码资源都有)_数据库_06

一个简单的登录页面(思路和代码资源都有)_数据库_07

配置好了以后就可以查看自己的数据库表了

一个简单的登录页面(思路和代码资源都有)_数据库_08

6配置数据库的连接

在resources包下面建立db.properties

一个简单的登录页面(思路和代码资源都有)_数据库_09

db.properties里面放置一会连接需要的数据

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc? //3306 useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username=root
password=123456

7根据数据库表格设计好自己的实体类,放在pojo包下面

有几个数据库就建立几个类

一个简单的登录页面(思路和代码资源都有)_数据库_10

以uers为例

package com.mac.pojo;

import java.util.Date;

public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private Integer age;//年龄

private String userRoleName; //用户角色名称


public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getAge() {
/*long time = System.currentTimeMillis()-birthday.getTime();
Integer age = Long.valueOf(time/365/24/60/60/1000).IntegerValue();*/
Date date = new Date();
Integer age = date.getYear()-birthday.getYear();
return age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
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 Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}

一个简单的登录页面(思路和代码资源都有)_数据库_11

8在Dao包下面建立连接数据库的公共类

1.对应着db.properties里面的数据建立自己的私有变量

private static String driver;
private static String url;
private static String username;
private static String password;

2.建立一个静态代码块,在类加载之前就完成一下功能

//静态代码块,类加载的时候就初始化
static {
//通过类加载器读取对应的资源
Properties properties = new Properties();
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}

//得到值
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
}

3.建立一个获取数据库连接的方法,放回一个数据库连接

//获取数据库的连接
public static Connection getConnection() throws Exception {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, username, password);
return connection;
}

4.编写查询公共类、增删改工具类的方法,查询返回result,增删改返回int

都需要进行参数的传递

就是得到service传过来的数据, 这里值进行和数据库对接,SQL语句不编写

//编写查询公共类
public static ResultSet excute(Connection connection,PreparedStatement preparedStatement, ResultSet resultSet,String sql,Object[] params ) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i <params.length ; i++) {
preparedStatement.setObject(i+1,params[i]);
}

resultSet = preparedStatement.executeQuery();
return resultSet;
}

//编写增删改的公共方法
public static int excute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement ) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i <params.length ; i++) {
preparedStatement.setObject(i+1,params[i]);
}

int updateRows = preparedStatement.executeUpdate();
return updateRows;
}

5.释放资源

//释放资源
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
boolean flag=true;
if (resultSet!=null){
try {
resultSet.close();
resultSet=null;
} catch (SQLException sqlException) {
sqlException.printStackTrace();
flag=false;
}
}
if (preparedStatement!=null){
try {
preparedStatement.close();
preparedStatement=null;
} catch (SQLException sqlException) {
sqlException.printStackTrace();
flag=false;
}
}
if (connection!=null){
try {
connection.close();
connection=null;
} catch (SQLException sqlException) {
sqlException.printStackTrace();
flag=false;
}
}
return false;
}

9在Dao下面建立一个和可以通过BaseDao拿到数据库中要登录用户的包,

一个简单的登录页面(思路和代码资源都有)_数据库_12

1.首先思考我们要用什么条件判定去的到这个用户,参数需要什么,(连接,用户名,密码)。这里一般抛出一个SQl异常好一点

package com.mac.dao.User;

import com.mac.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;

public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection,String userCode) throws SQLException;

}

2.怎么得到一个用户呢?根据传入的条件去找出是哪一个用户(需要sql),然后把这个查出来的数据全放到提前的实体类里面,就相当于拿出来了一个用户。然后放回一个实体类对象

public class UserDaoImpl implements UserDao{
public User getLoginUser(Connection connection, String userCode) throws SQLException {
ResultSet rs=null;
PreparedStatement pstm=null;
String sql=null;
User user = null;

if (connection!=null){
sql="select * from smbms_user where userCode=?";
Object[] params={userCode};
rs= BaseDao.excute(connection, pstm, rs, sql, params);
if (rs.next()){
user=new User();
user.setId(rs.getInt("id"));
user.setUserCode(rs.getString("userCode"));
user.setUserName(rs.getString("userName"));
user.setUserPassword(rs.getString("userPassword"));
user.setGender(rs.getInt("gender"));
user.setBirthday(rs.getDate("birthday"));
user.setPhone(rs.getString("phone"));
user.setAddress(rs.getString("address"));
user.setUserRole(rs.getInt("userRole"));
user.setCreatedBy(rs.getInt("createdBy"));
user.setCreationDate(rs.getTimestamp("creationDate"));
user.setModifyBy(rs.getInt("modifyBy"));
user.setModifyDate(rs.getTimestamp("modifyDate"));
}
BaseDao.closeResource(null,pstm,rs);
}
return user;
}

这样Dao层去数据库拿到一个用户就ok了

10到了Service去使用Dao层刚才的方法,这里要考虑我们的参数是我们从前端拿到的东西,需要去判断的

1.还是和dao层一样的,我们把属于一类的东西放在一起,这样方便我们查看

一个简单的登录页面(思路和代码资源都有)_xml_13

2.编写一个UserService的接口,用来规范我们的UserServiceImpl里面需要的方法。

package com.mac.service.user;

import com.mac.pojo.User;

import java.sql.SQLException;

public interface UserService {
//用户登录;
public User Login(String userCode,String password) throws Exception;

}

2,这一层需要的东西呢,就是service那层我们编写方法放回的参数类型

public class UserServiceImpl implements UserService{
//业务层都会调Dao层,所以我们要引入Dao层
private UserDao userDao;
public UserServiceImpl() {
userDao=new UserDaoImpl();
}

//这里的东西是前端转给我们的
public User Login(String userCode, String password) throws Exception {
Connection connection=null;
User user=null;

connection = BaseDao.getConnection();
//通过业务层调用具体的数据库操作
user = userDao.getLoginUser(connection, userCode);
BaseDao.closeResource(connection,null,null);
return user;
}

数据库在哪里打开的就在哪里关闭,在这里关闭。相当于前面那些数据库的连接就是从这里开始的。

11到了servlet操作前端数据传输给service数据,并且实现前端页面的跳转

这一层的思路就是,通过前端的name获得前端的值,用req.getParameter("前端对应的name");去接收值,传给service去进行比对对,成功了以后就得到这个用户了,把这个用户放到网站的session中,然后进行页面的跳转

public class LoginServlet extends HttpServlet {
//Servlet:控制层,调用业务层代码



@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//获取前端用户名和密码,记得对比名字
String userCode = req.getParameter("userCode");
String userPassword = req.getParameter("userPassword");

//和数据库中的密码进行对比,调用业务层
UserService userService = new UserServiceImpl();
try {
User user = userService.Login(userCode, userPassword); //这里就把前端和后端的人对应了

if(user!=null){ //查有此人,可以登录
//将用户的信息放到session中
req.getSession().setAttribute(Constants.USER_SESSION,user);
//跳转到内部主页
resp.sendRedirect("jsp/frame.jsp");
}else {
//没有这个人,无法登录,放回当前页面,提示错误
req.getSession().setAttribute("error","密码错了,请联系小周");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}

//代码成功,去注册,记得看前端的请求地址

} catch (Exception e) {
e.printStackTrace();
}
}

12wed.xml里面进行注册,(这个注册的地址要根据登页面提交的地址来)

<!--    注册第一个登录功能-->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.mac.servlet.user.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>

一个简单的登录页面(思路和代码资源都有)_xml_14

这样一个简单登录页面就成功了,后期我会把源码放到这里,可以下载下来自己玩玩