bean層(封裝數據對象):
package bean; public class LoginBean { private String name; private String pwd; public LoginBean() { super(); } public LoginBean(String name, String pwd) { super(); this.name = name; this.pwd = pwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
dao層(封裝對數據的邏輯操作):
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import bean.LoginBean; //處理登錄 public class LoginDao { private final static String DriverName="com.mysql.jdbc.Driver"; private final static String Uname="root"; private final static String Upwd="1108.shjzh..sql.lq"; private final static String DbURL="jdbc:mysql://localhost:3306/login"; public static int Connect(LoginBean loginbean) { int flag=-1; //flag=1(登錄成功) Connection conn=null; PreparedStatement pstmt=null; ResultSet result=null; int count=-1; try { Class.forName(DriverName); conn=DriverManager.getConnection(DbURL, Uname, Upwd); String sql="select count(*) from user where name=? and pwd=?"; pstmt=conn.prepareStatement(sql); pstmt.setString(1, loginbean.getName()); pstmt.setString(2, loginbean.getPwd()); result=pstmt.executeQuery(); if(result.next()) { count=result.getInt(1); } if(count>0) { return 1; //登錄成功 } else { return 0; //登錄失敗(用戶名,密碼有誤) } } catch(ClassNotFoundException e) { e.printStackTrace(); return -1; } catch(SQLException e) { e.printStackTrace(); return -1; } catch(Exception e) { e.printStackTrace(); return -1; } finally { try { result.close(); pstmt.close(); conn.close(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } } }
servlet層(封裝處理從頁面傳來的值):
package 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 bean.LoginBean; import dao.LoginDao; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String name=request.getParameter("name"); String pwd=request.getParameter("pwd"); LoginBean loginbean=new LoginBean(name,pwd); //將數據傳入LoginBean中 int result=LoginDao.Connect(loginbean); if(result>0) //登錄成功 { response.sendRedirect("success.jsp"); }else if(result==0) //登錄失敗(用戶名,密碼錯誤) { request.getRequestDispatcher("fault.jsp").forward(request, response); }else //登錄失敗(系統錯誤) { response.sendRedirect("error.html"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
web頁面:
1,登錄頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登錄Insert title here</title> </head> <body> <h2>登錄</h2> <form action="LoginServlet" method="post"> 用戶名:<input type="text" name="name"> 密 碼:<input type="password" name="pwd"> <input type="submit" value="提交"> </form> </body> </html>
2,登錄成功,登錄失敗,系統異常頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 登錄成功 </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 用戶名或密碼錯誤 </body> </html> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 系統異常 </body> </html>