JSP——用户登录功能(MySQL)

用户登录功能(核心)

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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值