1、Login.jsp
在这个页面中,通过表单输入用户名和密码,点击登陆;如果数据库中存在此人的正确的用户名和密码,那么系统提示成功登陆;否则系统提示登陆失败,请重新登陆。
<body><form id="form1" name="form1" method="post" action="checklogin.jsp">用户名:<label><input type="text" name="username" /></label><p>密码:<label><input type="text" name="password" /></label></p><p><label><input type="submit" name="Submit" value="提交" /></label></p></form></body>
2、Checklogin.jsp
这个页面首先取得login.jsp提交过来的数据,然后连接数据库,通过sql语句在数据库中进行查询比对,看有没有此用户,如果有,提示用户登陆成功,反之。
<body><%String username=new String(request.getParameter("username").getBytes("ISO8859_1"),"GBK");String password=new String(request.getParameter("password").getBytes("ISO8859_1"),"GBK");try {// 加载数据库驱动,注册到驱动管理器Class.forName("com.mysql.jdbc.Driver");// 数据库连接字符串String url = "jdbc:mysql://localhost:3306/userdb";// 数据库用户名String usename = "root";// 数据库密码String psw = "111";// 创建Connection连接Connection conn = DriverManager.getConnection(url,usename,psw);// 判断 数据库连接是否为空if(conn != null){String sql="select * from user where username='"+username+"' and password='"+ password + "'";Statement stmt = conn.createStatement();ResultSet rs=stmt.executeQuery(sql);if(rs.next()){response.sendRedirect("home.jsp");}else{out.print("用户名或密码错误,请重新输入!");%><a href="javascript:history.back()">返回</a><% }// 输出连接信息//out.println("数据库连接成功!");// 关闭数据库连接conn.close();}else{// 输出连接信息out.println("数据库连接失败!");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}%></body>
3、Home.jsp
登录成功,欢迎大家进入我的主页
<body>登陆成功</body>