Spring MVC and Spring Webflow basic and simple explanation

Spring MVC basic and simple explanation

1. As a pre-requisite, org.springframework libraires are to be downloaded and added in the project.

2. All mappings are declared in the Controller Class, not in any xml files.

3. @RequestMapping annotation maps the specific URI as accessed by the the browser.

Ex. Map the URI “/welcome” and when the complete URI http://localhost:8080/spring2/welcome
is accessed by the browser, the Controller Class with “welcome” @RequestMapping is accessed.

4. A specific method will return the ModelAndView Class, where the jsp file name is
the String constructor initialization of the ModelAndView class.

Ex. ModelAndView model = new ModelAndView(“HelloWorldPage”);
where HelloWorldPage is the name of the jsp file

5. All other general configurations are set in spring-mvc-config.xml and web.xml files.

See a more comprehensive tutorial here:

http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-annotation-example/

 

Spring Web MVC and Spring Webflow combined.

Spring Web MVC is the url->to-> controller mapping, while Spring Webflow is the transition controller of a page to another page using a flow configuration.

If both frameworks are combined, the Spring Web MVC will return the initial page, then in this initial page, an option to transfer to another page is processed by the Spring Webflow using xml configuration, then the Spring libraries takes care of the rest….

here’s a neat post where Spring Web MVC and Spring Webflow is combined, for comparison.

https://tamasrathonyi.wordpress.com/2010/09/22/spring-web-mvc-with-spring-webflow/

Struts + Spring + Hibernate Integration

Posted on 2014/07/20, Updated on 2015/08/30

Update: For new software development or building a new website from ground-up, there is better design alternative instead of using the Struts framework. Using JSTL is a better alternative, and here is a post in Webpage development using JSTL, Javascript, and JQuery. For older webpage systems, created before 2014, the Java and Struts combination is popular, and therefore a lot of legacy systems runs using these technologies.

Let’s Remember these important files that are needed in configuration of a Java Web application.
( Which includes Spring, Hibernate, and Struts )

web.xml – Servlet class to URL mapping
applicationContext.xml – Spring mapping of Bean/Object injection
struts.xml – maps the defined action to the servlet ( action class is needed in struts ) to the result jsp
hibernate.cfg.xml– database connection such as JDBC name , user/password to access the database name is defined here
<classname>.hbm.xml – the database table hibernate mapping file

index.jsp – the initial action URL, the startpage

Steps:

1.) Define org.springframework.web.context.ContextLoaderListener as the listener and org.apache.struts2.dispatcher.FilterDispatcher as the filter in web.xml

2.) Basically Spring is used here to create the predefined values, as these can be done by using the bean property injection in applicationContext.xml

Here’s a detailed explanation at this site: http://www.tutorialspoint.com/struts_2/struts_spring.htm

Website Login Security : Comparison of Java and PHP

Login and authentication is the basic and important function in almost all websites. I will just give a summary of my research and how login and authentication is done in JAVA and PHP.

JAVA

By using Spring Securities and Spring MVC jars ( by using Maven or downloading the jar files from the internet ), Website login can be easily created by following tutorial instructions. The basic tutorial with authentication can be created in just one day ( according to my experience ). SQL authentication is not yet included from the basic, but can be easily added. Knowledge of Spring MVC and Servlet Filters are also needed to understand the concept.

Spring Security tutorials by Mkyong
http://www.mkyong.com/tutorials/spring-security-tutorials/

The basic Spring Security example
http://www.mkyong.com/spring-security/spring-security-hello-world-example/

PHP

I also researched on how to this in PHP, and saw results with core PHP and another using Laravel. PHP is known to have very unstable and messy code ( which is why maintainability is an issue ). But with Laravel Framework, PHP code is improved.

PHP hardcode using MYSQL database for secure login
http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL

PHP authentication using Laravel
http://laravel.com/docs/5.0/authentication

Laravel features discussed including login and authentication
http://code.tutsplus.com/tutorials/why-laravel-is-taking-the-php-community-by-storm–pre-

PHP and JAVA codes are very different in syntax and implementation, and both languages has its advantage and disadvantage.

Play framework – The next web server phenomenon

As I have learned, there are more advancements in Web technologies which makes software development much easier, less complicated, and its end product to be highly efficient, stable, robust, fast, reliable and yet takes complex requirements. And top of it all, it should never take the user experience for granted, and keep in mind the developer experience as well.

Play framework is an emerging technology. It is a web server that can be created in Java or Scala. ( Scala is not as popular as Java, but it was created to address Java shortcomings ) It is foreseen to replace the J2EE web server technologies. It is comparable to MongoDB ( or any NoSQL ) , which has very high potential of replacing the SQL.

To code using Play framework, the concept is in MVC but without servlets or any web server, since Play is already the web server. Other MVC frameworks will not be used also ( ex. Spring ). Front end code is still needed such as Javascript ( or CoffeScript, JQuery, AngularJS ) and CSS. Server side logic can be coded using Java or Scala.

Comparison with J2EE: J2EE web technologies have been the standard and reliable web server on most enterprise applications, with Java as the top programming language of choice. J2EE technology is built on top or constructed with other modules to make it functional. A full stack J2EE web application should have a J2EE container / web server, the J2EE application package itself, which is integrated with a MVC framework such as Spring and a Database access component such as Hibernate. Currenty J2EE application have become bloated with layers of modules and log files have too much hierarchy. It is because J2EE has become an increasing version of add-ons and enhancements.

Play framework eliminates all of those J2EE modules. It is not a Servlet technology. Play framework is a Non-Blocking I/O. Non-Blocking I/O means that HTTP request and processing in CPU side of the server is asynchronous, Yet Servlet 3.1 already supports Non-Blocking I/O, Play framework has Websocket support, which is not in Servlet 3.1. And best part of Play framework is the simplicity of the debug logs and application modules, where J2EE has its drawbacks.

I’m really impressed with this new Web Server advancements in Play framework. It is really comparable to the shift from SQL to NoSQL. SQL and NoSQL databases have the same purpose, a Database, yet its design and architecture is completely different. For simplicity, yet have served its function as a Database, NoSQL ( ex. MongoDB ) does it. It is much easier to setup ( ex. in Java, we just need the libraries and codes for transaction ), compared to SQL ( ex. Hiberante ) , which needs code, models, configuration files, and even integration with Spring to make its code less. So in this case, Play framework is like the NoSQL solution to an already legacy code such as J2EE.

Some Cons: Yes Play is better, but it is still new. So there may be some uncertainties hiding in the code. Another drawback of Play is the Scala and its build code ( called SBT , which is similar to Ant / Maven ). Play is built in combination with Java and Scala, So for non-Scala developers, it’s a steep learning curve.

Production components: For production and deployment, Play is integrated with Netty ( HTTP server ) and Akka ( Background Process ). No need of Webservers ( Tomcat, JBoss, IBM Websphere and etc.. ) . Other sub components are Templates, SBT and Websockets ( Websockets and be used as an option only ). Just include front end JS and CSS to be added in Play. Other third party libraries in Java ( if using Play as Java based, not Scala based ) can be added.

Bonus Factors: This technology is really new, with only a few enterprise websites using it, and as of this writing, they only have 5 on their list. See the list here in their home page. LinkedIn is one popular site using Play, so there is no question of its performance. I also checked zaptravel.com , and it loads pretty fast too. Compared to MongoDB which already has numerous users, across all industries, so similarly, in a matter of years, Play framework will get more attention.

Highlights:

– Lightweight
– Has authentication and security
– Can easily be switched from development mode to production mode via a single configuration.
– Has support for Design Patterns ( ex. Singleton and etc .. )
– Not a Servlet
– Not tied up to SQL . Can handle other Database types also
– No need of Web Server ( Or a servlet container ). The Play framework is the server itself.
– Can be scaled for production
– pretty darn straight forward, not so layered.
– Supports Unit test
– Better error detection / Direct to the point compile errors, does not display a full stack trace
– built as non blocking I/O. Adapts to multi core CPU.
– Much better compared to node.js and ruby on rails. Which I still have to find out the reason…

Here’s the explanation why Play framework is really fast.
https://playingscala.wordpress.com/2015/05/01/why-play-framework-is-fast/

Some important links:

1.) routing of http requests:

http://stackoverflow.com/questions/15907996/how-to-get-query-string-parameters-in-java-play-framework?rq=1

2.) basics

https://www.playframework.com/documentation/1.2.x/faq

3. ) philosophy

https://www.playframework.com/documentation/2.3.x/Philosophy

4.) One user even got embarrassed.. or how it is unbelievalbe good.

https://dzone.com/articles/play-framework-now-officially

Webpage development in Java using Spring MVC, JSTL, Javascript and JQuery

How this website is structured in development:

1.) VIEW format – This is the front page view as shown in the browser. components: HTML and CSS for page layout, Spring MVC tags and JSTL for JSP for Java integration to Controller side.

2.) VIEW logic – Javascript and Jquery is used to create button/checkbox logic and is integrated to the HTML

3.) CONTROLLER – Built using Java classes using J2EE/HTTP libraries with Servlet Request and Response parameters. Spring annotations are also included to bind the Controller and the Models

4.) MODEL – Java classes that are POJOs ( Plain-Old-Java-Objects ) which has fields, setters and getters. Models are data storages on the transaction level. These are integrated to the JSP page and can also be used to be integrated to the Database frameworks such as Hibernate and Spring with Hibernate.

Before JSTL, the Java, Spring, Hibernate and Struts design was popular. Struts was used to bind the UI components to the Java and Spring server side code. Here is my post on Struts, Spring, and Hibernate configuration.

Servlet cookie and multithread

Servlet cookies are given to clients that will access the servlet for the first time, so in case when the client will access again, authentication will be quick. Take note that cookies will also expire.

Servlet handles all request as a multi-thread. This means that all requests from client to a particular servlet will be handled concurrently by a single single servlet, running on different threads. For more on servlet multi-thread see my other post below:

https://cmsoftwaretech.wordpress.com/2014/10/27/servlet-is-multithread-by-default/

Distributable Web Application

In large scale and multi-server setup, the web application should be Distributable. This means that any client can access any server of the cluster, provided that the application is in a distributable state. Advantages of a distributable web application is that each session is recognized in each member of the cluster, and the client will not re-login if it is redirected to another server.

More detailed explanation of a distributable web application using tomcat is in the link below.

http://wiki.metawerx.net/wiki/Web.xml.Distributable

Also a side note on Servlet-Context is provided in the link below

http://java.boot.by/wcd-guide/ch03s04.html