Student(1)——系统环境配置
程序员文章站
2022-07-05 16:02:57
...
目录
数据库表:
1. 实体层:model层;
package com.java.model;
public class User {
private int id;
private String userName;
private String passWord;
public User() {
}
public User(String userName, String passWord) {
this.userName = userName;
this.passWord = passWord;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
2.工具类:util层;
package com.java.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil {
private String dbUrl="jdbc:mysql://localhost:3306/db_student?useSSL=false&serverTimezone=UTC";
private String dbUserName="root";
private String dbPassWord="123456";
private String jdbcName="com.mysql.cj.jdbc.Driver";
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con= DriverManager.getConnection(dbUrl,dbUserName,dbPassWord);
return con;
}
public void closeCon(Connection con) throws Exception{
if(con!=null){
con.close();
}
}
public static void main(String[] args) {
DbUtil dbUtil=new DbUtil();
try {
dbUtil.getCon();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.数据访问层:dao层;
package com.java.dao;
import com.java.model.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDao {
public User login(Connection con, User user) throws Exception{
User resultUser=null;
String sql="select * from t_user where userName=? and passWord=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,user.getUserName());
pstmt.setString(2,user.getUserName());
ResultSet rs=pstmt.executeQuery();
if (rs.next()) {
resultUser=new User();
resultUser.setId(rs.getInt("id"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassWord(rs.getString("passWord"));
}
return resultUser;
}
}
4. 响应层:web层;
package com.java.web;
import com.java.dao.UserDao;
import com.java.model.User;
import com.java.util.DbUtil;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
public class LoginServlet extends HttpServlet {
private DbUtil dbUtil=new DbUtil();
private UserDao userDao=new UserDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName=request.getParameter("userName");
String passWord=request.getParameter("passWord");
Connection con=null;
try {
con=dbUtil.getCon();
User user=new User(userName,passWord);
User currentUser=userDao.login(con,user);
if(currentUser!=null){
request.setAttribute("userName",userName);
request.setAttribute("passWord",passWord);
request.getRequestDispatcher("login.jsp").forward(request,response);
} else{
HttpSession session=request.getSession();
session.setAttribute("currentUser",currentUser);
response.sendRedirect("main.jsp");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 记录用户信息的方法
private void rememberMe(String userName,String passWord,HttpServletResponse response){
Cookie user=new Cookie("user",userName+"-"+passWord);
user.setMaxAge(60*60*24*7);
response.addCookie(user);
}
}
5. 登录页面login.jsp;
<%@ page import="com.java.model.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
<title>学生信息管理系统</title>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap/js/jQuery.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.js"></script>
<%--获取Cookie功能--%>
<%
if(request.getAttribute("user")==null){
String userName=null;
String passWord=null;
Cookie[] cookies=request.getCookies();
for(int i=0;cookies!=null && i<cookies.length;i++){
if(cookies[i].getName().equals("user")){
userName=cookies[i].getValue().split("-")[0];
passWord=cookies[i].getValue().split("-")[1];
}
}
if(userName==null){
userName="";
}
if(passWord==null){
passWord="";
}
//EL表达式中采用pageContext范围,优先级最高
pageContext.setAttribute("user", new User(userName,passWord));
}
%>
<!-- 登录页面的样式css -->
<style type="text/css">
body {
padding-top: 200px;
padding-bottom: 40px;
background-image: url('images/login1.jpg');
}
.form-signin-heading{
text-align: center;
}
.form-signin {
max-width: 300px;
padding: 19px 29px 0px;
margin: 0 auto 20px;
background-color: #5bc0de;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin input[type="text"],
.form-signin input[type="password"] {
font-size: 16px;
height: auto;
margin-bottom: 15px;
padding: 7px 9px;
}
</style>
<%--界面功能逻辑判断--%>
<script type="text/javascript">
/*登录功能*/
function checkForm() {
var userName=document.getElementById("userName").value;
var passWord=document.getElementById("passWord").value;
if(userName==null||userName==""){
document.getElementById("error").innerHTML="用户名不能为空!"
}
if(passWord==null||passWord==""){
document.getElementById("error").innerHTML="密码不能为空!"
}
}
/*重置功能*/
function resetValue(){
document.getElementById("userName").value="";
document.getElementById("passWord").value="";
}
</script>
</head>
<body>
<div class="container">
<form name="myForm" class="form-signin" action="login" method="post" οnsubmit="checkForm()">
<h2 class="form-signin-heading">学生信息管理系统</h2>
<input id="userName" name="userName" value="${user.userName }" type="text" class="input-block-level" placeholder="姓名...">
<input id="passWord" name="passWord" value="${user.passWord }" type="password" class="input-block-level" placeholder="密码..." >
<label class="checkbox">
<input id="remember" name="remember" type="checkbox" value="remember-me" checked="checked">记住我 <font id="error" color="red">${error }</font>
</label>
<button class="btn btn-primary" type="submit">登录</button>
<button class="btn btn-primary" type="button" οnclick="resetValue()">重置</button>
<p align="center" style="padding-top: 15px;">版权所有 2019 Colin </p>
</form>
</div>
</body>
</html>
效果如下;