⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 login.java

📁 这是本人曾经在公司里用的,内部开发框架,基于struts+hibernate今天分享给大家
💻 JAVA
字号:
/**
 * 
 */
package cn.bway.servlet;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import org.hibernate.Session;
import cn.bway.admin.vo.*;
import cn.bway.common.*;
import cn.bway.common.code.codeDao;
import cn.bway.common.dao.HibernateSessionFactory;

/**
 * @author Kson
 *
 */
public class Login extends HttpServlet implements Serializable {
	protected static final Logger log = LogManager.getLogger(Login.class);
    private static final String CONTENT_TYPE = "text/html; charset=utf-8";
    private static String ADDCODE_SESSION_NAME = "ADDCODE";
    
    //Initialize global variables
    public void init() throws ServletException
    {
    }
    
    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
    	
        HttpSession session = request.getSession();
        if (session.getAttribute(LoginConstants.GLOBLE_STAFF_ID) != null) {
            session.removeAttribute(LoginConstants.GLOBLE_STAFF_ID);
        }      
        if (session.getAttribute(LoginConstants.GLOBLE_STAFF_NAME) != null) {
            session.removeAttribute(LoginConstants.GLOBLE_STAFF_NAME);
        }
        if (session.getAttribute(LoginConstants.GLOBLE_STAFF_HASRIGHTS) != null) {
            session.removeAttribute(LoginConstants.GLOBLE_STAFF_HASRIGHTS);
        }
        if (session.getAttribute(LoginConstants.GLOBLE_STAFF_MENU) != null) {
            session.removeAttribute(LoginConstants.GLOBLE_STAFF_MENU);
        }
        if (session.getAttribute(LoginConstants.GLOBLE_STAFF_CN_NAME) != null) {
            session.removeAttribute(LoginConstants.GLOBLE_STAFF_CN_NAME);
        }
        RequestDispatcher disp = request.getRequestDispatcher("index.jsp");
        disp.forward(request, response);
    }
	
    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException   {
        String user = StringUtils.convertToISO(request.getParameter("user"));
        String password = StringUtils.convertToISO2(request.getParameter("password"));
        String addCode = request.getParameter("addCode");
        HttpSession session = request.getSession();
        Session sess;
        String realCode = "";
        List list = null;
        if (session.getAttribute(ADDCODE_SESSION_NAME) != null) {
            realCode = (String) session.getAttribute(ADDCODE_SESSION_NAME);
        }
        if (user == null || password == null) {
            session.setAttribute("errMsg","密码为空!");
            response.sendRedirect(request.getContextPath() + "/errormsgforindex.jsp");
            return;
        } else {
            if (realCode.equals("")) {
                session.setAttribute("errMsg","认证码为空!");
                response.sendRedirect(request.getContextPath() + "/errormsgforindex.jsp");
                return;
            }
            if (!realCode.equals(addCode)) {
                session.setAttribute("errMsg","认证码错误!");
                session.removeAttribute(ADDCODE_SESSION_NAME);
                response.sendRedirect(request.getContextPath() +"/errormsgforindex.jsp");
                return;
            } else {
                session.removeAttribute(ADDCODE_SESSION_NAME);
            }
            StaffInfoVO vo = new StaffInfoVO();
            vo.setStaffname(user);
            vo.setPassward(password);
            StaffInfoVO tempVO = new StaffInfoVO();
            List menuNum = new ArrayList();
            sess = HibernateSessionFactory.currentSession();
            Connection conn =null;
            conn = sess.connection();
            try {
            	 try {
						list = UtilSQL.getStaffInfo(vo,conn);
						
					} catch (BwayHibernateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	                if (list == null || list.size() <= 0) {
	                    session.setAttribute("errMsg","密码错误!");
	                    response.sendRedirect(request.getContextPath() + "/errormsgforindex.jsp");
	                    return;
	                }
	                Iterator iterator = list.iterator();
	                while (iterator.hasNext()) {
	                    tempVO = (StaffInfoVO) iterator.next();
	                }
            } catch (Exception ex) {
                session.setAttribute("errMsg",ex.getMessage());
                response.sendRedirect(request.getContextPath() +  "/errormsgforindex.jsp");
                return;
            }finally {
            	try {
            		if(null !=conn){
            				conn.close();
            		}
				} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
            	}
            	HibernateSessionFactory.closeSession();
            }
            
            String ipAddr = PublicUtility.getRemoteIP(request);
            LoginVO loginVO = new LoginVO();
            loginVO.setIpAddress(ipAddr);
            loginVO.setUserName(tempVO.getStaffname());
            loginVO.setUserId(Integer.parseInt(tempVO.getStaffid()));
           
            session.setAttribute(LoginConstants.GLOBLE_STAFF_MENU, menuNum);
            session.setAttribute(LoginConstants.GLOBLE_STAFF_ID,tempVO.getStaffid());
            session.setAttribute(LoginConstants.GLOBLE_STAFF_NAME,tempVO.getStaffname());
            session.setAttribute(LoginConstants.GLOBLE_STAFF_CN_NAME,tempVO.getStaffcnname());
            session.setAttribute(LoginConstants.GLOBLE_LOGINVO,loginVO);
            response.sendRedirect(request.getContextPath() + "/main.jsp");
        }
    }
    
    private List getSubMenuList(List menuList, String labelId) {
        List resultList = new ArrayList();
        for (int i = 0; i < menuList.size(); i++) {
            LabelVO labelVO = menuList.get(i) == null ? new LabelVO() : (LabelVO) menuList.get(i);
            if (labelVO.getParentId().equals(labelId)) {
                resultList.add(labelVO);
            }
        }
        return resultList;
    }
    
    //Clean up resources
    public void destroy()
    {
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -