Monday 30 April 2007

Struts Request Processing illustration, the first sight...

The following Figure shows how a request flows through the Struts framework. Follow step by step....

Step 1. The request calls the ActionServlet with a certain logical input form name.

Step 2. This logical form name is then resolved according to mapping entries defined in action.xml. This file contains the mapping that defines which Struts components are associated with which logical form.

Once the mapping is resolved, the ActionServlet will look in the session for the ActionForm class associated with the request. If such a class is located in the session, the ActionForm attributes are populated by calling the corresponding setter methods. However, if the ActionForm is not located in the session, a new instance is created and added to the user’s session. Subsequently, the reset method is called on the action form and the ActionForm attributes are populated.

Step 3/4. Next, the ActionServlet calls the validate method on the ActionForm. The validate method returns either null (indicating success) or an array of errors (indicating an error condition). If the ActionForm’s validate method returns an array of errors, the ActionServlet populates the requesting input form and displays the errors to the user. we can disable the call to this method if we define validate=false in struts config file. we can disable the validate for all the action forms if we define init parameter validate with value false in web.xml for ActionServlet(Controller).

Step 5. Once the validate method returns null, the ActionServlet will instantiate an instance of ActionClass (if one does not already exist) as indicated by the mapping in action.xml. The ActionClass execute method is then invoked by ActionServlet. The execute method is responsible for processing the request.

Step 6. The execute method is required to return an ActionForward instance. This instance indicates to the ActionServlet which JSP (if any) to forward the request off to for rendering of the presentation.

Step 7. The JSP is responsible for generating the appropriate response based on the results of executing the execute method. In Struts, an ActionForm bean has only one purpose -- to represent the last input state of all the user's entries on a particular form. the ActionForms are also called the transfer objects between the controller and the Views because they carry the values from controller (Action) to the Views (Jsp).
That way, the framework can easily redisplay the last input screen (plus some error messages) pre-populated with what the user last entered, which is what anyone who has ever used a GUI application expects.

May be the request processing seems to be complex at first sight, but once you are acquinted with it...it would put you one the drive!!!!. Enjoy Struts!!!!!

Struts 2 : features and short summary

The strut-2 framework is designed for the compilation of the entire development cycle including of building, developing and maintaining the whole application. It is very extensible as each class of the framework is based on an Interface and all the base classes are given an extra application and even you can add your own. The basic platform requirements are Servlet API 2.4, JSP API 2.0 and Java 5.

Some of the general features of the current Apache Strut 2 framework are given below.

Architecture – First the web browser request a resource for which the Filter Dispatcher decides the suitable action. Then the Interceptors use the required functions and after that the Action method executes all the function like storing and retrieving data from a database. Then the result can be seen on the output of the browser in HTML, PDF, images or any other.

Tags - Tags in Strut 2 allow creating dynamic web application with less number of coding. Not only these tags contain output data but also provide style sheet driven markup that in turn helps in creating pages with less code. Here the tags also support validation and localization of coding that in turn offer more utilization. The less number of codes also makes it easy to read and maintain.

MVC – The Model View Controller in Strut 2 framework acts as a co-coordinator between application’s model and web view. Its Controller and View components can come together with other technology to develop the model. The framework has its library and markup tags to present the data dynamically.

Configuration – Provides a deployment descriptor to initialize resources and is in XML format. The initialization is taken place by scanning all the classes using Java packages or you can use an application configuration file to control the entire configuration. Its general-purpose defaults allow using struts directly Out of the box.

Configuration files are reloadable that allows changes without restarting a web container.

Other Features:

  • All framework classes are based on interfaces and core interfaces are independent from HTTP.
  • Check boxes do not require any kind of special application for false values.
  • Any class can be used as an action class and one can input properties by using any JavaBean directly to the action class.
  • Strut 2 actions are Spring friendly and so easy to Spring integration.
  • AZAX theme enables to make the application more dynamic.
  • Portal and servlet deployment are easy due to automatic portlet support without altering code.
  • The request handling in every action makes it easy to customize, when required.

Struts 2 History

Apache Struts is an open-source framework that is used for developing Java web application. Originally developed by the programmer and author Craig R. McClanahan this was later taken over by the Apache Software Foundation in 2002. Struts have provided an excellent framework for developing application easily by organizing JSP and Servlet basically based on HTML formats and Java code. Strut1 with all standard Java technologies and packages of Jakarta assists to create an extensible development environment. However, with the growing demand of web application, Strut 1 does not stand firm and needs to be changed with demand. This led to the creation of Strut2, which is more developer friendly with features like Ajax, rapid development and extensibility.

Strut is a well-organized framework based on MVC architecture. In Model-View-Architecture, Model stands for the business or database code, the View represents the page design code and the Controller for navigational code. All these together makes Struts an essential framework for building Java application. But with the development of new and lightweight MVC based frame works like Spring, Stripes and Tapestry, it becomes necessary to modify the Struts framework. So, the team of Apache Struts and another J2EE framework, WebWork of OpenSymphony joined hand together to develop an advanced frame works with all possible developing features that will make it developer and user friendly.

Strut2 is a combined features of Struts Ti and WebWork 2 projects that advocates higher level application by using the architecture of WebWork2 with features including a plugin framework, a new API, Ajax tags etc. So the Struts communities and the WebWork team brought together several special features in WebWork2 to make it more advance in the Open Source world. Later the name of WebWork2 has changed to Struts2. Hence, Apache Strut 2 is a dynamic, extensible framework for a complete application development from building, deploying and maintaining.

Struts 2 Architecture

Struts and webwork has joined together to develop the Struts 2 Framework. Struts 2 Framework is very extensible and elegant for the development of enterprise web application of any size. In this section we are going to explain you the architecture of Struts 2 Framework.

Request Lifecycle in Struts 2 applications

  1. User Sends request: User sends a request to the server for some resource.
  2. FilterDispatcher determines the appropriate action: The FilterDispatcher looks at the request and then determines the appropriate Action.
  3. Interceptors are applied: Interceptors configured for applying the common functionalities such as workflow, validation, file upload etc. are automatically applied to the request.
  4. Execution of Action: Then the action method is executed to perform the database related operations like storing or retrieving the data from database.
  5. Output rendering: Then the Result render the output.
  6. Return of Request: Then the request returns through the interceptors in the reverse order. The returning request allows us to perform the clean-up or additional processing.
Display the result to user: Finally the control is returned to the servlet container, which sends the output to the user browser.

Struts 2 Architecture

Struts 2 is very elegant and flexible front controller framework based on many standard technologies like Java Filters, Java Beans, ResourceBundles, XML etc.

For the Model, the framework can use any data access technologies like JDBC, EJB, Hibernate etc. and for the View, the framework can be integrated with JSP, JTL, JSF, Jakarta Velocity Engine, Templates, PDF, XSLT etc.

Exception Handling:

The Struts 2 Framework allows us to define exception handlers and inceptors.

  • Exception Handlers:
    Exception handlers allows us to define the exception handling procedure on global and local basis. Framework catches the exception and then displays the page of our choice with appropriate message and exception details.
  • Interceptors:
    The Interceptors are used to specify the "request-processing lifecycle" for an action. Interceptors are configured to apply the common functionalities like workflow, validation, etc.. to the request.

Struts 2 Architecture

The following diagram depicts the architecture of Struts 2 Framework. Following diagram shows the the initial request goes to the servlet container such as tomcat, which is then passed through standard filer chain.

Image: Struts 2 Architecture

The filter chain includes:

  • Action ContextCleanUp filter:
    The ActionContextCleanUp filter is optional and it is useful when integration has to be done with other technologies like SiteMash Plugin.
  • FilterDispatcher:
    Next the FilterDispatch is called, which in turn uses the ActionMapper to determine weather to invoke an Action. If the action is required to be invoked, the FilterDispatcher delegates the control to the ActionProxy.
  • ActionProxy:
    The ActionProxy takes the help from Configuration Files manager, which is initialized from the struts.xml. Then the ActionProxy creates an ActionInvocation, which implements the command pattern. The ActionInvocation process invokes the Interceptors (if configured) and then invokes the action. The the ActionInvocation looks for proper result. Then the result is executed, which involves the rendering of JSP or templates.

    Then the Interceptors are executed again in reverse order. Finally the response returns through the filters configured in web.xml file. If the ActionContextCleanUp filter is configured, the FilterDispatcher does not clean the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not present then the FilterDispatcher will cleanup all the ThreadLocals present.

In this section we have learned about the Architecture of Struts 2 Framework.

Download Struts 2.0

In this section we will download and install the Struts 2.0 on the latest version of Tomcat container. We will first download tomcat and configure it as our development server. Then we will download Struts 2.0 and install the struts-blank application on the tomcat server server to check the examples that comes with the struts-blank application.

Downloading Struts 2.0

Visit the Struts download site http://struts.apache.org/download.cgi and download the Struts 2.0 for this tutorial.

Download the Tomcat

Download the latest version of tomcat from http://tomcat.apache.org/download-55.cgi. We have downloaded apache-tomcat-5.5.20.zip for this tutorial Extract downloaded file and run the C:\apache-tomcat-5.5.20\bin\startup.bat to start the tomcat. Type http://localhost:8080/ in your browser, it should show the welcome page in the browser window Now we will install the struts-blank application on the tomcat container and test the application.

Extracting Struts 2.0 distribution and installing on tomcat

Extract the downloaded struts distribution struts-2.0.6-all.zip into your favorite directory. To install the struts blank application copy "struts2-blank-2.0.6" from "\struts-2.0.6-all\struts-2.0.6\apps" into the webapps directory of tomcat. Tomcat will automatically deploy the application



Feb 25, 2007 11:42:23 PM org.apache.coyote.http11.Http11BaseProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Feb 25, 2007 11:42:24 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Feb 25, 2007 11:42:24 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/141 config=null
Feb 25, 2007 11:42:24 PM org.apache.catalina.storeconfig.StoreLoader load
INFO: Find registry server-registry.xml at classpath resource
Feb 25, 2007 11:42:24 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6672 ms
Feb 25, 2007 11:52:55 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive struts2-blank-2.0.6.war

To test the struts-blank application type http://localhost:8080/struts2-blank-2.0.6 in the browser and the struts-blank application get displayed in your browser window.


Click the English link and familiarize yourself with Struts Blank sample application.

Struts 2.0 distribution also contains the following samples applications that you can try:

  • struts2-mailreader-2.0.6.war
  • struts2-portlet-2.0.6.war
  • struts2-showcase-2.0.6.war

You can simply copy these files to the webapps directory of your tomcat server. Tomcat will automatically deploy these application and then you can test these applications.

Sunday 29 April 2007

Sun Certified Java Programmer (SCJP) Free Mock exams

List of 40+ Free sites, Where core java mock exams are free online. These sites can help individuals in preparing for Sun Certified Java Programmer (SCJP) exam and the core java interview questions.

If you want to revise your java skills before taking these mock exams , then go to jchq essentials first. Please follow the link.....

http://www.jchq.net/essentials/index.htm#full

And here goes the site lists..... cheers guys!!!!


  1. Sun's sample questions:
  2. Java Ranch Rules Round Up Game: 36 questions. Basic questions, but important for certification.
  3. Jaworski: Java quiz with answers. Very useful
  4. Robert and Heller's Online Self Test: 10 questions.
  5. Absolute Java's Test Your Java Knowledge: 17 questions on variety of topics
  6. Java Prepare Questions: 59 questions.
  7. John Hunt Mock exam: 65 questions, Simple
  8. Java Caps Mock exam: 60 questions with answers
  9. Marcus Green mock exam - 1: 60 questions.
  10. Marcus Green mock exam - 2: 60 questions. Highly recommended
  11. Marcus Green mock exam - 3: 60 questions.
  12. MindQ exam: Good one.
  13. Majji exam: 30 questions, Very good.
  14. Amit Poddar's questions: 42 questions, related to Strings, Components and Layouts and Math class
  15. IBM online exam tool: 30 questions
  16. Barry Boone Exam: 70 questions.
  17. Bill Brogden exam: Random 18 from 38 questions.
  18. Bill Brogden's Hardest questions: 19 questions, If you want to waste your time
  19. Jxam: 59 from 200 questions.
  20. Sarga's Jargon Exam: 60 questions from 140 questions.
  21. JQuest Exam simulator: 30 from 90 questions.
  22. JDcert: 66 questions, Simple
  23. Deepak exam site: 250 questions.
  24. Applied Reasoning Exam: 77 questions, generated from a pool of 200 questions.
  25. Boris's Exam Simulator: 40 questions from 64 questions.
  26. Abhilash's Quiz: 80 questions with answers.
  27. Java Exam Simulation - Test 1: 30 questions.
  28. Java Exam Simulation - Test 2: 59 questions.
  29. Java Exam Simulation - Test 3: 59 questions.
  30. Java Exam Simulation - Test 4: 59 questions.
  31. Java Exam Simulation - Test 5: 128 questions.
  32. Exam Cram Pratice Exam 34 questions.
  33. 4tests.com's Java Exam: 40 questions.
  34. JvalTest Exam Applet: 60 questions.
  35. Mughal Khalid's Exam Simulator: 60 questions,
    Tough in comparison to the actual certification exam.
  36. JWhizTrial Exam
    These are sample questions from the actual software. Need to download them to your computer.
  37. Java Exam Simulator .
  38. Rahul Raje's Mock Exam Simulator : 60 questions. Needs downloading and extracting jar files
  39. http://www.go4java.20m.com/mock1.htm : 2 mock exams with 59 questions each.
  40. Web Tech Frontier : 56 Questions.
  41. Voodoo Exam by Ashish Hareet : At the time of writing this page, the mock exam has 60 questions related to SCJP.
Selected Java certification related websites: (Sorted in Alphabetical order)
About.com Java certification links
Abhinav Gupta's Guide to Certification
Amit Verma's Certification Page
Ashok Kumar's Java section
Bill Brogden's Java Resources
Chitra Sridhar's Java Site
Certify in Java Resources
Deepak's Certification Page
Dave's Java Site
Dasari's Java Certification Page
Javaranch
Java Prepare
Jyothi Krishnan's Java Page
Java Caps
Jxam Homepage
Java China Page
Java Certified Programmer Resources
Valiveru's Homepage
Java Skinny
Kaan
Levteck Java site
Maha Anna's Java Home page
Marcus Green Certification Headquarter
Michael Thomas Java Certification and Training Page
Mukherjee's Java Page
Moogly-googly
Sun Certification Site
Tony Alicea's Java Page
Usha Kasiram SCJP Resources

I recently found the following site having similar SCJP Mock exams links. Have a look if you find some useful or new stuff.
http://www.javacertificationexams.com/scjp-mock-exams.php

Secrets Of The Masters: Core Java Job Interview Questions.
JDJ Enterprise Editor Yakov Fain Offers 30 Core Java Questions You Might Expect During Job Interviews. Good 20 ones in the list anyways.