0

What is best practice to implement HttpServletRequest in Struts2?

  1. ServletActionContext - directly accessing the getRequest method from the ServletActionContext class
  2. ServletRequestAware : the action class implements ServletRequestAware interface
Ariana
  • 283
  • 1
  • 6
  • 17
  • Also, if you feel the interface way is better but want to avoid annoying code repetitions, just put the interface and the setter in the BaseAction extended by the others, only once – Andrea Ligios Mar 06 '17 at 11:27

1 Answers1

0

I couldn't see (or find documentation about) the benefits of 2. vs 1.

So why should I prefer

public class MyAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
}

When is worth enough

HttpServletRequest request = ServletActionContext.getRequest(); 
fustaki
  • 1,574
  • 1
  • 13
  • 20