Struts ActionServlet

Why do we have only one ActionServlet in Struts?
(Asked in Polaris Interview for Java Experienced people , on April 11, Chennai)

Questions by khadarzone   answers by khadarzone

Showing Answers 1 - 11 of 11 Answers

ActionServlet is like BackBone of our application, we have done all actions(request & response) by using only one instance, that is ActionServlet's Instance, that is the main reason to go for MVC architecture, no need to create more than one instance, thats why we use only one ActionServlet for whole project.

In Previous Structure, we should create new instance for each and every request, in MVC no need.

Note of it "we have only one Backbone in our BODY"

The ActionServlet is the Controller comonent that handle the client request and determines which Action class will process each received request. As in MVC2 architecture only one controller component should govern the flow of application -i mean when request is generated ,it should be served by action class and based on the outcome next view is selected (internally RequestDispatcher forward()  method is used to choose next view). All business logic is executed by Action class ,so ActionServlet does not have such overhead that we need multiple instances .

What is ur take ?

ratheeshkn

  • Jun 9th, 2008
 

An action servlet is a servlet that is started by the servlet container of the web server in order to process the request.

The servlet receives a request fromt he action and asks the servlet container to pass the request to the forward's URL.

Its the primary component of the controller and it should be and instance or a sub class of org.apache.struts.action.ActionServlet.

  Was this answer useful?  Yes

he Jakarta Struts Framework does not support configuring more than one Struts ActionServlet in the Web deployment descriptor. This limitation occurs because there is only one servlet context per Web application and the commons digester imports the Struts configuration into the servlet context. Struts stores the required information as attributes in the servlet context, and the information is retrieved by using globally defined key names. Struts will suffix these attribute names with the prefix of the Struts modules, so several Struts modules are supported in a single Web application, but multiple Struts applications are not.

http://publib.boulder.ibm.com/infocenter/wpdoc/v510/index.jsp?topic=/com.ibm.wp.ent.doc/wps/multi_struts.html

pradeep_rmk

  • Nov 20th, 2008
 

If the question was why does we have only one instance of Action Servlet than the life cycle would give us some awnser. A servlet will be instatiated only once.
But for the current question why do we have only one ActionServlet, I am not sure if it is rule for MVC.

  Was this answer useful?  Yes

javareddy

  • Jan 8th, 2010
 

The Struts 1.x MVC has been designed based on core J2EE Design patterns. One of the prominent pattern is Front Controller pattern which takes the responsibility of receiving all the requests where as the Request Processor will act as the Application Controller. The struts Action Servlet is for handling all the request that end with a .do extension or any other defined extension in web.xml. 

  Was this answer useful?  Yes

apgenius

  • Jan 9th, 2010
 

This is so because struts implements MVC and in an MVC we generally (almost 99%) have only one controller. The ActionServlet acts as a controller.

  Was this answer useful?  Yes

Guys, I would like to look at this question from two perspectives:--

(1) Is it necessary to have only one ActionServlet in Struts? Are we not permitted?
No, its not necessary. We can have more than one [you can have 2/3/4/5...] ActionServlet in your web application.

The reason behind this is pretty simple. ActionServlet in itself extends HttpServlet and we can for sure have more than one servlets in our application which can extend HttpServlet [which we genrally have].

Now our container will treat more than one ActionServlet [in case we have] just like any other servlet and would deploy it for us.

In fact I verified with the below xml and it worked fine.

***************************************
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.shopping.action.YourActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>

<servlet>
<servlet-name>myaction</servlet-name>
<servlet-class>com.shopping.action.MyActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>


<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/struts/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myaction</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>

</web-app>

***************************************

(2) why do we usually have only one ActionServlet in Struts?
Because we would usually want to have single Controller which decides the future course of incoming requests.

Now, Guys think that why a perosn would want to have such a implementation in place:-

(a) Could be a requirement that he want to override / add some or the other feature which the base ActionServlet does....
--My recommendation for this would be to have that person create a custom RequestProcessor class because it provides a lot of hooks [methods] to over-ride which would server this purpose.

(b) It could be that a person would want to modularize a web application to greater extent.
--In that case I would suggest that their is a possiblity to create modules at the web application level and have a seperate struts-config-<module_name_here> file for each of it and that would suffice this requirement.

I can't think beyond this if a person would want to have more than one ActionServlet in a web application but if a person still wishes to do it... I would say 'best-of-luck'.


Thanks,
Vinay








  Was this answer useful?  Yes

Sturts is one type of framework so framework should combine different design patterns.In the same way struts follows FrontController design pattern. Because of that when we send the request from client for a particular page then it should be handled by one controller so in struts it is none other than ActionServlet

  Was this answer useful?  Yes

swapnil

  • Oct 1st, 2011
 

We can configure many servlets, pls check it.

  Was this answer useful?  Yes

pradeep simhadri

  • May 15th, 2012
 

ActionServlet acts as a controller in Struts 1.x,Structs is a Mvc2 architecture based web-framework, According to MVC2 there must be only one Controller in our app,

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions