?? loginaction.java
字號(hào):
package com.stuman.web.struts.action;import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;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.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import com.stuman.dao.hibernate.HibernateUtil;import com.stuman.domain.Admin;import com.stuman.domain.Student;import com.stuman.domain.Teacher;import com.stuman.web.struts.form.LoginForm;public class LoginAction extends Action { // --------------------------------------------------------- Instance // Variables // --------------------------------------------------------- Methods /** * Method execute * * @param mapping * @param form * @param request * @param response * @return ActionForward * @throws UnsupportedEncodingException */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { // TODO Auto-generated method stub HttpSession session = request.getSession(); Session s = HibernateUtil.currentSession(); LoginForm loginForm = (LoginForm) form; String sort = loginForm.getSort(); String username = loginForm.getUsername(); String password = loginForm.getPassword(); int loginSort = Integer.parseInt(sort); String[] userlist = new String[2]; userlist[0] = username; userlist[1] = password; try { HibernateUtil.beginTransaction(); String str = new String(); switch (loginSort) { case 1: str = " from Student as stu where stu.name=:stuName and stu.password=:stuPassword"; Query query = s.createQuery(str); System.out.println(username + " " + password); query.setString("stuName", username); query.setString("stuPassword", password); if (query.list().size() > 0) { session.setAttribute("stuid", ((Student) query.list().get(0)).getId()); s.close(); return mapping.findForward("studentLoginsuccess"); } else break; case 2: str = " from Teacher tea where tea.name = '" + username + "' and tea.password ='" + password + "'"; if (s.createQuery(str).list().size() > 0) { session.setAttribute("teaid", ((Teacher) s.createQuery(str) .list().get(0)).getId()); s.close(); return mapping.findForward("teacherLoginsuccess"); } else break; case 3: str = " from Admin admin where admin.name = '" + username + "' and admin.password ='" + password + "'"; System.out.println(username + " " + password); if (s.createQuery(str).list().size() > 0) { session.setAttribute("id", ((Admin) s.createQuery(str) .list().get(0)).getId()); s.close(); return mapping.findForward("adminLoginsuccess"); } else break; default: break; } } catch (HibernateException e) { e.printStackTrace(); } finally { s.close(); } ActionMessages errors = new ActionMessages(); errors.add("login error", new ActionMessage("login.error")); saveErrors(request, errors); return mapping.getInputForward(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -