您的位置:首页 > 数据库 > MySQL

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数据库数据

  • 点赞
  • 收藏
  • 分享
  • 文章举报
可一z可再 发布了18 篇原创文章 · 获赞 1 · 访问量 247 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: