Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a Struts2 action compare to a Servlet?

How do Struts2 actions compare to Servlets? Can an action act as a servlet?

like image 496
jmcmahon Avatar asked Dec 22 '22 01:12

jmcmahon


1 Answers

A Struts (Struts1/Struts classic) action was more tied to a servlet. In Struts2, things are quite different. A Struts2 action is just a POJO (plain Java class), totally decoupled from the Servlet API. This decoupling eases testing.

In the typical workflow of a Struts2 webapp, an action will be instantiated for each request and will be associated with a Servlet (it can implement the ServletAware interface if it needs to be aware of this association; normally this is not necessary nor advisable).

An important conceptual difference with Servlets (and with Struts actions) is that Struts2 actions are not reused for different requests, and hence are thread safe: say, it can happen that three http requests (simultaneous or not) are served by one servlet instance; but inthat case we will still have three different Struts2 action instances, one for each request.

like image 88
leonbloy Avatar answered Feb 20 '23 15:02

leonbloy