1.创建数据库:user表及其字段ID,username,password及其属性
ID | int| 主键 | 自动增长
username| varchar| 非空
password | varchar| 非空
记得添加javax.servlet-api不然创建不了servlet,参考3
参考: lidea创建maven的web项目
详细教程关注我,后续会补上!
项目结构如下:(ps:项目结构不够严谨,wonderful包需要改成Servlet包比较好点)
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!--mysql的jar-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
<scope>compile</scope>
</dependency>
<!--c3p0的jar-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
<scope>compile</scope>
</dependency>
<!--dbutil-->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.5</version>
</dependency>
package domain;
import lombok.*;
/**
* @author :wonderful_wzf
* @message:
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/19,14:50
*/
@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
}
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///wonderful
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">10</property>
<property name="maxPoolSize">20</property>
</default-config>
<named-config name="mysql">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///wonderful
<property name="user">root</property>
<property name="password">root</property>
</named-config>
</c3p0-config>
C3p0Demo.class
package Utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @author :wonderful_wzf
* @message:使用c3p0连接池
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/19,11:46
*/
public class C3p0Demo {
private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
public static Connection getConnection() throws SQLException {
return comboPooledDataSource.getConnection();
}
//返回DataSource
public static DataSource getDataSource() throws SQLException{
return comboPooledDataSource;
}
}
ps:不建议实现I在前代表实现,而是impl在后代表实现
package dao;
import domain.User;
/**
* @author :wonderful_wzf
* @message:针对user的dao接口
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/19,14:56
*/
public interface UserDao {
/**
* 判断用户登录
* @param username
* @return
*/
User userLogin(String username);
}
package dao.impl;
import Utils.C3p0Demo;
import dao.UserDao;
import domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.SQLException;
/**
* @author :wonderful_wzf
* @message:dao层针对user对数据库操作
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/19,14:56
*/
public class IUserDao implements UserDao {
@Override
public User userLogin(String username) {
try {
QueryRunner queryRunner = new QueryRunner(C3p0Demo.getDataSource());
String sql = "select * from user where username = ?";
return queryRunner.query(sql, new BeanHandler<User>(User.class),username);
} catch (SQLException e) {
//抛出一个新的异常
throw new RuntimeException(e);
}
}
}
package service;
import domain.User;
/**
* @author :wonderful_wzf
* @message:针对用户的Service层接口
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/19,14:58
*/
public interface UserService {
User userLogin(String username);
}
package service.impl;
import dao.UserDao;
import dao.impl.IUserDao;
import domain.User;
import service.UserService;
/**
* @author :wonderful_wzf
* @message:针对user的Service实现
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/19,14:58
*/
public class IUserService implements UserService {
/**
* Service层依赖Dao层
*/
UserDao userDao = new IUserDao();
@Override
public User userLogin(String username) {
return userDao.userLogin(username);
}
}
package wonderful;
import domain.User;
import service.UserService;
import service.impl.IUserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author :wonderful_wzf
* @message:一个Servlet 实现判断用户登录
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/19,11:00
*/
public class UserServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//请求设置编码格式 浏览器到服务器
request.setCharacterEncoding("utf-8");
//防止中文乱码 设置响应编码 服务器到浏览器
response.setContentType("text/html;charset=UTF-8");
/**
* 别忘记使用Filter中文编码过滤器欧,不然乱码
* 依赖Service层
*/
UserService userService = new IUserService();
/**
* 获取页面输入内容
*/
String username = request.getParameter("username");
String password = request.getParameter("password");
//到数据库进行判断 查出来了数据 证明当前用户名存在 没查出来当前用户不存在
User user = userService.userLogin(username);
if (user!=null){
if (password.equals(user.getPassword())){
//登录成功 跳转页面 记录用户名 重定向
request.getSession().setAttribute("user",user);
response.sendRedirect("index.jsp");
}
else{
//密码错误 回到登录 携带错误信息
request.setAttribute("errorInfo","密码错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
else {
//用户不存在 回到登录 携带错误信息
request.setAttribute("errorInfo","用户不存在");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>wonderful.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
</web-app>
login.jsp
<%--
Created by IntelliJ IDEA.
User: wonderful
Date: 2020/5/19
Time: 11:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>实训第二天servlet讲解</title>
</head>
<body>
<%--记住用绝对路径欧,不然项目跳着跳着就404了--%>
<form action="${pageContext.request.contextPath}/userServlet" method="post" style="width: 300px;margin: 50px auto;">
<span style="color: red" >${errorInfo}</span><br>
姓名:<input type="text" placeholder="请输入用户名" name="username"><br>
密码:<input type="text" placeholder="密码" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
参考: lidea创建web项目配置Tomcat
详细教程关注我,后续会补上!
祝你好运
现在已经是0:03分了,明天接着做个目录,在完善这个。
自己感动自己,你们也要加油欧!
7.编写EncodingFilter中文编码过滤器
参考: EncodingFilter中文编码过滤器