无法获取servlet请求对象和会话
我试图获取会话数据,但我无法获取它,因为我无法获取请求对象... 代码是...
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...
i m trying to get session data,but i m not able to get it bcoz i m not able to get request object...
code is...
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";
}
}
it gives nullpointerexception when i m getting session that portion i mark...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有当前会话并且 create 为 true,则返回一个新会话。
更新:
您的
请求
为null
,因为您从未在setServletRequest()
中设置HttpServletRequest
>。尝试
代替
if there is no current session and create is true, returns a new session.
UPDATE:
your
request
isnull
cause you never set theHttpServletRequest
insetServletRequest()
.Try
instead of