If your question is why you need servlets if you're using Struts -since you mostly write ActionBeans and other classes, none of which implement Servlet-, then the answer is that something has to listen to the HTTP requests. The servlets that are part of Struts do that for you, abstracting away the servlet details. But without servlets underneath, nobody would be able to listen and respond to HTTP requests.
Joined: May 24, 2007
Thanks , can some one tell me in brief working of servlets in struts , how servlet is calling and how it is responsing back to the request
The user fills in the required data (input) and submits the form
The request parameters are wrapped into (through HTTP, thereby HttpRequest) HttpRequest and it is sent to WebServer or WebContainer.
WebContainer looks into the contract between the application and itself -> this is done through a file called deployment descriptor. In case of web applications, its "web.xml".
Say, in case of your url being -> http://localhost:8080/MyStrutsApp/login.do, the webserver receives a request for the resource "login.do" for the web application "MyStrutsApp".
The WebContainer finds the corresponding resource to forward the control. In case of Struts, it would be an ActionServlet (mostly the default ActionServlet of apache's org.apache.struts.action.ActionServlet) and the control is delegated to it along with the HttpRequest and HttpResponse objects.
ActionServlet knows what to do with the incoming request. It has to read something called struts specific deployment descriptor which is called as "struts-config.xml". It reads the file and finds a matching entry for the incoming request "login" in one of the <action> elements which is present inside the <action-mapping> element.
ActionServlet instantiates the corresponding Action class (if it does not exist) and ActionForm object by populating the ActionForm with the matching input values. Then it forwards the control to the execute() method of Action class along with the HttpRequest, HttpResponse, ActionMapping and ActionForm objects.
execute() method can do whatever it wants -> Your Business Logic lies here
The execute() method returns the ActionMapping object back to the ActionServlet which invoked it. The ActionMapping object contains the key to be evaluated for further redirecting the control
The ActionServlet unwraps the ActionMapping and extracts the key and again looks into the struts-config.xml for the <action> tag (which will contain the <forward> elements if specific to the <action>) . If it does not find an entry,it looks into <global-forwards> element where the forward entries are present for the entire application.
The ActionServlet then forwards the control along with the request, response to the found out resource which would typically be the JSP.
The jsp page retreives the results and displays it.