管理员登录
This commit is contained in:
@@ -1,4 +1,57 @@
|
||||
package com.hellogithub.dao;
|
||||
|
||||
import com.hellogithub.entity.adminEntity;
|
||||
import com.hellogithub.entity.userEntity;
|
||||
import com.hellogithub.utils.JdbcUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class AdminDao {
|
||||
public static adminEntity AdminLogin(String adminName, String password,String loginTime) {
|
||||
ResultSet resultSet = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = JdbcUtils.getConnection();
|
||||
String loginSql = "select * from admin where adminName=? and adminPassword=? and isValid=1;";
|
||||
preparedStatement = connection.prepareStatement(loginSql);
|
||||
preparedStatement.setString(1, adminName);
|
||||
preparedStatement.setString(2, password);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
if (!resultSet.next()) { // 查询不到用户数据
|
||||
return null;
|
||||
}
|
||||
// 将db中数据 返回给客户端 查询到数据
|
||||
Integer id = resultSet.getInt(1);
|
||||
String dbAdminName = resultSet.getString(2);
|
||||
String dbAdminPassword = resultSet.getString(3);
|
||||
adminEntity adminEntity1 = new adminEntity(dbAdminName, dbAdminPassword);
|
||||
return adminEntity1;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||
}
|
||||
}
|
||||
|
||||
public int updateAdminLoginTime(String name){
|
||||
String label="";
|
||||
int num;
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql="UPDATE user SET searchHistory = ? WHERE userName= ? and isValid = 1";
|
||||
num = runner.update(conn,sql,label,name);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -5,4 +5,10 @@ public class adminEntity {
|
||||
String adminName;
|
||||
String adminPassword;
|
||||
int isValid;
|
||||
|
||||
public adminEntity(String adminName, String adminPassword) {
|
||||
this.adminName = adminName;
|
||||
this.adminPassword = adminPassword;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,4 +1,14 @@
|
||||
package com.hellogithub.service;
|
||||
|
||||
import com.hellogithub.dao.AdminDao;
|
||||
import com.hellogithub.entity.adminEntity;
|
||||
import com.hellogithub.entity.userEntity;
|
||||
|
||||
public class AdminService {
|
||||
AdminDao adminDao=new AdminDao();
|
||||
|
||||
public adminEntity adminLogin(String userName, String password,String loginTime){
|
||||
return AdminDao.AdminLogin(userName,password,loginTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
103
src/com/hellogithub/servlet/systemAdmin/AdminLoginServlet.java
Normal file
103
src/com/hellogithub/servlet/systemAdmin/AdminLoginServlet.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package com.hellogithub.servlet.systemAdmin;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hellogithub.entity.adminEntity;
|
||||
import com.hellogithub.entity.userEntity;
|
||||
import com.hellogithub.service.AdminService;
|
||||
import com.hellogithub.utils.RandomValidateCode;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
|
||||
@WebServlet("/adminLogin")
|
||||
public class AdminLoginServlet extends HttpServlet {
|
||||
AdminService adminService=new AdminService();
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doGet(req, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("text/html; charset=utf-8");
|
||||
// 设置响应头允许ajax跨域访问
|
||||
String curOrigin = req.getHeader("Origin");
|
||||
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
||||
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
|
||||
PrintWriter writer = resp.getWriter();
|
||||
String adminName = req.getParameter("adminname");
|
||||
if (StringUtils.isEmpty(adminName)) {
|
||||
setResultError("用户名不能够是为空!", writer);
|
||||
return;
|
||||
}
|
||||
|
||||
String adminPwd = req.getParameter("password");
|
||||
// 参数验证
|
||||
if (StringUtils.isEmpty(adminPwd)) {
|
||||
//转发到错误页面
|
||||
setResultError("密码不能够是为空!", writer);
|
||||
return;
|
||||
}
|
||||
|
||||
String loginTime=req.getParameter("loginTime");
|
||||
if (StringUtils.isEmpty(loginTime)){
|
||||
//转发到错误页面
|
||||
setResultError("登陆时间不能够是为空!", writer);
|
||||
return;
|
||||
}
|
||||
|
||||
// 从session中获取图形验证码
|
||||
HttpSession session = req.getSession();
|
||||
|
||||
// 在调用业务逻辑层
|
||||
adminEntity adminEntity = adminService.adminLogin(adminName, adminPwd,loginTime);
|
||||
if (adminEntity == null) {
|
||||
// 用户名称或者密码错误!
|
||||
setResultError("用户名或者密码错误!!!", writer);
|
||||
return;
|
||||
}
|
||||
|
||||
// 能够db中查询到对象 登录成功了 将管理员数据存放在session中
|
||||
session = req.getSession();
|
||||
session.setAttribute("admin", adminEntity);
|
||||
setResultOK("登录成功!", writer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
super.doDelete(req, resp);
|
||||
}
|
||||
|
||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", code);
|
||||
result.put("msg", msg);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
writer.println(jsonString);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public void setResultError(String msg, PrintWriter writer) {
|
||||
setResult(500, msg, writer);
|
||||
}
|
||||
|
||||
public void setResultOK(String msg, PrintWriter writer) {
|
||||
setResult(200, msg, writer);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user