1.新建项目,选中JAVA或者JAVA Enterprise中的Web Application(每个人略有不同,总之在二者之一中找到Web Application即可):
2.新建libs文件夹,导入如下两个包,右键Add as Library
其中mysql包是连接数据库的Jar包,可自行下载和自己数据库版本对应的包,jstl( JSP Standard Tag Library jsp)简化我们写spring框架。
3.配置tomcat
在此处选择自己tomcat的安装路径
4.文件编码环节(实践课老师给的样例,如有雷同,纯属同门)`
model.user文件:
//User.java
public class User {
private int id;
private String name;
private String password;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
Service.UserService文件:
//UserService
public class UserService {
UserDao userDao = new UserDao();
//查找用户
public User SelectByName(String name){
return userDao.selectByName(name);
}
//插入一个新用户
public boolean Register(String name,String password,int age){
return userDao.InsertUser(name,password,age);
}
}
dao.UserDao文件:
//UserDao.java
public class UserDao {
public User selectByName(String name){
ResultSet rs = null;
Connection connection = null;
PreparedStatement pstmt = null;
DBUtil dbUtil = new DBUtil();
User user = new User();
try {
connection = dbUtil.getConnection();
pstmt = connection.prepareStatement("select * from user where name=?");
pstmt.setString(1,name);
rs = pstmt.executeQuery();
while(rs.next()){
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
user.setAge(rs.getInt(4));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
dbUtil.closeAll(rs,pstmt,connection);
}
return user;
}
public boolean InsertUser(String name,String password,int age){
Connection connection = null;
PreparedStatement pstmt = null;
try {
connection = DBUtil.getConnection();
pstmt = connection.prepareStatement("insert into user(name,password,age) values (?,?,?)");
pstmt.setString(1,name);
pstmt.setString(2,password);
pstmt.setInt(3,age);
pstmt.execute();
return true;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
ServLet.LoginServLet文件:
//loginServLet.java
public class LoginServLet extends HttpServlet {
UserService userService = new UserService();
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//如果点击的是登录按钮则获取数据
String name = req.getParameter("name");
String password = req.getParameter("password");
System.out.println("name:"+name);
User user = userService.SelectByName(name);
System.out.println(user);
//验证密码是否正确
if (password.equals(user.getPassword())) {
resp.getWriter().write("success");
System.out.println("success");
} else {
resp.getWriter().write("fail");
System.out.println("fail");
}
}
}
Utils.DBUtil文件:
//DBUtil.java
public class DBUtil {
public static Connection connection = null;
public static Connection getConnection() throws ClassNotFoundException, SQLException {
if(connection==null) {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&autoReconnect=true&serverTimezone=GMT", "root", "hxh19981225");
return connection;
}else {
return connection;
}
}
public static void closeAll(ResultSet rs, Statement statement, Connection connection){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
5.配置index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>servlet登录跳转</title>
</head>
<body>
<h1>登录界面</h1>
<form action="/login" method="post">
name:<input name="name" type="text">
password:<input name="password" type="password">
<input type="submit" value="登录">
</form>
</body>
</html>
6.配置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">
<!-- 登录界面-->
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.xs1701.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<!-- 注册界面-->
</web-app>