项目结构如下:
登录界面
登录成功
登录失败
user代码段
package bean; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import util.DB; 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; } public boolean Check(String username,String password)throws Exception{ Connection conn=null; ResultSet rest=null; PreparedStatement prst=null; String sql="select * from user where username=?"; boolean flag=false; try{ conn=DB.getConn(); prst=conn.prepareStatement(sql); prst.setString(1, username); rest=prst.executeQuery(); if(rest.next()){ if(password.equals(rest.getString("password"))){ flag=true; }else{ throw new Exception("密码不对哦"); } }else{ throw new Exception("用户名不存在"); } }catch(Exception e){ e.printStackTrace(); }finally{ try { rest.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { prst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return flag; } }
DB代码段
package util; import java.sql.Connection; import java.sql.DriverManager; public class DB { public static Connection getConn(){ Connection conn=null; try{ String url="jdbc:mysql://localhost/student"; String user="root"; String password="root"; Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url, user, password); if(conn!=null){ System.out.println("数据库连接成功!"); } }catch(Exception e){ e.printStackTrace(); } return conn; } }
userServlet代码段
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.user; /** * Servlet implementation class userServlet */ @WebServlet( urlPatterns={"/userServlet"},name="userServlet" ) public class userServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public userServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8");//处理表单中文乱码 user u=new user(); String username,password; try{ username=request.getParameter("username"); password=request.getParameter("password"); if(u.Check(username, password)){ u.setUsername(username); u.setPassword(password); request.getSession().setAttribute("use", u); request.getRequestDispatcher("/Success.jsp").forward(request, response); }else{ request.getRequestDispatcher("/fail.jsp").forward(request, response); } }catch(Exception e){ e.printStackTrace(); } } }
login.jsp代码段
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>用户登录</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <form action="<%=request.getContextPath() %>/userServlet" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"></td> </tr> <tr> <td></td> <td><input type="submit" value="登录"></td> </tr> </table> </form> </center> </body> </html>
success.jsp代码段
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <jsp:useBean id="use" class="bean.user" scope="session"></jsp:useBean> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登录成功</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table> <tr> <td>用户名:</td> <td><jsp:getProperty name="use" property="username"/></td> </tr> <tr> <td>密码:</td> <td><jsp:getProperty name="use" property="password"/></td> </tr> </table> </body> </html>
fail.jsp代码段
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登录失败</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登录失败!<a href="login.jsp">返回登录</a> </body> </html>