📄 loginservlet.java
字号:
package com.galaxy.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.galaxy.dao.LoginDao;
import com.galaxy.dao.UserInfoDAO;
import com.galaxy.vo.UserInfoVO;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* 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 {
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 {
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
//从前台获取用户名和密码信息
String username = request.getParameter("username");
String userpsw = request.getParameter("userpswd");
//查询数据库中的用户信息
UserInfoDAO uiDao = new UserInfoDAO();
UserInfoVO uivo = new UserInfoVO();
List userList = new ArrayList();
String condition = " and ui_loadname = '" + username +
"' and ui_password = '" + userpsw + "'";
userList = uiDao.queryByCondition(condition);
if(userList != null)
{
for(int i=0; i < userList.size(); i++)
{
uivo = (UserInfoVO)userList.get(i);
break;
}
}
/**验证从数据库提取出来的用户数据,是否有效
*/
//判断用户是否存在,然后决定跳转与否
if( (uivo.getUiRealname()!= null) && !("".equals(uivo.getUiRealname()))
&& uivo.getUiPassword() != null && !("".equals(uivo.getUiPassword())))
{
//把用户角色信息信息保存在session里,随其转发
HashMap upHashMap = getUserPriorityHashMap(uivo.getUiId());
request.getSession().setAttribute("upHashMap", upHashMap);
String [] menuInfo = getMenuInfos(upHashMap);
request.getSession().setAttribute("item_word", menuInfo[0]);
request.getSession().setAttribute("item_link", menuInfo[1]);
request.getSession().setAttribute("supermenu_num", menuInfo[2]);
request.setAttribute("errorMsg", "");
request.getRequestDispatcher("frameset.jsp").forward(request, response);
}
else
{
request.setAttribute("errorMsg", "登陆信息错误,请重新输入!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
/**
*@ 根据用户信息,把用户的ID,姓名,登陆名,角色ID,是否是管理员,能访问的系统模块,
*存入一个HashMap的结构体里
*结构体的key值分别是:U_ID, U_Name, U_LoadName, U_RoleID, U_IsAdmin, &
* 和数据库中存入的各模块名
*模块名(KEY)对应的值为'0,1','0'表示不能访问,'1'表示能访问
*/
public static HashMap getUserPriorityHashMap(Long userID)
{
HashMap upHashMap = new HashMap();
LoginDao ld = new LoginDao();
upHashMap = ld.getUserPriorityInfo(userID);
return upHashMap;
}
/**
* 计算前台菜单信息,返回数组的第一个串是前台菜单名字
* 第二个串是前台菜单的链接路径
* 第三个串是前台顶级菜单的个数
*/
public static String[] getMenuInfos(HashMap upHashMap)
{
//计算顶级菜单的个数
int supermenu_num = 0;
String [] menuInfo = new String[3];
menuInfo[0]="";
menuInfo[1]="";
menuInfo[2]="";
Long roleID = (Long)upHashMap.get("U_RoleID");
//人员档案模块
if("1".equals(upHashMap.get("人员档案")))
{
supermenu_num += 1;
menuInfo[0] +="||||";
menuInfo[1] +="||||";
if("1".equals(upHashMap.get("基本信息")))
{
menuInfo[0] +="&基本信息";
menuInfo[1] +="%PA_UserInfoServlet?opflag=query";
}
if("1".equals(upHashMap.get("员工档案")))
{
menuInfo[0] +="&员工档案";
menuInfo[1] +="%PA_EmployeeFileServlet?opflag=query";
}
if("1".equals(upHashMap.get("员工履历")))
{
menuInfo[0] +="&员工履历";
menuInfo[1] +="%PA_EmployeeRecordServlet?opflag=query¤tpage=1&cond=&ui_name=";
}
if("1".equals(upHashMap.get("员工合同")))
{
menuInfo[0] +="&员工合同";
menuInfo[1] +="%PA_EmployeeContractServlet?opflag=query¤tpage=1&selectdep=&ui_name=";
}
if("1".equals(upHashMap.get("奖惩管理")))
{
menuInfo[0] +="&奖惩管理";
menuInfo[1] +="%PA_RewardPunishmentServlet?opflag=query¤tpage=1";
}
}
//人事调配模块
////////////////////////////////////////////////////
if("1".equals(upHashMap.get("人事调配")))
{
supermenu_num += 1;
menuInfo[0] +="||||";
menuInfo[1] +="||||";
if("1".equals(upHashMap.get("人事调动管理")))
{
menuInfo[0] +="&人事调动管理";
menuInfo[1] +="%psl_dispatch//PD_MainDispatchServlet?opflag=";
}
if("1".equals(upHashMap.get("离退人员管理")))
{
menuInfo[0] +="&离退人员管理";
menuInfo[1] +="%psl_dispatch//PD_DispatchQueryServlet?opflag=retireload";
}
if("1".equals(upHashMap.get("人事调配查询")))
{
menuInfo[0] +="&人事调配查询";
menuInfo[1] +="%psl_dispatch//PD_DispatchQueryServlet?opflag=login";
}
}
//教育培训模块
/////////////////////////////////////////////////////
if("1".equals(upHashMap.get("教育培训")))
{
supermenu_num += 1;
menuInfo[0] +="||||";
menuInfo[1] +="||||";
if("1".equals(upHashMap.get("员工培训报名")))
{
menuInfo[0] +="&员工培训报名";
menuInfo[1] +="%training//TrainRegister.jsp";
}
if("1".equals(upHashMap.get("报名信息")))
{
menuInfo[0] +="&报名信息";
menuInfo[1] +="%training//TR_TrainRegisterServlet?opraParam=trainregister";
}
if("1".equals(upHashMap.get("培训类别维护")))
{
menuInfo[0] +="&培训类别维护";
menuInfo[1] +="%training//TR_TrainClassServlet?opraParam=trainclass";
}
if("1".equals(upHashMap.get("培训记录")))
{
menuInfo[0] +="&培训记录";
menuInfo[1] +="%training//TR_TrainRecordServlet?opraParam=trainrecord";
}
if("1".equals(upHashMap.get("培训人员及成绩")))
{
menuInfo[0] +="&培训人员及成绩";
menuInfo[1] +="%training//TR_TrainScoreServlet?opraParam=trainscore";
}
if("1".equals(upHashMap.get("员工证书记录")))
{
menuInfo[0] +="&员工证书记录";
menuInfo[1] +="%training//TR_CertRecordServlet?opFlag=init";
}
}
//系统管理模块
/////////////////////////////////////////////////////
if("1".equals(upHashMap.get("系统管理")))
{
supermenu_num += 1;
menuInfo[0] +="||||";
menuInfo[1] +="||||";
if("1".equals(upHashMap.get("机构级别维护")))
{
menuInfo[0] +="&机构级别维护";
menuInfo[1] +="%SM_LevelInfoServlet?opflag=init";
}
if("1".equals(upHashMap.get("机构信息维护")))
{
menuInfo[0] +="&机构信息维护";
menuInfo[1] +="%SM_DeptInfoServlet?opflag=init";
}
if("1".equals(upHashMap.get("用户管理")))
{
menuInfo[0] +="&用户管理";
menuInfo[1] +="%SM_UserInfoServlet?opflag=init";
}
if("1".equals(upHashMap.get("角色管理")))
{
menuInfo[0] +="&角色管理";
menuInfo[1] +="%SM_RoleInfoServlet?opflag=init";
}
if("1".equals(upHashMap.get("系统模块管理")))
{
menuInfo[0] +="&系统模块管理";
menuInfo[1] +="%sys_manage//sys_modelmanage_show.jsp";
}
}
///////////////////////////////
//去掉字符串前端的字符"||||"
menuInfo[0] = menuInfo[0].substring(4);
menuInfo[1] = menuInfo[1].substring(4);
menuInfo[2] = String.valueOf(supermenu_num);
//////////////////////////////////
return menuInfo;
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -