Pages

Post Redirect Get pattern in Web application and Spring.

In Web application, we are aware that there are two ways of submitting data from browser to server through a form. Post and Get.
Now, what the Post Redirect and Get pattern advocates is that, it is not a good idea to display a page in a web application on POST requests. One most common problem is re-submit. Imagine a very delicate application, where one post method causes a lot of vital state changes in the server which might worth millions of dollars. Not just because of some load issue the client might refresh the browser and run into trouble. We as a web application developers could not rely on browsers alert warnings of re-submission.

Solution.

The post method should do the state change, put some parameters in the session, and do a get:redirect to another function or page which should display the attributes. Ideally, after being displayed, the session attributes should be erased from the session as the session is consuming memory and other resources which might grow.

Spring provides a very efficient solution to the problem with Flash attributes. What you do is handle the post request to one method, put the attributes in the flash attributes, and redirect the page to the get request in another method(possibly with same url), which will redirect it to a jsp file which displays the attributes.

    @RequestMapping(value="/submit", method=RequestMethod.POST )
    public String submit(Calculator calculator, Model model, 
     RedirectAttributes redirectAttributes) {
 
 List<String> errors = calculatorValidator.validate(calculator);
 
 redirectAttributes.addFlashAttribute("form", calculator);
 redirectAttributes.addFlashAttribute("errors", errors);
 
 return "redirect:submit";
    }
 
    @RequestMapping(value="/submit", method=RequestMethod.GET)
    public String get(){
 return "CalculatorForm";
    }


Java multiThread application | join method

Join method is used in java to wait until a child method is finished executing.

Scenario

Suppose you have to read multiple files from the os and put it into different list. You could create a runnable class that could read a file and store it in its own list. Now, the main could create multiple threads from this class in which case, the main class has to wait until all the file reading threads are finished running, so that it could run its own logic or algorithm. The object method join comes in handy in this scenario. Join method waits the current thread until the called method is finished executing.

Apache Spark and Big data

Apache spark is one of the latest projects from apache that makes it really easy to write Map reduce tasks both in single cluster and multiple cluster mode. I have not tried multiple cluster, but as per the documentation, all it required is some configurations.

Installation

All you need is JDK installed in your machine.

Apache Spark does not need any installation on single node local mode. It is available in many languages like java, python, scala . I am using Java for my case.
Create a Maven Java Project and add the following dependencies in your pom.xml file.
<dependency>
 <groupId>org.apache.spark</groupId>
 <artifactId>spark-core_2.10</artifactId>
 <version>1.3.1</version>
</dependency>

If you are creating a non-maven project, just download the spark-core_<version>.jar file and add it to the projects build path. Then search in the internet and try one of the word count examples. You can run the class directly from eclipse, which case it will expect the file in your local filesystem.

You can also download the compiled version of spark and extract it in your directory and then use that to run your project. In this case, you need to package the classes in your project, and hit the command.

spark-1.3.1-bin-hadoop2.6/bin/spark-submit --class com.spark.WordCount --master local[2] ./target/ApacheSparkExample-0.0.1-SNAPSHOT.jar ipsuminput/*.txt ipsumoutput


The spark will expect the input and output locations to be in the HDFS in this case.

Map and Fold | Basics

Map and fold is a different approach of solving a problem then we normally do in our daily computations.

Basic steps

  • The problem basically is a list of some values(eg a number array), and we have to do some operation on it to get the output.
  • There is a mapper f(x) which takes one value at a time, and creates another value as its output. The created value may be same or different depending upon the required result.
  • The Fold(g(x,y), I) function takes a function g(x,y) and an initial value I.
  • Given a problem in Map and fold, as a solution, we have to find the 
    • function f(x), 
    • function g(x,y), 
    • initial value I, 
    • And the interpretation of the final result.