📄 checkloginservlet.java
字号:
package cn.jx.ecjtu.oa.servlets.users;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;
import cn.jx.ecjtu.oa.common.Constant;
import cn.jx.ecjtu.oa.ps.pojo.LoginUser;
import cn.jx.ecjtu.oa.ps.pojo.Role;
import cn.jx.ecjtu.oa.services.OnlineList;
import cn.jx.ecjtu.oa.services.Result;
import cn.jx.ecjtu.oa.services.ServiceFactory;
import cn.jx.ecjtu.oa.services.SysUserService;
import cn.jx.ecjtu.oa.services.UserInSession;
public class CheckLoginServlet extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//SysUserService service 提供登录用户验证的方法checkUser(String login_name, String password);
HttpSession session = request.getSession();
String loginName = request.getParameter("loginName");
String password = request.getParameter("password");
//对登陆的用户进行查找
LoginUser loginUser = null;
loginUser = service.checkUser(loginName, password);
if (null != loginUser) //成功登陆
{
int role_id=loginUser.getRoleId();
loginUser.setSessionID(session.getId()); //设置成功登陆的session id
Role role=service.findRoleById(role_id);
UserInSession user_in_session=new UserInSession(loginUser.getId(),loginUser.getRealName(),role,loginUser.getDeptID());
//添加到在线用户列表
ServletContext application =this.getServletContext();
if (null == application.getAttribute(Constant.ONLINELIST_IN_APPLICATION)) //服务器中还没有在线人员列表,则创建这个列表
{
OnlineList list = new OnlineList();
application.setAttribute(Constant.ONLINELIST_IN_APPLICATION,list);
}
//这时application中肯定已经有在线人员列表
OnlineList list = (OnlineList) application.getAttribute(Constant.ONLINELIST_IN_APPLICATION);
if (list.isOnline(loginUser)) //如果该用户已经登陆了,则返回一个已经登陆错误到error.jsp
{
request.setAttribute("message","<div align='center'>出错原因可能如下:<br>1.该用户已经登陆了,同一个帐号不能同时多处登陆,请选择另外一个帐号登陆.<br>2.未安全退出,请等待一会后再登陆.<p><a href='index.jsp'>重新登陆</a></div>");
request.getRequestDispatcher("error.jsp").forward(request,response);
//list.getUserById(loginUser.getId()).setSessionID(loginUser.getSessionID());//更新已经有的session id为当前session id;
} else
{
list.addOnlineUser(loginUser);
application.setAttribute(Constant.ONLINELIST_IN_APPLICATION,list); //更新到Application的ONLINE_LIST
session.setAttribute(Constant.USER_IN_SESSION,user_in_session); //把登陆信息记录到Session中提供给其他应用使用
response.sendRedirect("main.jsp");
}
}
else //登陆失败,返回一个登陆失败错误到error.jsp,该用户不存在
{
request.setAttribute("message","登陆失败,该用户不存在或密码不正确!");
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
private SysUserService service;//使用到的SysUserService的对象
public void init() throws ServletException {
service = (SysUserService) ServiceFactory.getService(SysUserService.class);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -