在SSH中调用HttpServletRequest问题
初学SSH,在登录时我根据书上的用session来保存用户已登录的状态,但是执行HttpSession session = request.getSession();时出错了,写的是
java.lang.NullPointerException com.service.UserService.saveUserInSession(UserService.java:68)
我的代码如下:
package com.service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.jasper.tagplugins.jstl.core.Out;
import com.hibernate.Users;
import com.hibernate.UsersDAO;
public class UserService
{
private Users user;
private UsersDAO usersDAO;
private HttpServletRequest request;
public void setUsersDAO(UsersDAO usersDAO)
{
this.usersDAO = usersDAO;
}
public void setServletRequest(HttpServletRequest request)
{
this.request = request;
}
/*
* login validation
*/
public boolean login(String userName,String password)
{
user = usersDAO.findById(userName);
if(user == null)
{
System.out.println("user is null");
return false;
}
if(user.getPassword().equals(password))
{
return true;
}
return false;
}
/*
* register validation
*/
public boolean regiser(String userName,String email,String password,String rePassword)
{
user = new Users(userName, rePassword, email);
usersDAO.save(user);
return true;
}
public boolean userNameUnique(String userName)
{
user = usersDAO.findById(userName);
if(user != null){
return false;
}
else{
return true;
}
}
/*
* Login successfully,save userName in session and set session‘s effective time
*/
public void saveUserInSession(String userName)
{
user = usersDAO.findById(userName);
HttpSession session = request.getSession();
session.setAttribute("userName", user.getUserName());
session.setMaxInactiveInterval(60*60*3);
}
}
是不是要对HttpServletRequest在Spring中注入
如果是求HttpServletRequest的配置方式
------解决方案--------------------
http://www.iteye.com/topic/581820