jmcmahon
jmcmahon

Reputation: 630

How does a Struts2 action compare to a Servlet?

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

Upvotes: 4

Views: 6284

Answers (3)

leonbloy
leonbloy

Reputation: 76016

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.

Upvotes: 10

Johannes
Johannes

Reputation: 2070

Struts2 is a MVC framework implementation based on Java EE technology.

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120318

  1. Struts is an abstraction layer on top of the vanilla java servlet stuff. Actions themselves are defined by the programmer and are invoked by struts frameworks when a URL is hit (you configure what url maps to which action). So they don't really "compare" to a servlet, they are an abstraction around the functionality the servlet provides. One typical thing you do with an action is output a jsp, which is equivalent to a servlet. so what happens is a) request comes in, gets mapped to action b) action loads some data c) action renders a jsp, passing loaded data to the jsp.

  2. An action can output directly to the request/response, if that is what you want, but in most cases is probably not good practice.

Upvotes: 1

Related Questions