如何在Java的普通类中获取Session以及request对象

2025-05-08 14:58:53
推荐回答(1个)
回答1:

你的类必须继承 ActionSupport

import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
private HttpServletRequest request;
public HttpServletRequest getRequest() {

ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context
.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
return request;
}
}

使用在类中使用“getRequest().getParameter("参数名");”获取request,session也一样