diff --git a/README.md b/README.md index c4520c6c23f8c3cd6d1cf424abf6264297fe2308..f1d0c8d811e48397f19f69728fe3fdeb3a3bb490 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ #### 完成内容列表 1. 搭建环境 -2. xxxx +2. 完成修改 3. xxxx 4. xxxx diff --git a/com/pom.xml b/com/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..a49a9f5bae09c4595e958bd29699962f24ff5d67 --- /dev/null +++ b/com/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.example + com + 1.0-SNAPSHOT + com + war + + + 1.8 + 1.8 + 5.7.0 + + + + + javax + javaee-api + 8.0.1 + provided + + + org.junit.jupiter + junit-jupiter-api + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + mysql + mysql-connector-java + 8.0.11 + + + + + + + org.apache.maven.plugins + maven-war-plugin + 3.3.0 + + + + \ No newline at end of file diff --git a/com/src/main/java/LoginFilter.java b/com/src/main/java/LoginFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..20be22fba156012b2caba4ffdbd0fd2587a29eda --- /dev/null +++ b/com/src/main/java/LoginFilter.java @@ -0,0 +1,39 @@ +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; + +public class LoginFilter implements Filter { + + public void doFilter(ServletRequest request, + ServletResponse response, + FilterChain chain) throws IOException, ServletException { + HttpServletRequest hrequest = (HttpServletRequest)request; + HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response); + String user = null; + if(hrequest.getSession(false) != null){ + + user = ( String ) hrequest.getSession(false).getAttribute("username"); + } + if (hrequest.getRequestURI().contains("index")) { + // 对登录页面不进行过滤 + chain.doFilter(request, response); + return; + } + + //判断用户是否登录 + if (user == null & !(hrequest.getRequestURI().contains("login"))){ + wrapper.sendRedirect("index.jsp"); + return; + }else { + chain.doFilter(request, response); + return; + } + + } +} diff --git a/com/src/main/java/LoginServlet.java b/com/src/main/java/LoginServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..dcb0281738a1f9b3628acf9b2ea0e68534480ac7 --- /dev/null +++ b/com/src/main/java/LoginServlet.java @@ -0,0 +1,43 @@ +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.*; +import java.io.*; +import javax.servlet.*; + +@WebServlet("/loginAction") +public class LoginServlet extends HttpServlet { + + protected void doPost(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException + { + + response.setContentType("text/html;charset=gb2312"); + request.setCharacterEncoding("utf-8"); + User u = new User(); + u.setUsername(request.getParameter("username")); + u.setPassword(request.getParameter("password")); + request.setAttribute("user", u); + + boolean flag = Service.assertUser(u); + + if(flag){ + // 判断用户登录成功,创建session + request.getSession(); + request.setAttribute("count",OnlineCont.count); + request.getRequestDispatcher("/main.jsp").forward(request,response); + }else{ + response.sendRedirect("./index.jsp"); + } + + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getSession().invalidate(); + response.sendRedirect("./index.jsp"); + } +} + +} + + diff --git a/com/src/main/java/OnlineCont.java b/com/src/main/java/OnlineCont.java new file mode 100644 index 0000000000000000000000000000000000000000..343023449991f684e5c2d87d3b87bc01310bcd94 --- /dev/null +++ b/com/src/main/java/OnlineCont.java @@ -0,0 +1,23 @@ +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; + +public class OnlineCont implements HttpSessionListener { + static int count=0; + static HttpSessionEvent se; + + @Override + public void sessionCreated(HttpSessionEvent se) { + System.out.println("into sessionCreated"); + count++; + se.getSession().getServletContext().setAttribute("count", count); + + } + + @Override + public synchronized void sessionDestroyed(HttpSessionEvent se) { + count--; + se.getSession().getServletContext().setAttribute("count", count); + } + + +} \ No newline at end of file diff --git a/com/src/main/java/Service.java b/com/src/main/java/Service.java new file mode 100644 index 0000000000000000000000000000000000000000..2de1ecf9a58845c73fadd86102b4391791e5f356 --- /dev/null +++ b/com/src/main/java/Service.java @@ -0,0 +1,51 @@ +import java.sql.*; + + +public class Service { + public static boolean assertUser(User u){ + // TODO Auto-generated method stub + boolean flag = false; + try { + + //加载驱动程序 + Class.forName("com.mysql.cj.jdbc.Driver"); + + //创建连接 + String url="jdbc:mysql://localhost:3306/javaee?serverTimezone=GMT&useSSL=false&allowPublicKeyRetrieval=true"; + String username="root"; + String pwd ="root"; + Connection conn = DriverManager.getConnection(url,username,pwd); + + //创建Statement,执行sql + Statement st=conn.createStatement(); + String sql = "select * from user where username = '"+u.getUsername()+"' "; + ResultSet rs =st.executeQuery(sql); + + while(rs.next()){ + if(rs.getString("password").equals(u.getPassword())){ + flag = true; + }else{ + flag = false; + } + } + + + //关闭连接 + rs.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return flag; + } + + + + +} diff --git a/com/src/main/java/User.java b/com/src/main/java/User.java new file mode 100644 index 0000000000000000000000000000000000000000..a80078e849b80aa384f4296fbc4d9e325d6b55f2 --- /dev/null +++ b/com/src/main/java/User.java @@ -0,0 +1,20 @@ +public class User { + private String username; + private 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; + } +} diff --git a/com/src/main/webapp/WEB-INF/web.xml b/com/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..c2d2cf41ea753c209d3de1c808aca841a28d757e --- /dev/null +++ b/com/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,26 @@ + + + + + LoginServlet + LoginServlet + + + LoginServlet + /loginAction + + + SessionFilter + LoginFilter + + + SessionFilter + /* + + + OnlineCont + + \ No newline at end of file diff --git a/com/src/main/webapp/index.jsp b/com/src/main/webapp/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..974e2d892eafd8b3cf2e2c59c60d9ac3905d1450 --- /dev/null +++ b/com/src/main/webapp/index.jsp @@ -0,0 +1,37 @@ +<%@ page session="false" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + JSP - Hello World + + +

<%= "Hello World!" %> +

+<% + HttpSession s = request.getSession(false); +%> +
+
+ + + + + + + + + + +
用户名:
+
+
密码:
+ +
+

+ + +

+
+ + + \ No newline at end of file diff --git a/com/src/main/webapp/main.jsp b/com/src/main/webapp/main.jsp new file mode 100644 index 0000000000000000000000000000000000000000..a465add6d6f62ee288ab30d732dbac0ed3da3e8f --- /dev/null +++ b/com/src/main/webapp/main.jsp @@ -0,0 +1,27 @@ +<%-- + Created by IntelliJ IDEA. + User: lijunhui + Date: 3/18/2021 + Time: 11:00 AM + To change this template use File | Settings | File Templates. +--%> +<%@ page session="false" contentType="text/html;charset=UTF-8" language="java" %> + + + + + Main + + + + + diff --git a/com/target/com-1.0-SNAPSHOT/META-INF/MANIFEST.MF b/com/target/com-1.0-SNAPSHOT/META-INF/MANIFEST.MF new file mode 100644 index 0000000000000000000000000000000000000000..5168ab5b65abe27bd01f1ed6a70df94d4700dd6a --- /dev/null +++ b/com/target/com-1.0-SNAPSHOT/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Created-By: IntelliJ IDEA +Built-By: lijunhui +Build-Jdk: version 15.0.1 + diff --git a/com/target/com-1.0-SNAPSHOT/WEB-INF/web.xml b/com/target/com-1.0-SNAPSHOT/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..3725c596a65f9f12a0f3dd21569701ae0b3f94ca --- /dev/null +++ b/com/target/com-1.0-SNAPSHOT/WEB-INF/web.xml @@ -0,0 +1,15 @@ + + + + + LoginServlet + LoginServlet + + + LoginServlet + /loginAction + + \ No newline at end of file diff --git a/com/target/com-1.0-SNAPSHOT/index.jsp b/com/target/com-1.0-SNAPSHOT/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..7edddc347abbe0b77ce8ebf4a0f2bf4b4f7eaa20 --- /dev/null +++ b/com/target/com-1.0-SNAPSHOT/index.jsp @@ -0,0 +1,35 @@ +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + JSP - Hello World + + +

<%= "Hello World!" %> +

+
+Login JSP +
+ + + + + + + + + + +
用户名:
+
+
密码:
+ +
+

+ + +

+
+ + + \ No newline at end of file diff --git a/com/target/com-1.0-SNAPSHOT/main.jsp b/com/target/com-1.0-SNAPSHOT/main.jsp new file mode 100644 index 0000000000000000000000000000000000000000..2d4e943a6297e2ed9ef15dabcd2e60ace4effcb1 --- /dev/null +++ b/com/target/com-1.0-SNAPSHOT/main.jsp @@ -0,0 +1,20 @@ +<%-- + Created by IntelliJ IDEA. + User: lijunhui + Date: 3/18/2021 + Time: 11:00 AM + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Main + + + + +