如何获取 HttpServletRequest 、HttpSession、ServletContext 、HttpServletResponse:
方法一,通过ServletActionContext类直接获取:
public String rsa() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext servletContext = ServletActionContext.getServletContext();
request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();
return "success";
}
方法二,实现指定接口,由struts运行时注入:
public class HelloWorldAction implements ServletRequestAware, ServletResponseAware,ServletContextAware{
private HttpServletRequest request;
private ServletContext servletContext;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest req){
this.request = req;
}
public void setServletContext(ServletContext context){
this.servletContext = context;
}
public void setServletResponse(HttpServletResponse response){
this.reponse = response;
}
}
方法三,由ActionContext获取
public String execute() throws Exception{
ActionContext ctx = ActionContext.getContext();
ctx.getApplication().put("app", "应用范围");
ctx.getSession().put("see", "session范围");
ctx.put("req", "request范围");
return "success";
}
相关推荐
HttpServletRequest HttpServletResponse HttpSession HttpSessionBindingEvent HttpSessionBindingListener HttpSessionContext HttpUtils RequestDispatcher Servlet ServletConfig ServletContext ...
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); // 设置响应内容格式和编码格式...
HttpServletRequest HttpServletRequestWrapper HttpServletResponse HttpServletResponseWrapper HttpSession HttpSessionActivationListener HttpSessionAttributeListener HttpSessionBindingEvent ...
public String login(HttpServletRequest request, HttpServletResponse response) { return "login/main"; } @MappingMethodAnnotation(mappingMethod = "/loginvm.do") public SimpleModelView loginView...
// protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // //解决POST表单的中文乱码问题 // request.setCharacterEncoding("utf-8"); // ...
doDelete(HttpServletRequest, HttpServletResponse) - Method in class javax.servlet.http.HttpServlet Called by the server (via the service method) to allow a servlet to handle a DELETE request. ...
response表示HttpServletResponse对象,并提供了几个用于设置送回 浏览器的响应的方法(如cookies,头信息等) out对象是javax.jsp.JspWriter的一个实例,并提供了几个方法使你能用于向浏览器回送输出结果。 ...
javax.servlet.ServletContext javax.servlet.ServletRequest javax.servlet.http.HttpUtils javax.servlet.ServletResponse javax.servlet.ServletException javax.servlet.http.HttpServlet javax.servlet....
它是HttpServletRequest类的实例。 序号 方 法 说 明 1 object getAttribute(String name) 返回指定属性的属性值 2 Enumeration getAttributeNames() 返回所有可用属性名的枚举 3 String ...
如果有助于你处理应用的数据需求,你也许需要绑定对象到Session中,你可以通过一个唯一的名字绑定任何的对象到Session中,这时,你需要使用HttpSession对象。任何绑定到Session上的对象都可以被处理同一会话的...
javax.servlet.ServletContext.class javax.servlet.RequestDispatcher.class javax.servlet.Servlet.class javax.servlet.ServletException.class javax.servlet.ServletRequest.class javax.servlet.Servlet...
redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 23、EJB与JAVA BEAN的区别? Java Bean 是可...
redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 20、EJB与JAVA BEAN的区别? Java Bean 是可复用...