|
|
|
|
@ -14,62 +14,69 @@ import javax.servlet.http.HttpSession;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理用户登录请求的Servlet
|
|
|
|
|
* 根据用户选择的身份(学生/教师/管理员)进行不同的验证逻辑
|
|
|
|
|
*/
|
|
|
|
|
@WebServlet("/LoginServlet")
|
|
|
|
|
public class LoginServlet extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
// 设置请求和响应的字符编码,防止中文乱码
|
|
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
|
|
|
|
// 从请求参数中获取用户输入的用户名、密码和身份
|
|
|
|
|
String userName = req.getParameter("userName");
|
|
|
|
|
String psw = req.getParameter("psw");
|
|
|
|
|
String identity = req.getParameter("identity1");
|
|
|
|
|
// 打印日志用于调试
|
|
|
|
|
System.out.println(userName);
|
|
|
|
|
System.out.println(psw);
|
|
|
|
|
System.out.println(identity);
|
|
|
|
|
|
|
|
|
|
// 根据用户选择的身份进行不同的验证逻辑
|
|
|
|
|
if (identity.equals("学生")){//学生身份
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
String sql = "select * from student where sname = ? and spsw = ?";
|
|
|
|
|
Object[] objects = {userName, psw};
|
|
|
|
|
ResultSet resultSet = StuDao.login(sql, objects);
|
|
|
|
|
String sno = null;
|
|
|
|
|
boolean flag = false; // 标记是否验证成功
|
|
|
|
|
String sql = "select * from student where sname = ? and spsw = ?"; // SQL查询语句
|
|
|
|
|
Object[] objects = {userName, psw}; // SQL查询参数
|
|
|
|
|
ResultSet resultSet = StuDao.login(sql, objects); // 执行数据库查询
|
|
|
|
|
String sno = null; // 学生学号
|
|
|
|
|
try {
|
|
|
|
|
if (resultSet.next()){
|
|
|
|
|
flag = true;
|
|
|
|
|
sno = resultSet.getString("sno");
|
|
|
|
|
if (resultSet.next()){ // 如果查询结果不为空
|
|
|
|
|
flag = true; // 验证成功
|
|
|
|
|
sno = resultSet.getString("sno"); // 获取学生学号
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
e.printStackTrace(); // 打印异常堆栈信息
|
|
|
|
|
}finally {
|
|
|
|
|
JdbcUtils.close(resultSet);
|
|
|
|
|
JdbcUtils.close(resultSet); // 关闭结果集,释放资源
|
|
|
|
|
}
|
|
|
|
|
if (flag){
|
|
|
|
|
if (flag){ // 验证成功
|
|
|
|
|
System.out.println("登录成功!");
|
|
|
|
|
//创建会话,处理请求要用
|
|
|
|
|
// 创建会话,存储用户信息
|
|
|
|
|
HttpSession session = req.getSession();
|
|
|
|
|
session.setAttribute("userName", userName);
|
|
|
|
|
// 创建请求属性对象,处理请求要用
|
|
|
|
|
session.setAttribute("sno", sno);
|
|
|
|
|
session.setAttribute("userName", userName); // 存储用户名
|
|
|
|
|
session.setAttribute("sno", sno); // 存储学号
|
|
|
|
|
System.out.println(sno);
|
|
|
|
|
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
|
|
req.setAttribute("httpUrl","/view/stu/stumainview.jsp");
|
|
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
|
|
req.setAttribute("title","登录成功");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
// resp.sendRedirect(req.getContextPath() + "/view/stu/stumainview.jsp");
|
|
|
|
|
}else {
|
|
|
|
|
}else { // 验证失败
|
|
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
|
|
//返回登录成功的信息
|
|
|
|
|
// 设置错误信息并转发到提示页面
|
|
|
|
|
req.setAttribute("httpUrl","/view/login/login.jsp");
|
|
|
|
|
req.setAttribute("info", "登录失败!用户名或密码错误!即将跳转至登录页面!");
|
|
|
|
|
req.setAttribute("title","登录失败");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
}
|
|
|
|
|
}else
|
|
|
|
|
if (identity.equals("教师")){//教师身份
|
|
|
|
|
}else if (identity.equals("教师")){//教师身份
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
String sql = "select * from teacher where tname = ? and tpsw = ?";
|
|
|
|
|
Object[] objects = {userName, psw};
|
|
|
|
|
@ -87,13 +94,13 @@ public class LoginServlet extends HttpServlet {
|
|
|
|
|
}
|
|
|
|
|
if (flag){
|
|
|
|
|
System.out.println("登录成功!");
|
|
|
|
|
//创建会话,处理请求要用
|
|
|
|
|
// 创建会话,存储用户信息
|
|
|
|
|
HttpSession session = req.getSession();
|
|
|
|
|
session.setAttribute("userName", userName);
|
|
|
|
|
// 创建请求属性对象,处理请求要用
|
|
|
|
|
session.setAttribute("tno", tno);
|
|
|
|
|
System.out.println(tno);
|
|
|
|
|
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
|
|
req.setAttribute("httpUrl","/view/tea/teamainview.jsp");
|
|
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
|
|
req.setAttribute("title","登录成功");
|
|
|
|
|
@ -101,72 +108,68 @@ public class LoginServlet extends HttpServlet {
|
|
|
|
|
// resp.sendRedirect(req.getContextPath() + "/view/guide/mainview_guide.jsp");
|
|
|
|
|
}else {
|
|
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
|
|
//返回登录成功的信息
|
|
|
|
|
// 设置错误信息并转发到提示页面
|
|
|
|
|
req.setAttribute("httpUrl","/view/login/login.jsp");
|
|
|
|
|
req.setAttribute("info", "登录失败!用户名或密码错误!即将跳转至登录页面!");
|
|
|
|
|
req.setAttribute("title","登录失败");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
}
|
|
|
|
|
}else {//如果是管理员
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
String sql = "select * from admin where adname = ? and adpsw = ? and belong = ?";
|
|
|
|
|
Object[] objects = {userName, psw, identity};
|
|
|
|
|
ResultSet resultSet = SchoAdminDao.login(sql, objects);
|
|
|
|
|
// String belong = null;
|
|
|
|
|
try {
|
|
|
|
|
if (resultSet.next()){
|
|
|
|
|
flag = true;
|
|
|
|
|
// belong = resultSet.getString("belong");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}finally {
|
|
|
|
|
JdbcUtils.close(resultSet);
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
String sql = "select * from admin where adname = ? and adpsw = ? and belong = ?";
|
|
|
|
|
Object[] objects = {userName, psw, identity};
|
|
|
|
|
ResultSet resultSet = SchoAdminDao.login(sql, objects);
|
|
|
|
|
try {
|
|
|
|
|
if (resultSet.next()){
|
|
|
|
|
flag = true;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}finally {
|
|
|
|
|
JdbcUtils.close(resultSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag){
|
|
|
|
|
|
|
|
|
|
if (identity.equals("学校")){//学校管理员
|
|
|
|
|
System.out.println("登录成功!");
|
|
|
|
|
HttpSession session = req.getSession();
|
|
|
|
|
// 创建会话属性对象,处理请求要用
|
|
|
|
|
session.setAttribute("belong", identity);
|
|
|
|
|
System.out.println(identity);
|
|
|
|
|
// session.setAttribute("belong", belong);
|
|
|
|
|
// System.out.println(belong);
|
|
|
|
|
if (flag){ // 管理员验证成功
|
|
|
|
|
|
|
|
|
|
req.setAttribute("httpUrl","/view/schoadmin/schomainview.jsp");
|
|
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
|
|
req.setAttribute("title","登录成功");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
}else {//二级学院管理员
|
|
|
|
|
System.out.println("登录成功!");
|
|
|
|
|
//创建会话,处理请求要用
|
|
|
|
|
HttpSession session = req.getSession();
|
|
|
|
|
// 创建请求属性对象,处理请求要用
|
|
|
|
|
session.setAttribute("belong", identity);
|
|
|
|
|
System.out.println(identity);
|
|
|
|
|
if (identity.equals("学校")){//学校管理员
|
|
|
|
|
System.out.println("登录成功!");
|
|
|
|
|
HttpSession session = req.getSession();
|
|
|
|
|
session.setAttribute("belong", identity); // 存储管理员所属单位
|
|
|
|
|
System.out.println(identity);
|
|
|
|
|
|
|
|
|
|
req.setAttribute("httpUrl","/view/deptadmin/deptmainview.jsp");
|
|
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
|
|
req.setAttribute("title","登录成功");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
// resp.sendRedirect(req.getContextPath() + "/view/guide/mainview_guide.jsp");
|
|
|
|
|
}
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
|
|
req.setAttribute("httpUrl","/view/schoadmin/schomainview.jsp");
|
|
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
|
|
req.setAttribute("title","登录成功");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
}else {//二级学院管理员
|
|
|
|
|
System.out.println("登录成功!");
|
|
|
|
|
// 创建会话,存储用户信息
|
|
|
|
|
HttpSession session = req.getSession();
|
|
|
|
|
session.setAttribute("belong", identity);
|
|
|
|
|
System.out.println(identity);
|
|
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
|
|
//返回登录成功的信息
|
|
|
|
|
req.setAttribute("httpUrl","/view/login/login.jsp");
|
|
|
|
|
req.setAttribute("info", "登录失败!用户名或密码错误!即将跳转至登录页面!");
|
|
|
|
|
req.setAttribute("title","登录失败");
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
|
|
req.setAttribute("httpUrl","/view/deptadmin/deptmainview.jsp");
|
|
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
|
|
req.setAttribute("title","登录成功");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
// resp.sendRedirect(req.getContextPath() + "/view/guide/mainview_guide.jsp");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
|
|
// 设置错误信息并转发到提示页面
|
|
|
|
|
req.setAttribute("httpUrl","/view/login/login.jsp");
|
|
|
|
|
req.setAttribute("info", "登录失败!用户名或密码错误!即将跳转至登录页面!");
|
|
|
|
|
req.setAttribute("title","登录失败");
|
|
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
doGet(req, resp);
|
|
|
|
|
doGet(req, resp); // 对于POST请求,直接调用doGet方法处理
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|