纯jsp实现数据库的连接和验证登录
2021/5/3 2:26:16
本文主要是介绍纯jsp实现数据库的连接和验证登录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
纯jsp实现数据库连接并且登录
代码段
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录页面</title> </head> <body> <form action="dologin.jsp" method="post"> <h3>登录界面</h3> 账户:<input name="username" type="text"><br> 密码:<input name="passward" type="password"><br> <input type="submit" value="登录"> </form> </body> </html>
数据库连接和处理阶段
dologin.jsp
数据库连接名:root 密码自己设置的
数据库名:root
表明:user
<%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Driver"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name = request.getParameter("username"); String pwd = request.getParameter("password"); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/root", "root", "123456"); PreparedStatement preparedStatement=connection.prepareStatement("select * from user where username=? and password=?"); preparedStatement.setString(1,name); preparedStatement.setString(2,pwd); ResultSet resultSet=preparedStatement.executeQuery(); if(resultSet.next()){ response.sendRedirect("default.jsp"); }else{ response.sendRedirect("success.jsp"); } connection.close(); preparedStatement.close(); %> </body> </html>
登录成功页面
success.jsp
<%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Driver"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 成功!! </body> </html>
登录失败页面
default.jsp
<%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Driver"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 失败!! </body> </html>
这篇关于纯jsp实现数据库的连接和验证登录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist