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

发布于 2024-11-14 16:43:02 字数 1084 浏览 1 评论 0原文

我试图获取会话数据,但我无法获取它,因为我无法获取请求对象... 代码是...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2024-11-21 16:43:02
session = request.getSession( true );

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

更新:

您的请求null,因为您从未在setServletRequest()中设置HttpServletRequest >。

尝试

this.setRequest(arg0);

代替

this.setServletRequest(arg0);
session = request.getSession( true );

if there is no current session and create is true, returns a new session.

UPDATE:

your request is null cause you never set the HttpServletRequest in setServletRequest().

Try

this.setRequest(arg0);

instead of

this.setServletRequest(arg0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文