无法获取servlet请求对象和会话

时间:2011-06-09 13:41:28

标签: session-variables servlets

我试图获取会话数据,但我无法得到它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 ...

1 个答案:

答案 0 :(得分:0)

session = request.getSession( true );

如果没有当前会话且create为true,则返回一个新会话。

<强>更新

您的requestnull因为您从未在HttpServletRequest中设置setServletRequest()
试试

this.setRequest(arg0);

而不是

this.setServletRequest(arg0);