/class User
package com.neuedu.bean; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 1L; private int password; private String name; public User() { super(); } public User(int password, String name) { super(); this.password = password; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPassword() { return password; } public void setPassword(int password) { this.password = password; } @Override public String toString() { return "password=" + password + ", name=" + name; } }
/class LoginDao
package com.neusoft.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.neuedu.bean.User; import com.neusoft.utils.JDBCUtil; public class LoginDao { public User getUser(String name,String password){ User user=null; PreparedStatement ps=null; ResultSet rs =null; Connection conn=JDBCUtil.getConnection(); String sql="select * from t_user where password= ? and name =?"; try { ps = conn.prepareStatement(sql); ps.setString(1, password); ps.setString(2, name); rs = ps.executeQuery(); while (rs.next()) { String username = rs.getString("name"); int password2 = rs.getInt("password"); user=new User(password2,username); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JDBCUtil.close(ps, rs, conn); } return user; } public void Add(String name,String password,String email){ PreparedStatement ps=null; Connection conn=JDBCUtil.getConnection(); String sql="insert into t_user values(?,?,?,?)"; try { ps = conn.prepareStatement(sql); ps.setString(1, null); ps.setString(2, name); ps.setString(3, password); ps.setString(4, email); ps.executeUpdate(); System.out.println(ps.toString()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if (ps !=null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn !=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void Update(String name,String password,String email){ PreparedStatement ps=null; Connection conn=JDBCUtil.getConnection(); String sql="update t_user set name=?,pasword=?,mail=? where id=?"; try { ps = conn.prepareStatement(sql); ps.setString(1, null); ps.setString(2, name); ps.setString(3, password); ps.setString(4, email); ps.executeUpdate(); System.out.println(ps.toString()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if (ps !=null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn !=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
/class AFilter
package com.neusoft.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter( "/LoginServlet" ) public class AFilter implements Filter { public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("AFilter此路是我开,此树是我栽!"); String name = request.getParameter("username"); if (name.equals("qwe")) { chain.doFilter(request, response); System.out.println("AFilter要想从此过,留下买路财!"); }else { request.getRequestDispatcher("/Login.jsp").forward(request, response);//转发 } } public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
/class BFilter
package com.neusoft.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter("/LoginServlet") public class BFilter implements Filter { public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("BFilter此路是我开,此树是我栽!"); String psd = request.getParameter("pwd"); if (psd.equals("123")) { chain.doFilter(request, response); System.out.println("BFilter要想从此过,留下买路财!"); }else { request.getRequestDispatcher("/Login.jsp").forward(request, response);//转发 } } public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
/class LoginServlet
package com.neusoft.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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 javax.servlet.http.HttpSession; import com.neuedu.bean.User; import com.neusoft.dao.LoginDao; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String reqUUID = request.getParameter("uuid"); HttpSession session = request.getSession(); String sessUUID =(String)session.getAttribute("uuid"); session.removeAttribute("uuid");//防止重复提交 try { Thread.sleep(1000*3);//休眠3秒 String name = request.getParameter("username"); String psd = request.getParameter("pwd"); System.out.println(name); User user=new LoginDao().getUser(name, psd); if (user !=null&&reqUUID.equals(sessUUID)) { // List<User>List=new ArrayList<User>(); // List.add(user); request.setAttribute("user", user); System.out.println(user); // response.sendRedirect(request.getContextPath()+"/login-success.jsp");//重定向 request.getRequestDispatcher("/login-success.jsp").forward(request, response);//转发 }else { request.setAttribute("errorMsg", "不要重复提交!"); request.getRequestDispatcher("/Login.jsp").forward(request, response);//转发 } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); doGet(request, response); } }
/class OUTServlet
package com.neusoft.servlet; import java.io.IOException; 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 javax.servlet.http.HttpSession; import javax.swing.JOptionPane; @WebServlet("/OUTServlet") public class OUTServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.invalidate();//清除会话,也就是清除参数 JOptionPane.showMessageDialog(null,"您已退出,请重新登录"); response.sendRedirect(request.getContextPath()+"/Login.jsp");//重定向 } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
/class JDBCUtil
package com.neusoft.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtil { private static String driver="com.mysql.jdbc.Driver"; private static String url="jdbc:mysql://localhost:3306/demo"; private static String username="root"; private static String password="123456"; static{ try { Class.forName(driver); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection(){ try { return DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block return null; } } public static void close(Statement st,ResultSet rs,Connection conn){ if (conn !=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (rs !=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (st !=null) { try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
/Login.jsp
<%@page import="java.util.UUID"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String uuid=UUID.randomUUID().toString(); session.setAttribute("uuid", uuid); %> ${errorMsg} <form action="${pageContext.request.contextPath}/LoginServlet" method="post"> <input type="hidden" name="uuid" value="<%=uuid%>"/> 用户名:<input type="text" name="username"/> 密码:<input type="password" name="pwd"/> <input type="submit" value="提交"/> </form> <a href="Regist.html">去注册</a> </body> </html>
<%@page import= "com.neuedu.bean.User"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table border="1" align="center" width="50%"> <tr> <th>姓名 </th> <th>密码 </th> <th colspan="2"> </th> </tr> <%-- <% List<User>stuList=new ArrayList<User>(); stuList=(List<User>)request.getAttribute("stuList"); for(int i=0;i<stuList.size();i++){ User user=stuList.get(i); %> --%> <tr> <td><%-- <%=user.getName() %> --%>${user.name}</td> <td><%-- <%=user.getPassword() %> --%>${user.password}</td> <td><a href="#">修改</a></td> <td><a href="#">删除</a></td> </tr> <%-- <% } %> --%> </table> <form action="${pageContext.request.contextPath}/OUTServlet" > <input type="submit"value="退出"/> </form> </body> </html>
写之前导包