javaweb学习(四)----用户名密码登录并验证(纯jsp实现)

用户名密码登录并验证

  • index.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>
<style>
.box{
    position: absolute;/*生成绝对定位的元素,元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。(此处相对于body)*/
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);/* 实现块元素百分比下居中*/
    width: 400px;
    padding: 40px;/*设置四个内边距*/
    background: rgba(0,0,0,0.8);/*函数的前三个变量为颜色(黑),后一个为透明度(1为不透明)*/
    box-sizing: border-box;/*使实际所占宽高度 = 设置的高度(height)/ 设置的宽度(width)+ 外边距(margin)*/
    box-shadow: 0 15px 25px rgba(0,0,0,.5);/*产生盒子阴影:x轴偏移,y轴偏移,模糊度,透明色*/
    border-radius: 10px;/*使矩形四边圆滑*/
}
.box h2{
    margin: 0 0 30px;/*上、左右、下*/
    padding: 0;
    color: #ffffff;/*设置字体颜色*/
    text-align: center;/*文字居中*/
}
.box .inputBox{
    position: relative;/*相对定位*/
}
.box .inputBox input{
    width: 100%;
    padding: 10px 0;/*上下、左右*/
    font-size: 16px;/*字体大小(可用于调节输入框高)*/
    color: #ffffff;/*输入框颜色*/
    letter-spacing: 1px;/*设置对象中的文字之间的间隔*/
    margin-bottom: 30px;/*设置下外距*/
    border: none;/*定义无边框*/
    border-bottom: 1px solid #ffffff;/*底边框:大小1PX 实线 颜色#ffffff */
    outline: none;/*设置无外边框*/
    background: transparent;/*背景设置为透明*/
}
.box .inputBox label{
    position: absolute;
    top: 0;
    left: 0;
    letter-spacing: 1px;
    padding: 10px 0;
    font-size: 16px;
    color: #ffffff;
    pointer-events:none;/*阻止区块被点击,使得点击穿透*/
    transition: 1s;/*动画运行时间*/
}
/*产生对应操作时,label发生的样式变化*/
.box .inputBox input:focus~label,
.box .inputBox input:valid~label
{
    top:-18px;
    left: 0;
    color: #03a9f4;
    font-size: 12px;
}
.box input[type="submit"]{
   background: transparent;
    border: none;
    outline: none;
    color: #ffffff;
    background: #03a9f4;
    margin: 0px 80px;
    padding: 10px 30px;
    cursor: pointer;/*鼠标指针变成手的形状*/
    border-radius: 15px;/*调节矩形弧度*/
}
</style>
</head>
<body>

<div class="box">
        <h2>Login</h2>
        <!--创建供用户输入的 HTML 表单-->
        <form action="check.jsp" method="post">
            <div class="inputBox">
                <input type="text" name="uname" required="">
                <label>Username</label>
            </div>
            <div class="inputBox">
                <input type="password" name="upwd" required="">
                <label>Password</label>
            </div>
            <div align="center">
                <input type="submit" name="" value="提交">
            </div>

        </form>
    </div>

</body>
</html>
  • 显示
    在这里插入图片描述
  • 数据库端
    在这里插入图片描述
  • check.jsp
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<%
	Connection con=null;//连接流
	PreparedStatement pstmt=null;//PreparedStatement接口--实现SQL安全问题
	ResultSet rs=null;//结果流
	try {
	
		//数据库驱动
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		System.out.println("数据库驱动成功!");
		//创建连接
		String url="jdbc:sqlserver://localhost:1433;databaseName=用户注册库";//数据库配置信息
		String username="sa";//数据库登录账号
		String password="1234567";//数据库登录密码
		con=DriverManager.getConnection(url,username,password);
		if (con!=null) {
			System.out.println("已创建连接!");
		
			request.setCharacterEncoding("utf-8");
			String name=request.getParameter("uname");
			String pwd=request.getParameter("upwd");
			String sql="select count(*) from tb_user where username=? and password=?";
			pstmt=con.prepareStatement(sql);
			pstmt.setString(1, name);
			pstmt.setString(2, pwd);
			rs=pstmt.executeQuery();
			int count=-1;
			if(rs.next()){
				count=rs.getInt(1);
			}
			if(count>0){
				out.println("登陆成功!");
			}else{
				out.println("登陆失败!");
			}
		
		}else {
			System.out.println("数据库连接失败!");
		}
	} catch (ClassNotFoundException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}finally{
		//打开顺序与关闭顺序相反
		if(rs!=null){
			rs.close();
		}
		if(pstmt!=null){
			pstmt.close();
		}
		if(con!=null){
			con.close();
		}
	}



%>
</body>
</html>
  • 显示
    在这里插入图片描述
  • 3
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值