网页开始运行login.jsp文件,出现如下图所示界面,若输入账号和密码正确,则进入个人主页界面,否则,网站也提示登录失败,3秒后跳转回最初的登陆界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.bundle.js"></script>
<script src="js/bootstrap.js"></script>
<style>
.container{
width:20%;
height: 20%;
margin-top: 10%;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form role="form" method="post" action="index.jsp">
<div class="form-group">
<label for="exampleInputEmail1">账户名</label><input class="form-control" name="username" id="exampleInputEmail1" type="text" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">密码</label><input class="form-control" name="password" id="exampleInputPassword1" type="password" />
</div>
<div class="checkbox">
<label><input type="checkbox" />Check me out</label>
</div> <button class="btn btn-default" type="submit">提交</button>
</form>
</div>
</div>
</div>
</body>
</html>
增加数据
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%--
Created by IntelliJ IDEA.
User: 给
Date: 2020/10/20
Time: 16:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="com.user" scope="request"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
String url ="jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
//关闭自动提交
connection.setAutoCommit(false);
//sql语句模板
String sql = "insert into user (username,password,gender,age,id) value (?,?,?,?,?)";
//创建preparedStatement对象
PreparedStatement ps =connection.prepareStatement(sql);
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
ps.setInt(3,user.getGender());
ps.setInt(4,user.getAge());
ps.setInt(5,7);
ps.addBatch();
ps.setString(1,user.getUsername()+"123");
ps.setString(2,user.getPassword()+"123");
ps.setInt(3,user.getGender());
ps.setInt(4,user.getAge());
ps.setInt(5,13);
ps.addBatch();
//事务
int[] count={0};
try {
count = ps.executeBatch();
connection.commit();
}catch (SQLException e){
connection.rollback();
e.printStackTrace();
}
if(count[0] > 0){
out.print("添加成功");
}else{
out.print("添加失败");
}
response.setHeader("refresh","3;url=home.jsp");
%>
<html>
<head>
<title>Title</title>
</head>
<body>
</body>
</html>