用户登录页面——jdbc
Posted 野心家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户登录页面——jdbc相关的知识,希望对你有一定的参考价值。
登录页面程序login.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>登录页面</title> </head> <body> <form action="receive.jsp" method="post"> <table> <tr><td>用户</td><td><input type="text"name="user"></td></tr> <tr><td>密码</td><td><input type="password"name="password"></td></tr> <tr align="center"> <td colspan="2"> <input type="submit"value="登 录"> <input type="reset"value="取 消 "> </td> </tr> </table> </form> </body> </html> 检验输入信息程序receive.jsp <%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>验证页面</title> </head> <body> <% String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="123456"; String dbName="student"; String url1="jdbc:mysql://localhost:3306/student"; String url2="?user=root&password=123456"; String url3="&userUnicode=true&characterEncoding=UTF-8"; String url=url1+url2+url3; Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?user=root&password=123456&userUnicode=true&characterEncoding=UTF-8"); request.setCharacterEncoding("UTF-8"); String name=request.getParameter("user"); String password=request.getParameter("password"); session.setAttribute("user",name); session.setAttribute("password",password); String sql="select *from student where name=? and password=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1,name); pstmt.setString(2,password); ResultSet rs=pstmt.executeQuery(); if(rs.next()) { if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();}%> <jsp:forward page="succeed.jsp"/> <%} else { %> <jsp:forward page="failed.jsp"/> <% if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> <%} %> </body> </html> 登陆成功页面succeed.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>成功页面</title> </head> <body> <%String Name=request.getParameter("user"); %> 欢迎,<%=Name %>成功登陆! </body> </html> 登陆失败页面failed.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>失败页面</title> </head> <body> 登陆失败! <%String Name=request.getParameter("user"); %> <br><a href="login.jsp">请重新登录,<%=Name %>同学! </body> </html>
以上是关于用户登录页面——jdbc的主要内容,如果未能解决你的问题,请参考以下文章