📄 loginaction.java
字号:
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.IOException;
import javax.servlet.ServletException;
import java.util.ArrayList;
import org.hibernate.*;
import org.hibernate.cfg.*;
/**
* @author Chao Wu
*
* Description: This class is a Struts Action class used to do the validation
* work and login to the system.
* <p>
*
* Copyright:
* <p>
*/
public class LoginAction extends Action {
/**
* This method deals with the actions need to login
*
* @param mapping
* @param form
* @param req
* @param res
* @return
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
LoginForm loginForm = (LoginForm) form;
// Get the username and password from user input
String username = loginForm.getUsername();
String password = loginForm.getPassword();
try {
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
// Select the users from database with the username
String sqlQuery = "select u from User u where u.username='"
+ username + "'";
Query lQuery = session.createQuery(sqlQuery);
ArrayList userList = (ArrayList) lQuery.list();
tx.commit();
session.close();
User user = new User();
// There is such a user
if ((null != userList) && (userList.size() > 0)) {
user = (User) userList.get(0);
if (!user.getPassword().equals(password)) {
return mapping.findForward("failure");
}
} else {
// There is no such user
return mapping.findForward("failure");
}
} catch (HibernateException e) {
e.printStackTrace();
return mapping.findForward("failure");
}
HttpSession httpSession = req.getSession();
httpSession.setAttribute("buylist", new ArrayList());
Double totalprice = new Double(0);
httpSession.setAttribute("totalprice", totalprice);
// Successfully login
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -