- 注解配置web.xml
@WebServlet(urlPatterns = "/login")
- 输出快捷方式
- JDBC连接测试
User
package com.pactrea.bean;
import lombok.Data;
@Data
public class User {
private Integer id;
private String email;
private String password;
public User() {
}
public User(Integer id, String email, String password) {
this.id = id;
this.email = email;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDao
package com.pactrea.dao;
import com.pactrea.bean.User;
import com.pactrea.utils.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public User getUserByEmlAndPwd(String email, String password) {
Connection conn = DBUtils.getConnection();
try {
PreparedStatement preparedStatement = conn.prepareStatement("select * from user where email = ? and password = ?");
preparedStatement.setString(1, email);
preparedStatement.setString(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
User user = new User(resultSet.getInt("id"), resultSet.getString("email"), resultSet.getString("password"));
return user;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
DBUtils
package com.pactrea.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtils {
private static final String NAME = "root";
private static final String PASSWORD = "";
private static final String URL = "jdbc:mysql://localhost:3306/shopping";
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 获取连接
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, NAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
IndexServlet
package com.pactrea.servlet;
import com.pactrea.bean.User;
import com.pactrea.dao.UserDao;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class IndexServlet extends HttpServlet {
@Override
public void init() {
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
// request.setAttribute("name", "张三");
//
// // 重定向 不带参数
try {
response.sendRedirect("jsp/index.jsp");
} catch (IOException e) {
e.printStackTrace();
}
//
// // 转发 带着信息转发
// try {
// request.getRequestDispatcher("jsp/index.jsp").forward(request, response);
// } catch (ServletException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("email");
String password = request.getParameter("password");
UserDao userDao = new UserDao();
User user = new User();
user = userDao.getUserByEmlAndPwd(username, password);
if (user != null) {
try {
request.getSession().setAttribute("currUser", user);
// request.setAttribute("currUser2", user);
response.sendRedirect("jsp/success.jsp");
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
response.sendRedirect("jsp/login.jsp?error=yes");
} catch (IOException e) {
e.printStackTrace();
}
}
// if (username.equals("zhangsan") && password.equals("123")) {
// try {
// request.setAttribute("username", username);
// request.setAttribute("password", password);
// request.getRequestDispatcher("jsp/success.jsp").forward(request, response);
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ServletException e) {
// e.printStackTrace();
// }
// } else {
// try {
response.sendRedirect("jsp/login.jsp?error=yes");
// response.sendRedirect("jsp/error.jsp");
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
@Override
public void destroy() {
}
}
success.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/8/19
Time: 13:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>success page</title>
</head>
<body>
<h1>SUCCESS!</h1>
<%--欢迎${username},密码是${password}--%>
pageScope:${pageScope.currUser}<br>
sessionScope:${sessionScope.currUser}<br>
currUser2:${currUser2}<br>
</body>
</html>