<%--
Created by IntelliJ IDEA.
User: 47
Date: 2020/10/9
Time: 8:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>使用者登入</title>
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/bootstrap.bundle.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/jquery.min.js"></script>
<style>
.container
{
width: 28%;
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">
<h3 style="text-align: center">登入</h3>
<div class="form-group">
<label for="exampleInputEmail1">使用者名稱</label><input type="text" name="username" class="form-control" id="exampleInputEmail1" type="email" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">密碼</label><input type="password" name="password" class="form-control" id="exampleInputPassword1" type="password" />
</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.ResultSet" %><%--
Created by IntelliJ IDEA.
User: 47
Date: 2020/10/2
Time: 11:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
String sql = "select * from user where username = ? and password = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
session.setAttribute("username",username);
out.print("登入成功");
response.sendRedirect("home.jsp");
}
else
{
out.print("登入失敗");
response.setHeader("refresh","3;url='login.jsp'");
}
%>
<%@ page import="java.sql.*" %><%--
Created by IntelliJ IDEA.
User: 113主機
Date: 2020/10/6
Time: 14:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>個人中心</title>
</head>
<body>
<%
if(session.getAttribute("username")==null){
out.print("你尚未登入3秒後跳轉登入頁面");
response.setHeader("refresh","3;url=login.jsp");
}else{
out.print("歡迎來到個人主頁!<br>");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
String sql = "select * from user";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()){
out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
}
}
%>
</body>
</html>