欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2中获取request、response,与android客户端进行交互(转载)

程序员文章站 2022-04-23 16:53:08
...

用struts2作为服务器框架,与android客户端进行交互需要得到request、response对象。

struts2中获取request、response有两种方法。

第一种:利用ServletActionContext的静态方法

Struts2 利用ServletActionContext类来维护Servlet对象,ServletActionContext利用ThreadLocal来维护 不同线程的Servlet对象,因此可以使用ServletActionContext类获取,这种方法可以叫做非注射方式(非IoC方式)。

[代码]java代码:
01 public class LoginAction extends ActionSupport {
02   
03         private static final long serialVersionUID = 1L;
04           
05         private String account;//账号
06          private String pwd;//密码
07           
08         public String login() throws Exception{
09                   
10                 //获取request
11                  HttpServletRequest request=ServletActionContext.getRequest();
12                  //获取response
13                  HttpServletResponse response=ServletActionContext.getResponse();
14                  //获取Session
15                  HttpSession session=request.getSession(true);
16                  //放到session中
17                  session.setAttribute("account", account);
18                  //获得上下文
19                  ServletContext context=ServletActionContext.getServletContext();
20                  //获取upload文件夹
21                  File uploadFile=new File(context.getRealPath("upload"));
22                   
23                 //给客户端返回结果
24                  PrintWriter writer=response.getWriter();
25                  writer.print("account");
26                   
27                 return execute();
28          }
29  }

第二种:利用相关的Aware接口

Struts2 提供Aware接口,实现了相关Aware接口的Action能够感知相应的资源。Struts在实例化一个Action事例中,如果发现它实现了相应的 Aware接口,会把相应的资源通过Aware接口方法注射进去,这种方法可以叫做注射方式(IoC方式)。

[代码]java代码:
01 public class ServletAwareAction extends ActionSupport implements
02                  ServletRequestAware, ServletResponseAware, SessionAware, ServletContextAware {
03   
04         private static final long serialVersionUID = 1L;
05   
06         private ServletContext application;//Servlet上下文
07          private HttpServletRequest request;//request对象
08          private HttpServletResponse response;//response对象
09          private Map<String, Object> session;//session对象
10           
11         public void setServletRequest(HttpServletRequest request) {
12                  this.request=request;
13          }
14   
15         public void setServletResponse(HttpServletResponse response) {
16                  this.response=response;
17          }
18   
19         public void setSession(Map<String, Object> session) {
20                  this.session=session;
21          }
22   
23         public void setServletContext(ServletContext application) {
24                  this.application=application;
25          }
26   
27         public String execute(){
28                  String upload=application.getRealPath("upload");
29                   
30                 request.getRemoteAddr();
31                  response.getContentType();
32                  session.get("account");
33                   
34                 return Action.SUCCESS;
35          }

Aware接口其实是一种拦截器,拦截器代码会在执行Action之前执行,将相关的Servlet对象设置进去