我试图获取会话数据,但我无法得到它bcoz我无法获取请求对象... 代码是......
package secondary.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class GetUserData extends ActionSupport implements ServletRequestAware
{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.setServletRequest(arg0);
session = request.getSession();
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public GetUserData()
{
}
public String getUserLocation()
{
session = request.getSession(); // <- here
String []usr_dtl = session.getAttribute("USERDETAIL").toString().split("@");
System.out.println("User name = " + usr_dtl[6]);
return "DONE";
}
}
当我得到会话时,我会给出nullpointerexception ...
答案 0 :(得分:0)
session = request.getSession( true );
如果没有当前会话且create为true,则返回一个新会话。
<强>更新强>
您的request
为null
因为您从未在HttpServletRequest
中设置setServletRequest()
。
试试
this.setRequest(arg0);
而不是
this.setServletRequest(arg0);