wuheng 2 anni fa
parent
commit
000a170f87

+ 21 - 0
day09/pom.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.lovecoding</groupId>
+    <artifactId>day09</artifactId>
+    <version>0.0.1</version>
+    <dependencies>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.30</version>
+        </dependency>
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis</artifactId>
+            <version>3.5.5</version>
+        </dependency>
+    </dependencies>
+</project>

+ 52 - 0
day09/src/com/lovecoding/admin/contorller/LoginServlet.java

@@ -0,0 +1,52 @@
+package com.lovecoding.admin.contorller;
+
+import com.lovecoding.admin.dao.UsersMapper;
+import com.lovecoding.admin.doman.Users;
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+
+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.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+@WebServlet("/admin/login")
+public class LoginServlet extends HttpServlet {
+
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+        //我们定义了 mybatis 配置文件路径
+        String realPath = req.getServletContext().getRealPath("")+"WEB-INF/mybatis-config.xml";
+        //我们用 Resources 去把配置文件转化为 数据流
+        InputStream resourceAsStream = new FileInputStream(realPath);
+        //我们用 mybatis SqlSessionFactoryBuilder 工厂类 去创建 SqlSessionFactory
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
+        //我们用 SqlSessionFactory 创建 SqlSession
+        SqlSession sqlSession = build.openSession();
+        //我们用 sqlsession 去加载 UserMapper.xml 的代理类
+        UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
+
+        String username = req.getParameter("username");
+        String password = req.getParameter("password");
+
+        System.out.println( username + password );
+
+        Users user = mapper.login(username, password);
+
+        if ( user != null && user.getUsername().equals(username) ) {
+            req.getSession(true).setAttribute("user", user );
+            resp.sendRedirect( req.getContextPath() + "/admin/home.jsp" );
+        } else {
+            resp.sendRedirect( req.getContextPath() + "/admin/login.jsp" );
+        }
+
+    }
+}

+ 4 - 0
day09/src/com/lovecoding/admin/contorller/RegServlet.java

@@ -0,0 +1,4 @@
+package com.lovecoding.admin.contorller;
+
+public class RegServlet {
+}

+ 10 - 0
day09/src/com/lovecoding/admin/dao/UsersMapper.java

@@ -0,0 +1,10 @@
+package com.lovecoding.admin.dao;
+
+import com.lovecoding.admin.doman.Users;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+
+public interface UsersMapper {
+    @Select( "SELECT * FROM tb_users WHERE username = #{username} AND `password` = #{password}" )
+    Users login(@Param("username") String username, @Param("password")  String password );
+}

+ 22 - 0
day09/src/com/lovecoding/admin/doman/Users.java

@@ -0,0 +1,22 @@
+package com.lovecoding.admin.doman;
+
+public class Users {
+    String username;
+    String password;
+
+    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;
+    }
+}

+ 4 - 1
day09/src/com/lovecoding/request/FileRespones.java

@@ -16,8 +16,11 @@ public class FileRespones extends HttpServlet{
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
         resp.setContentType("image/png");
+
+        String realPath = req.getServletContext().getRealPath("") + "/img/a.png";
+        System.out.println( realPath );
         //1. 我们获取到文件流
-        FileInputStream fis = new FileInputStream( "D:\\a.png" );
+        FileInputStream fis = new FileInputStream( realPath );
         //2. 获取response字节输出流
         ServletOutputStream os = resp.getOutputStream();
         //3. 完成流的copy

+ 2 - 0
day09/src/com/lovecoding/request/ResponsesServlet.java

@@ -23,6 +23,8 @@ public class ResponsesServlet extends HttpServlet {
 
         //resp.sendRedirect(  req.getContextPath() + "/index.jsp"  );
 
+
+
         resp.setStatus(301);
         resp.setHeader( "Location", "https:www.qq.com" );
 

+ 21 - 0
day09/web/WEB-INF/mybatis-config.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+        <!DOCTYPE configuration
+                PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+                "http://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+<environments default="default">
+    <environment id="default">
+        <transactionManager type="JDBC"></transactionManager>
+        <dataSource type="POOLED">
+            <property name="url" value="jdbc:mysql://127.0.0.1:3306/vip21?useSSL=false"/>
+            <property name="username" value="root"/>
+            <property name="password" value="123456"/>
+            <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
+        </dataSource>
+    </environment>
+</environments>
+
+    <mappers>
+        <mapper class="com.lovecoding.admin.dao.UsersMapper" />
+    </mappers>
+</configuration>

+ 16 - 0
day09/web/admin/home.jsp

@@ -0,0 +1,16 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: 武恒
+  Date: 2023/2/4
+  Time: 16:19
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+<head>
+    <title>Title</title>
+</head>
+<body>
+<h1> 欢迎登陆!!!! 登陆成功 </h1>
+</body>
+</html>

+ 30 - 0
day09/web/admin/login.jsp

@@ -0,0 +1,30 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: 武恒
+  Date: 2023/2/4
+  Time: 16:19
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width">
+    <title>欢迎登陆</title>
+</head>
+<body>
+<div style="    width: 300px;  margin: 0 auto;  margin-top: 200px;">
+    <form action="<%=request.getContextPath()+"/admin/login"%>" method="get">
+        <fieldset style=" height: 160px; padding: 20px;">
+            <legend>登陆后台管理系统</legend>
+            <p>用户名:<input type="text" name="username" style="width: 150px; height: 20px;" />  </p>
+            <p>密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" style="width: 150px; height: 20px;" />  </p>
+            <p><input type="submit" value="提交" style="width: 80px; height: 25px;float: right; margin-right: 30px;text-align: center;line-height: 25px;" /> </p>
+        </fieldset>
+    </form>
+    <p>祝您学习进步心想事成!!</p>
+</div>
+</body>
+</html>

+ 16 - 0
day09/web/admin/reg.jsp

@@ -0,0 +1,16 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: 武恒
+  Date: 2023/2/4
+  Time: 16:19
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+<head>
+    <title>Title</title>
+</head>
+<body>
+
+</body>
+</html>

+ 0 - 0
day09/web/a.png → day09/web/img/a.png