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

📄 loginaction.java

📁 养殖信息的网站
💻 JAVA
字号:
package com.test.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.*;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.Query;
import org.hibernate.Session;
import com.test.form.LoginForm;
import com.test.HibernateUtil;
import com.test.Users;
import yy.jdbc;

public class LoginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm srcForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = mapping.findForward("frontPage");
        LoginForm form = (LoginForm) srcForm;
        try {
            // 检查用户名、密码是否合法
        	
            Session dbSession = HibernateUtil.currentSession();
            HttpSession session = request.getSession();
            String sqlStr = "from Users as u where u.userName=:username and u.pwd=:pwd";
            Query query = dbSession.createQuery(sqlStr);
            query.setString("username", form.getUserName());
            query.setString("pwd", form.getPassword());
            List result = query.list();
            if (result.size() == 0) {
                // 不合法
                forward = mapping.findForward("failed");
            } else {
                // 合法后保存到Session中
            	Users user = (Users) result.get(0);
            	session.setAttribute("UID", user.getId());
                session.setAttribute("UserName", user.getUserName());
                session.setAttribute("UserClass", user.getPower());
                //System.out.println(user.getId());
                forward = mapping.findForward("frontPage");
            }
        } catch (Exception e) {
            forward = mapping.findForward("errorPage");
        }

        return forward;
    }
}

⌨️ 快捷键说明

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