Fast Forward To MVC ‘ONE’

Recently, I worked on adding a feature to legacy webapp of my client, based upon MVC one model. And  it’s simplicity and getting work done, made me think again, about MVC one based webapps. I am always on hunt for simple web framework, which i can embark on for proof of concept of my startup ideas.

This is year 2010 and we as java developers, have spent countless time in arguing and reading (fun though) about various java web frameworks in play. Many bloggers publish their findings, opinions and countless others copied their content, republished them anyway. One common subject has been, which web framework? Cult is increasing everyday, for Spring MVC, Wicket, now JBoss Seam. I had personally spent time thinking about choosing one framework of choice, for my own projects, but I could never conclude on one. Spring MVC has become strong contender and safe choice, because I will be using spring for services and data access layer. Have we ever took step back and thought, we have surrounded ourselves plethora of frameworks for unlimited reasons, but never thought of ROI and time to market any product. Java always been treated as enterprise solution for big companies with big projects with big budgets. What about simple B2C startup projects which has limited user base and no funds. In today’s real web applications, PHP is thriving as before. People are still coding & adding more to PHP based projects. And those projects have been more successful and widely accepted. Example would be ‘WordPress’ , ‘Drupal’, ‘OSCommerce’. (Even used drupal. Not sure whether they still using it). So what is main charishma or any characteristics of PHP? We all have argued, bloggoed about How PHP is inferior compare to Java, but PHP is productive.

Having said that, lets come to drawing board, as new developer or even CIO of a company. What we need in a web application (lets assume startup webapp, upto 100 concurrent users in one cluster)

  1. Pretty interface, which users to love to use it. (Example
  2. Interactive & Intuitive interface.
  3. Functional interface. It does what it promises and always does that.
  4. Low or almost NIL learning curve for any new developer.
  5. Able to extend interface + functionality by 3rd party developers. (That strives any web product)
  6. Quick turn around of code to market
  7. Able to remove or disable code or by choice of end user. (user is deployer)

Having listed all the requirements, how many requirements really demand for use of MVC II framework?

First two are more of DHTML only.

Number 3 is quality of code and how well tested code is.

Number 4 is not good for MVC II frameworks like spring or wicket or even struts. They have some learning curve and you need resources, comfortable on that.

Number 5, people can argue, here MVC II excels. But at what cost? Lets analyze that more (I will use example of Spring MVC, because of popularity. Nothing against it)

First drawback is, to write any java code, you need to be java developer. It eliminates pool of casual web programmers who just like to write scripting (like PHP) and DHTML. Second is, if you write anything in JAVA, webapp has to be repackaged (and ofcourse released again). So there is no on-fly programming or releasing code on running webapp. (Lets forget about OSGi for now. It’s different ball game).

What about writing code using JSPs only?

That’s this post is all about. I know, many of you, thinking, c’mon man, Nineties called, they want their MVC one back. But lets entertain this idea for a while. For example, I want to write simple webapp ‘java press’ blogging engine, able to deploy it with in couple of weeks and my available heap size is 64MB (pretty common for VPS plans). If I go by spring mvc route, it will take me a couple of weeks, just enough to setup my environment, DBs, etc etc. I will have re-hash my knowledge of spring, and spring mvc controllers etc etc. But if I just pick JSPs model, i am ready to convert static HTMLs with-in a day.

Using JSPs and JSTL tags, I get lots of java functionality baked in. I can divide my jsps using includes (keeping option of apache tiles open). I can keep two sets of JSPs, presentation jsp and worker jsps. I can use extensively extra request parameters, to drive backend jsp, where to forward or redirect my request after finishing work.

If we still on this concept, few custom tags can be written and being delivered as part of SDK. So anyone who wants to customize it or extend it, he has JSPs, JSTL and some custom util tags at his disposal.

Advantages or trying to look at positive aspects.

  1. For deployments for small companies or concept deliveries or Y combinator kinda projects, time to deliver is small.
  2. You don’t have to muck around which framework to use. You think of doing something, and you doing it.
  3. Think of 3rd party developers pool, you get access to. If you document well, even PHP developer can pick JSP skills in few days and write JSPs for your webapp. (Imagine hiring PHP developer for spring MVC work. I don’t have time & budget for training).
  4. With new JEE specs coming out, you are always safe to upgrade your app and take advantages of new features without rewriting anything. (That’s what spring says, we give you another layer of abstraction. But you have upgrade code for spring 3.x now, isn’t it?)
  5. For scalability, it’s cheaper than before, clustering solutions based upon clouds. So my one deployment supports 50 concurrent sessions, I will just throw couple extra virtual tomcat instances, with in same cloud instance. or couple other different ways.
  6. I am less worried about performance, because recent benchmarks for tomcat 6 using JDK 6, is impressive.
  7. Using data source pooling, getting connections from JSP isn’t resource intensive. (As part of SDK, some classes with static methods can provided, to provide some encapsulation on common used API calls)
  8. web.xml supports basic security model. And again, as part of SDK, some common servlet filters can be provided for cross cutting concerns like security, logging, transactions.
  9. DB Caching can be ignored for now, assuming DB server and tomcat instance are co-located or installed in same instance. Caching adds lots of code maintenance, research & testing. Benefits of using caching diminishes in small db and small users.
  10. Well known advantage of JSP, do code change and see changes immediately on web browser. Hence time to see results, while doing development is almost nil. We all have taken coffee breaks in our past projects while waiting for server to bounce.

This post is not comparison between MVC I and MVC II model. It’s more of another look at MVC I model and putting on my thoughts, why It could make sense to use it.

Spring scope using Example

This post is about to experiment about spring framework ‘singleton’ and ‘prototype’ scope. I created couple of tests to show proof of concept and clear our understanding of ‘singleton’ scope.

Those are new or fairly new to spring framework, they sometimes get confused, when they read the term ‘singleton’ in spring beans. As traditionally, we know, Singleton class pattern makes sure, we have single instance of that class per jvm. But when we talk about singleton pattern in spring framework world or lets say spring context world,  it’s singleton pattern applied per spring container. Now we can have multiple containers initiated in a java virtual machine, hence we can have multiple instance of same class possible, but single instance per container per bean definition.

Example, Lets use simple java bean, which stores it’s own creation time and also defines equals method to check that equality.

public class SpringBean {
private long createTime;

public SpringBean()
this.createTime = System.currentTimeMillis();


public boolean equals(final Object obj) {
if (this == obj) {
return true;
if (obj == null) {
return false;
if (this.getClass() != obj.getClass()) {
return false;
SpringBean other = (SpringBean) obj;
if (this.createTime != other.createTime) {
return false;
return true;

My spring context file defined only two beans


<bean id=”singletonBean”/>

<bean id=”prototypeBean” scope=”prototype”/>


  • <bean id=”singletonBean” class=”com.jframeworks.learn.SpringBean”/>
  • <bean id=”prototypeBean” class=”com.jframeworks.learn.SpringBean” scope=”prototype”/>

Both beans use same class but different scope. Lets define a testsuite class which will initiate these beans and run some basic tests.


Class: SpringScopeTestSuite, method = runTests

public SpringScopeTestSuite(ApplicationContext ctx, ApplicationContext ctx2) {
this.ctx = ctx;
this.ctx2 = ctx2;
public void runTests(String name) throws InterruptedException
SpringBean singletonBean0 = this.getBeanUsingContext("singletonBean");
Thread.sleep(50);//To make sure, next bean from context is at different timestamp.
SpringBean singletonBean1 = (SpringBean) this.getCtx().getBean("singletonBean");
SpringBean protoTypeBean0 = (SpringBean) this.getCtx().getBean("prototypeBean");
SpringBean protoTypeBean1 = (SpringBean) this.getCtx().getBean("prototypeBean");
SpringBean singletonBean2 = (SpringBean) this.getCtx2().getBean("singletonBean");
SpringBean protoTypeBean2 = (SpringBean) this.getCtx2().getBean("prototypeBean");

System.out.println(name + " =====================================================");
System.out.println(name + " Equality Tests");
System.out.println(name + " =====================================================");
System.out.println(name + " Two beans references with Scope 'singleton'");
System.out.println(name + " singletonBean0.equals(singletonBean1) = " + singletonBean0.equals(singletonBean1));
System.out.println(name + " singletonBean1.equals(singletonBean2) = " + singletonBean1.equals(singletonBean2));
System.out.println(name + " singletonBean1.equals(protoTypeBean1) = " + singletonBean1.equals(protoTypeBean1));
System.out.println(name + " protoTypeBean0.equals(protoTypeBean1) = " + protoTypeBean0.equals(protoTypeBean1));
System.out.println(name + " protoTypeBean1.equals(protoTypeBean2) = " + protoTypeBean1.equals(protoTypeBean2));
System.out.println(name + " =====================================================");

To run this test, lets define test runner class, ‘BasicSpringScopeTest’

public class BasicSpringScopeTest
public static void main( final String[] args )
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationConfig.xml");
ApplicationContext ctx2 = new ClassPathXmlApplicationContext("applicationConfig.xml");
SpringScopeTestSuite test = new SpringScopeTestSuite(ctx, ctx2);
} catch (Exception ex) {

Now executing this class, we see this output…

singleThread =====================================================
singleThread Equality Tests
singleThread =====================================================
singleThread Two beans references with Scope 'singleton'
singleThread singletonBean0.equals(singletonBean1) = true
singleThread singletonBean1.equals(singletonBean2) = false
singleThread singletonBean1.equals(protoTypeBean1) = false
singleThread protoTypeBean0.equals(protoTypeBean1) = false
singleThread protoTypeBean1.equals(protoTypeBean2) = false
singleThread =====================================================

As singletonBean0 and singletonBean1 comes from same container, and their scope is default ‘singleton’, hence they are same instance.

singletonBean1 and singletonBean2, though are same bean and ‘singleton’ scope, but they comes from different container. Hence they are different instance of same class.

singletonBean1 and prototypeBean1 comes from same container and same class, but prototypeBean1 is different bean definition and ‘prototype’ scope. Every time, this bean retrieved from container, spring container creates new instance. Hence they are not equals. They are not equal for two reasons


  1. They are two different bean definition (even same class type)
  2. prototypeBean is defined as ‘prototype’ scope’


Next equality check is for prototypeBean0 and prototypeBean1 beans.  Even they come from same container but they still not equal, because they refer to spring bean defined as ‘prototype’ scope.

Last equality check is for prototypeBean1  and prototypeBean2 beans. They are not equal for two reasons..

  1. They both comes from different spring container
  2. They refer to spring bean defined as ‘prototype’ scope.

Spring framework takes away boiler plate code of defining Singleton classes in code base. This aligns with their philosphy of letting developers concentrate on business logic instead of writing boiler plate code.

Just to be clear, in spring world, a bean definition is singleton not bean class. We can have multiple of bean definitions of same class, each representing different singleton class. This makes spring container light weight container. Prototype scope is not used widely, but it can specified depending upon technical requirements.

For further testing, there is another code, which runs test suite in mutli-threaded environment.

public class AdvancedSpringScopeTest
public static void main( final String[] args )
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationConfig.xml");
ApplicationContext ctx2 = new ClassPathXmlApplicationContext("applicationConfig.xml");

SpringScopeTestSuite testSuite = new SpringScopeTestSuite(ctx, ctx2);



Thread thread1 = new Thread(testSuite, "thread1");
Thread thread2 = new Thread(testSuite, "thread2");
Thread thread3 = new Thread(testSuite, "thread3");



catch (Exception ex)

This class output is..

thread2 =====================================================
thread2 Equality Tests
thread2 =====================================================
thread2 Two beans references with Scope 'singleton'
thread1 =====================================================
thread1 Equality Tests
thread1 =====================================================
thread1 Two beans references with Scope 'singleton'
thread1 singletonBean0.equals(singletonBean1) = true
thread3 =====================================================
thread1 singletonBean1.equals(singletonBean2) = false
thread2 singletonBean0.equals(singletonBean1) = true
thread2 singletonBean1.equals(singletonBean2) = false
thread2 singletonBean1.equals(protoTypeBean1) = false
thread1 singletonBean1.equals(protoTypeBean1) = false
thread1 protoTypeBean0.equals(protoTypeBean1) = false
thread3 Equality Tests
thread3 =====================================================
thread3 Two beans references with Scope 'singleton'
thread3 singletonBean0.equals(singletonBean1) = true
thread3 singletonBean1.equals(singletonBean2) = false
thread3 singletonBean1.equals(protoTypeBean1) = false
thread1 protoTypeBean1.equals(protoTypeBean2) = false
thread1 =====================================================
thread2 protoTypeBean0.equals(protoTypeBean1) = false
thread3 protoTypeBean0.equals(protoTypeBean1) = false
thread2 protoTypeBean1.equals(protoTypeBean2) = false
thread2 =====================================================
thread3 protoTypeBean1.equals(protoTypeBean2) = false
thread3 =====================================================


//make sure, all threads finish.

To Interface or NOT for Value Beans in Java

What is a Java Interface


There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a “contract” that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group’s code is written. Generally speaking, interfaces are such contracts.

In this blog,  I am trying to present very common design pattern in projects, which I kind of don’t agree with.

This pattern is to define Interface for all data beans and value beans.  In most of the projects, I have worked in past (and current one too), I have observed, Architects have defined interface for any kind of bean flowing between layers of software.

For example  ‘Person’ and associated ‘Address’ bean.  So, there will be a Person interface and an Address interface. And there will be ‘PersonImpl’ and ‘AddressImpl’ object implementing respective interfaces.  Now Person interface has common getters and setters for properties like name, age, height and Address interface has getters & setters for addressLine1, addressLine2, city, zipcode, country etc.  So we can imagine implementing classes have defined class variables for corresponding each getter and setter. Now big question comes to mind, Why we have defined interface for this object? Why not to keep single class Person having getter/setter and it’s class variables.

Common Reasons (and why I think, they are wrong)

  1. It can have multiple, different implementations. As I have heard from architects, they just want to expose interface to integrating layers/client. And if later on, we change implementation, it won’t visible or require any code change on their side. Well, why we will have different implementations, in first place? These beans have getter and setter of certain property. How come you can have different implementation?
  2. We want integrating client to write their own implementation. Again, why? It’s simple data bean carrying data around. I think, this is another example of ‘abuse’ of interfaces by architects who are following more of text books version, instead of thinking out of box. Interfaces are contracts of expected behavior of operations, not data beans. Data beans doesn’t have behavior, they are mere payload objects. If they are more than payload, then I would put your design in question.
  3. To support legacy objects. Argument is, their legacy code of databeans have behavior. Now to bridge to new version of software, they need to use interfaces for databeans. Hence they provide bridged data beans implementing new data beans. And later on, when they move away from legacy, we will just use new implementation of interface. This is clear sign of ‘design’ smell. If you want to bridge to legacy code, then use bridge services objects. Bridged services will take care of handling legacy operations on legacy bean. Your new beans shouldn’t have any kind of behavior, encapsulating or hiding legacy smell. That was main reason to start new project, right?
  4. So that, we can have multiple inheritance. If your data bean’s getters/setters are not suppose to have different kind of implementation, hence why different objects will implement same implementation. Those different objects rather extend common super implementing class.
  5. (write in comments, I will add here)

Problems I have faced and I hate it

  • Whenever I need to add/remove any property, first you will have to fix interface. Hence doubling my effort in refactoring.
  • If same interface is implemented somewhere else, then changing interface (in prev step), I will have to change all other classes too. Question comes, why other implementing class, just simple extends original class.  They all are data beans,  no behavior.

Only behavior, data beans/value beans (must) contain, implementing equals() and hashCode() methods.  Well, we get those as part of Object class. Another possible ‘operations’ can from other interfaces like ‘Comparable’ or ‘Serializable’. If you get gist of it, none of  interfaces defined operations as accessors for properties.

In end, I am trying to advocate, to keep java programming simple and productive. Yes, we can boost of designing complex mutli-layered system, but at the cost of productivity and maintainability. Lets keep any kind of operability or logic in business classes.  Any logic or code in data beans, is destined for future refactoring some time.

Why I decided to host website using Amazon EC2


Cloud computing is something, we been listening and reading about from past couple of years.  From my perspective, it is virtual machines running on virtualized hardware. Hence giving us dual benefit of running any image on any kind of hardware. We can dynamically launch any image and also dynamically expand/reduce available hardware capacity. Hence the word ‘Elastic’ & ‘Cloud’. In cloud computing world, amazon is more established compare to others.  So rest of article will use Amazon EC2. Amazon elastic computing allows user to launch any image (from their growing library of public images). They are called ‘AMI’s. You pay per use of images. (hourly usage rate plans). Hence it makes it easy to choose any image w/o binding to any contract.

Why cloud computing is better than tradition web hosting?

Answer is, it is not. If you looking to run simple apache web server and few php based applications then web hosting companies give best pricing and pre-configured applications.  Decision comes, if you looking for better hardware and more RAM for your applications.

Pros, Comparing amazon EC2 smallest image & virtual private server (VPS) plans.

  1. Amazon EC2 guarantees one CPU core whereas 4-5 VPS images run per CPU core.
  2. RAM available is 1.7GB for EC2 instance and VPS gives maximum of 512MB to 756MB (for plans >$40/mo) (for simultaneous multiple J2EE web applications, you wish for more RAM.)
  3. Available capacity is elastic. You can expand it as required, compare to fixed capacity available in VPS/Dedicated plans.
  4. Once you finished configuring your images, you can make current installation as another image incase you want multiple instances running.
  5. Amazon EC2 instance are running with better reliable infrastructure comparing.
  6. If you pre-pay for EC2 account, your average cost of running website 24×7, for whole month comes to $42 something, which is comparable to good VPS plans with lots of memory.

Hence comparing to dedicated hosting, amazon ec2 seems to be better option, if we start comparing relability factor, ability to instantly increase computing capacity, price and many other options.

I decided to give it try to move my website.

Other factors to consider

  • 1.7 GB of available RAM and 160 GB of drive capacity, gives me chance to run java apps w/o worrying about available memory.
  • It was learning experience for me, (as average user of linux OS).
  • It’s easy to install applications on ubuntu image. (same goes for other OS like fedora/centOS).
  • I can run MySQL & PostGres DB server together, as required.
  • Java tomcat web server seems to be good enough, but I can try some other servers like glassfish or jboss for better experience.
  • Using it as subversion server. Ping time & bandwidth is better than other servers.
  • Will be using as build server & agent both. (Most probably open source project ‘Hudson’).

Java Products BUT PHP based website ?

I am starting to run a website which features java and j2ee products. That site would be more of ecosystem containing forums, blogs, demos for webapps etc. I am java developer, good in web development using j2ee technologies in JSP, JScripts, HTML. Ofcourse if I want to build a website, first thing I think is JSP and which container. (Container is pretty much standard, Tomcat for webapps). Hence quest for nice CMS application or portal application begins. So instead of researching and spending time what container and framework would be fine, I decide to look at major java sites infrastructure. I was pretty sure, for starters like me, it’s easier to replicate their website technologies than build myself. So here what I find.

  • – Running in drupal (PHP based content management site)
  • I recently read blog about “Java Parallel Processing Framework”. When I visited their site, again it was PHP.
  • And many many others.

It has definitely ticked me off, what’s so wrong with j2ee technologies, to run a even simple website. Everyone knows that JSP is far superior technology than PHP. So why j2ee evangelist, who live, eat, blog, earn using jsp, BUT not build their product site using JSP? For simple static site, just HTML is good. No doubt about it. But we try to wrap it up with some dynamic UI page, just in case. Ok so lets wrap that HTML page in JSP. but why PHP? Number of reasons, I think, might be?

  1. There is no simple content management system (CMS) in JSP world
  2. There are not enough JSP/Tomcat hosting available.
  3. We are using 3rd party hosting team, which prefers to use PHP
  4. There are many tools available in PHP, hence running whole website makes more sense.

Lets try to dissect each one by one.

There is no simple content management system in JSP world Yes there are.

  • OpenEdit
  • dotCMS
  • LifeRay
  • Clearspace

Most of them are available as FREE and almost all are free to open source projects. This also brings us to interesting point, do we really need CMS application, for my website? In most of cases, I don’t think. If you are only person to change content and you change content lets say every week, then it is lighter to run simple JSP pages. How many times, you update documentation of product? (Please be not confused with need to source control. I personally prefer everything should be in source control)

There are not enough JSP/Tomcat hosting available.

Over the period of time, with reduce cost of hardware processing and RAM, many good hosting companies are providing tomcat and even jboss hosting. I don’t this as show stopper now, if you want to host site running on tomcat. Here is list of companies, I researched 2 years back. Of course over the period, ,more companies has added.. and their plan prices has dropped.


We are using 3rd party hosting team, which prefers to use PHP

Well, you spending time coding on Tomcat or any other server, how big is deal for you to code JSP or convert html to JSP and throw it in Tomcat. If you are open source project, then you got volunteers. If you are commercial product provider, it makes more sense to use j2ee platform. To me, it puts bad impression if you using PHP and selling j2ee based CMS product. 🙂 To me, springframework is getting more interesting, but it’s running on Drupal? Their MVC or webflow APIs are not mature enough for this simple site?

There are many tools available in PHP, hence running whole website makes more sense.
This is one of most important factor, which deciding between JSP and PHP based hosting. I did some research, I found tools which are not great as PHP counterpart, but they solve the purpose. For Forums


For wikis

  • JSPWiki
  • SnipSnap

For Blogs

  • Apache Roller
  • Peeble

Issue Tracking

  • JIRA
  • TrackPlus (I just read their announcement on

Continous Integration Tools

  • Cruise Control
  • Continuum
  • Luntbuild
  • Hudson


Most common wish is, ‘Single Sign On (SSO)’ for all tools. If it is public website, then I want users to log on once for forums and able to comment to blogs using same profile. Right now, every product is using some kind of security, like j2ee form based security or Spring security or home grown security code. They also document, how to do SSO capabilities, but at this point, it seems more of hassle. Anyways, in case of PHP, I don’t think, people think about luxuries like SSO. Above wish, mentioned ‘user profile’. I am not able to find mature product, whose whole purpose is to manage user profile. Thinking aloud, brings us to point, user profile is essentailly feature of CMS application. So if I need user profile kinda feature on my public site, I can use CMS application. But can we use same user profile application to manage profiles to all other modules like forums or blog or customer service?

Should we always use J2EE for hosting if we are in business of J2EE/Java?
Now, I try to look at whole website hosting task at 35,000 feet. Basically, you want to spend minimal resources to get maximum UI experience. So if using vBulletin or phpBB makes my life easier then why I should stick to idealism of using j2ee? I can try to answer this by using these criteria

Q: I am selling/open sourcing j2ee product which is related to web-app development?
A: You should use JSP based hosting. It definitely puts good impression. Even better use your product in that webapp if possible.

Q: I am open source evangelist for java and j2ee but I still want PHP site?
A: Again, you should use JSP. If you can code in PHP, then you can code same functionality in JSP (assuming you are JSP developer)

Q: I need to use a tool which is perfect in PHP or Python (example Trac or vBulletin)
A: In this situation, using tool like Trac or anything else, shouldn’t matter. Basically, productivity matters. I would suggest to use hybrid solution. (Ofcourse SSO is nearly impossible)



I just read few more user comments. Almost every other user is recommending Drupal for CMS/Portal website. I wish, if something like that comes up in JSP world. If that is made on Spring, it’s even better.