wood burning stoves 2.0*
The moose likes Struts and the fly likes flow of servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "flow of servlets" Watch "flow of servlets" New topic
Author

flow of servlets

anita dhar
Ranch Hand

Joined: May 24, 2007
Posts: 34
actually i am confusing how servlets acts as a controller in struts framework what is the use of servlets
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
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.


Ping & DNS - my free Android networking tools app
anita dhar
Ranch Hand

Joined: May 24, 2007
Posts: 34
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
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi anitha,


  • 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.



  • HtH.
    [ May 24, 2007: Message edited by: Raghavan Muthu ]

    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Rahul Bhattacharjee
    Ranch Hand

    Joined: Nov 29, 2005
    Posts: 2308
    Another reason for having servlets in struts framework might be its a single entry point to the application that is built on top of struts.Front controller pattern.

    Well the main reason still remains that its like acceptor of HTTP requests from clients.


    Rahul Bhattacharjee
    LinkedIn - Blog
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Struts questions are best asked in our Struts forum.
    Moving...


    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: flow of servlets
     
    Similar Threads
    a jsp design question...
    server side processing error
    WA #1.....word association
    Downloading HTML page
    Un-downloadable Applets