JSP实现登陆页面(表单提交、连接数据库、实现页面跳转)

JSP实现登陆页面(表单提交、连接数据库、实现页面跳转)

1.数据库设计

在这里插入图片描述

2.主页面展示

在这里插入图片描述

3.代码展示:

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>
	<link rel="stylesheet" type="text/css" href="css/style.css" />
	<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
</head>
<body>
	<div id="bigBox">
			<h1>LOGIN</h1>
			<div class="inputBox">
				<form action="check.jsp" method="post">
					<div class="inputText">
						<i class="fa fa-user-circle" style="color: whitesmoke;"></i>
						<input type="text" placeholder="手机号或邮箱" name="username"/>
					</div>
					<div class="inputText">
						<i class="fa fa-key" style="color: whitesmoke;"></i>
						<input type="password" placeholder="密码" name="password"/>
					</div>
					<input type="submit" class="inputButton" value="LOGIN" />
				</form>
			</div>
		</div>
</body>
</html>

登录页面对应的CSS文件

body{
	margin: 0;
	padding: 0;
	background-image: url(../img/国漫.jpg);
	background-repeat: no-repeat;
}
a{
	color: #666;
	text-decoration: none;
}
#bigBox
{
	margin: 0 auto;
	margin-top: 100px;
	padding: 20px 50px;
	background-color: #000000;
	width: 500px;
	height: 400px;
	border-radius: 20px;
	text-align: center;
	background-image: linear-gradient(60deg, #29323c 0%, #485563 100%);
}
#bigBox h1
{
	font-size: 40px;
	color: floralwhite;
}
#bigBox .inputBox
{
	margin-top: 35px;
}
#bigBox .inputBox .inputText
{
	margin-top: 20px;
}
#bigBox .inputBox .inputText input
{
	border: 0;
	padding: 10px 10px;
	border-bottom: 1px solid white;
	background-color: #00000000;
	color: white;
	width: 200px;
	height: 40px;
	font-size: 20px;
}
#bigBox .inputBox .inputText i
{
	color: white;
}
#bigBox .inputBox .inputButton
{
	border: 0;
	width: 200px;
	height: 50px;
	color: white;
	margin-top: 55px;
	border-radius:20px;
	background-image: linear-gradient(to right, #b8cbb8 0%, #b8cbb8 0%,#b465da 0%, #cf6cc9 33%, #ee609c 66%, #ee609c 100%);
}

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"  %>
<!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 username = request.getParameter("username");
		String password = request.getParameter("password");
		
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		
		
		try {
			// 导入驱动,加载具体的驱动类
			Class.forName("com.mysql.jdbc.Driver");
			
			//与数据库建立连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "root");
			statement = connection.createStatement();
			//发送sql语句,执行
			String sql = "select count(*) from login where username = '"+username+"' and password = '"+password+"'  ";
			rs = statement.executeQuery(sql);
			//处理结果
			int count = -1;
			if (rs.next()) {
				count = rs.getInt(1);
			}
			if (count > 0) {
				request.getRequestDispatcher("success.jsp").forward(request, response);
			}else {
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
				try {
					if(rs != null) rs.close();
					if(statement != null) statement.close();
					if(connection != null) connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	%>
</body>
</html>

success.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>
	<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
	<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
	<div id="main">
		<i class="fa fa-diamond fa-5x" style="color: #ed9db2; size: 50;"></i>
		<h3 style="display: inline-block; ">登陆成功!!</h3>
	</div>
</body>
</html>

在这里插入图片描述

error.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>
	<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
	<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
	<div id="main">
		<i class="fa fa-exclamation-triangle fa-5x" style="color: darkgrey; size: 50;"></i>
		<h3 style="display: inline-block; ">登陆失败,请重新检查用户名或密码是否正确!!</h3>
	</div>
</body>
</html>

在这里插入图片描述

  • 58
    点赞
  • 454
    收藏
    觉得还不错? 一键收藏
  • 42
    评论
好的,下面是具体步骤: 1. 在Eclipse中新建一个动态Web项目,命名为“DynamicWebProject”。 2. 在WebContent目录下创建两个JSP文件:index.jsp和result.jsp。 3. 在index.jsp中添加一个表单,用于接收用户输入的信息,并将其提交到result.jsp页面。 ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Index Page</title> </head> <body> <form action="result.jsp" method="post"> <label for="name">Name:</label> <input type="text" name="name" required><br> <label for="age">Age:</label> <input type="number" name="age" required><br> <input type="submit" value="Submit"> </form> </body> </html> ``` 4. 在result.jsp中获取并显示用户输入的信息。 ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Result Page</title> </head> <body> <h1>Result Page</h1> <p>Name: <%= request.getParameter("name") %></p> <p>Age: <%= request.getParameter("age") %></p> </body> </html> ``` 5. 运行项目并访问index.jsp页面,在输入框中输入姓名和年龄,点击“Submit”按钮,即可跳转到result.jsp页面并显示用户输入的信息。 注意:在Eclipse中需要配置Tomcat服务器才能运行项目。如果还没有配置,可以在“Servers”视图中右键单击,选择“New” -> “Server”,然后选择Tomcat服务器进行配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值