I suppose I am increase redundancy over Internet by writing this article. You’ll find many of articles on Internet over struts. But only some of them has explained how easier strut is.
JSP is nothing but a HTML page with java scriplets. They were designed to reduce Servlet work load & length. Imagine a well designed HTML page with 2 input box and 1 submit button. You fill HTML form & submit the request to a servlet. Servlet is not aware where the request is coming from. It might be some awt application or web page or web service etc. So it just respond with sum of values of both textbox. What happens next?……… content of your whole HTML page will be erased and you will see just a numeric value on that. That’s why JSP was discovered.
Now what is Servlet?
Servlet is nothing but a special java class running on server hidden from external people. A person can request/invoke it with its alias name written in web.xml. For each request to the webserver, container reads web.xml. Read aliases and calls actual java class who matches with that alias. For the convenience J2EE provides some classes for JSP & SERVLET;
- Request : to read a request from a client
- Response : To give response to client
- Session : To remember client who requested last time
- Cookie : To store some data at client’s computer.
Read more about it here
Lets start with Strut 1.
Strut is a framework over JSP & Servlet. It divides a Servlet class into 2 parts as per its working: Action & POJO class (or Controller & Model respectively). JSP is just for display/view purpose.
Controller just listen clients. Means it reads client requests and response them like a receptionist. It takes help from other java classes to fulfill the request. These java classes are caller Model. It is just like a restaurant where waiter listen your order and serve you meal. But chef inside kitchen actually cooks food.
This sort of architecture is called MVC(Model-view-controller) architecture.
|Action Servlet||You need this one per application and its supplied by Struts itself. So you don’t write or modify it.|
|Form Beans||For each HTML form one form bean is written. They are just Java beans. When a request to Struts Action Servlet arises. it calls setters on the form bean and populate its field member form request parameters.|
|Action Objects||It has a call-back-like method called execute() which is a great place to get the validated form params and call model components.
its kind of a servlet lite
|struts-config.xml||This is the Struts-specific deployment descriptor, in it you will map
How struts work?
In short: A request come to web container (say Tomcat) who looks for alias in web.xml. It looks for URL pattern since eince exact URL alias is not present in web.xml. It sends request to ActionServlet. This servlet reads another configuration file (strut-config.xml) who actually has entries of all action classes. It calls setters of related form bean class and populate its fields with request parameters. Pass this form & request to relevant Action class.
See what actually happens:
- Action Servlet (provided into strut bundle, we don’t write our own) needs to be mapped in web.xml same as other servlet are mapped.
- When a hit comes to Action Servlet, it reads strut-config.xml to know actual mapping (like which Action class or ActionForm is mapped and what are their initial values).
- What ActionServlet does next with ActionForm
- It looks for form bean object into session. If it doesn’t present makes new one. (A form bean is simple java bean extends ActionForm)
- Calls reset() of ActionForm and populates it by calling all setters.
- If validate=true is set into action-mapping then it calls bean’s validate() of ActionForm.
- If form bean returns a non-empty errors object then it pass error object to input page or error page as per its action-mapping.
- If ActionError is empty then ActionServlet creates an object of relevant action class, pass the populated & validates form bean object, and calls action object’s execute().
- JSP: When a JSP with an html:form tag is rendered, the html:form tag uses its action path to look-up its action-mapping. Like the ActionServlet, it uses the form-bean to check for a ActionForm in session. If it doesn’t find one, it creates one. The new or pre-existing ActionForm is then used by the other html:tags to populate their elements. So the form fields get populated automatically if the form has been submitted once.
- form-bean name specified in action-mapping is used as the default attribute name in session.
- If you are using same form for 2 or multiple actions with the same name in action-mapping, you will ever get the form with last submitted values. To avoid this, use the same ActionForm but with different name in action-mapping.
- Similarly, If you submit a form from a JSP page and forward to the same JSP page again from action class or ActionForm, you’ll get the form filled with submitted values even if the values are modified by setters or validate().
- html:tags uses form-bean name from session by default. You can change bean scope as per your need.
Note: if it finds no action mapping, it set error in response header and return back.
Note: it calls setter for all methods which are coming as request parameter. So there is no problem if form bean has an extra setter. But if there is an extra request parameter and form bean has no corresponding setter then it’ll generate run time error.
Note: If there is no page defined action-mapping as input page then it does nothing.
Note: if ActionForm’s setters or validate() modify actual values of form then they will not be reflected in session. But updated ActionForm will be passed to Action class.
You’ll have to read strut tags similar to any other tag library like jsp, jakarta, apache, JSTL etc. Tags help in avoiding direct java code in JSP.