JSP——用户登录功能(MySQL)
2020-02-02 14:29
1196 查看
用户登录功能(核心)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="java.sql.*" %> <% 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>My JSP 'index.jsp' starting page</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> <% String url="jdbc:mysql://localhost/test";//数据库名 String username="root"; String password="123456"; Connection conn=null; Statement stmt=null; ResultSet rst=null; try{ Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e){ out.println("加载驱动器类时出现异常"); } try{ conn=DriverManager.getConnection(url,username,password); stmt=conn.createStatement(); String inputname=request.getParameter("username"); String inputpwd=request.getParameter("password"); out.println(inputname+"<br>"); out.println(inputpwd+"<br>"); //String sql="select * from user where name='"+inputname+"'"; String sql="select * from user where name='"+inputname+"' and password='"+inputpwd+"'"; out.println(sql+"<br>"); rst=stmt.executeQuery(sql); out.println(rst); if(rst.next()) { out.print("账号输入正确"); response.sendRedirect("login_success.jsp"); } else { out.print("账号输入错误"); } /*rst.next(); out.println(rst.getRow()); if(rst.getRow()==1) { out.print("账号正确!"); if(inputpwd.equals(rst.getString(2))) { out.println("密码正确!"); response.sendRedirect("login_success.jsp"); } else { out.println("密码错误!"); response.sendRedirect("login_fail1.jsp"); } } else { out.println("账号错误!"); response.sendRedirect("login_fail2.jsp"); } */ rst.close(); stmt.close(); }catch(SQLException e){ out.println("出现SQLException异常"); }finally{ try{ if(conn!=null)conn.close(); }catch(SQLException e){ out.println("关闭数据库连接时出现异常"); } } %> </body> </html>
简单的登录界面
<%@ 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>My JSP 'slogin.jsp' starting page</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> <form action="login_doing.jsp"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="确定"/> </form> </body> </html>
登陆成功界面
<%@ 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>My JSP 'showuser.jsp' starting page</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> 登录成功!!! <br> </body> </html>
登录失败界面(密码错误)
<%@ 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>My JSP 'login_fail1.jsp' starting page</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> 密码错误!!! <br> </body> </html>
登录失败界面(账号错误)
<%@ 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>My JSP 'login_fail2.jsp' starting page</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> 账号错误!!! <br> </body> </html>
MySQL数据库数据
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- jsp+javabean+servlet+Mysql实现MVC模式下的注册登录留言功能
- jsp中未登录用户也可以浏览页面的功能实现代码
- 论坛-纯JSP怎么实现用户的注销和登录功能
- JSP的隐式对象----实例,用模式一实现用户登录功能。
- jsp+servlet+mysql 实现简单的银行登录转账功能
- Java+mysql用户注册登录功能
- JSP实现用户登录、注册和退出功能
- jsp+javaBean+servlet+mysql完整的实现一个登录功能
- jsp连接数据库实现用户登录功能
- jsp+servlet+javaBean+mysql (MVC)模拟用户登录
- 用户注册登录测试版(jsp+servlet+mysql)
- jsp+servlet+javaBean+mysql (MVC)模拟用户登录
- JSP+JavaBean+Servlet技术实现某网站用户注册和登录功能
- 用户注册登录测试版(jsp+servlet+mysql)
- struts2框架+mysql,实现用户登录、注册功能
- 基于Spring+Mybatis+jsp+servlet的用户登录注册功能(数据库查找和插入用户),jsp写的前端(css+javascript),使用IDEA
- mysql开启用户(登录)审计功能
- JSP+JavaBean实现用户登录功能
- mysql开启用户(登录)审计功能
- Servlet+Jsp+MySql用户登录实例