0 to struts in 60 minutes

Steps to build a struts application

Struts has become a standard for web applications. It lets you fit your web application in a MVC framework – which earlier I had wrongly thought – MVC is only for desktop GUI clients.

What is MVC?

M-Model
V-View
C-Controller

Model deals with the data – JavaBeans containing the data.
View deals with how you want to display the model
Controller – How the model and view are to be controlled

Lets take the example of a login process.

Here is the scenario : User enters login name, password hits submit. The form data are sent to a servlet that talks to the database to see if the information is valid. If it is valid, it puts the name, role information in a javabean and sends the browser to a welcome page. If not it sends the browser an error page.

Now with MVC scenario. When the user hits submit, the control does not directly go to the servlet but to a controller servlet. The form values are put in a javabean ( model ). The controller’s main job is to see what the request is and finds the correct servlet to work on it – in this case the login servlet. The login servlet then authenticates and sends either of these two words : success or failure. Also if the authentication succeeds it puts the name,role information into a javabean ( the model ). The controller if it sees the return value as “success” sends the browser the welcome page ( view ) . The welcome page takes the value from the javabean ( model ) and displays it. You might be thinking what do we call the login servlet – the model or the controller.You might also think it lies somewhere between the model and the controller – but actually its the model. It just tells the controller where the logic flow should be going next – but it cannot change the logic flow by itself. Only the almighty controller has the power to change the logic flow.

What are the advantages of MVC?

* The control logic is in one place. Otherwise the servlets, jsps have fun throwing the logic between one another – and when it comes to maintenance mode its a headache when one has to follow the trial. Now if we have all the logic flows in one place its enough to go to that one place and see how things flow.
In Struts this logic flow, controller, is held in an xml file – struts-config.xml. Now you can play with the logic flow – lets say for some reason you do not want to show the welcome page but want to directly take the users to another page. You need not touch the servlets ( means compiling, testing etc. ) – vs – you touch the struts-config.xml file – make the change you want to do – and restart the server. ( no recompiling, testing is necessary yes – but need not worry about the servlets throwing any exception etc.. as we never touched the servlets ).

* Lets say you want to toss out the current JSP view and have a new design. All you have to make sure is you display the javabeans in the right places. The Model and controller need not be touched at all. Isnt it beautiful !!

* Lets say you change the model – for instance your application goes the EJB way. Authentication is not done with a simple servlet but by a complex session bean. As long as the session bean creates the same javabean with name,role – the View(JSP page) need not be touched at all.

* Lets say tomorrow a new version of Struts comes – or another alternative to Struts comes along – it has a new controller – instead of struts-config.xml file they change a couple of things. No problem your model, view never get affected.

What you need?

Tomcat
MySQL
Eclipse
A web browser
Coffee/Tea

Where to start?

Go to the wonderful Apache site.
http://struts.apache.org/download.cgi

Download the binaries. Unzip it.

There will be a directory called webapps. There is a nice little template file called struts-blank.war. Rename it as antiPC.war ( anti ProCrastinator – thats the name of our struts app – you can name it whatever you want )

Start tomcat.
Go to tomcat manager ( http://localhost:8080 ) and deploy this war file. Tomcat will neatly unzip the war file and deploy the application too. Click on the link for antiPC

http://localhost:8080/antiPC

You should be able to see the welcome message. If not try to fix the problem. Look at catalina.out to see if there is any error messages, missing class library anything.

And then?

Time to start working on our struts application.
Fire up eclipse.
If you havent installed sysdeo eclipse plugin its a good idea to install it. Google for sysdeo plugin and load it in. Its useful.
Start a new Java Tomcat project (antiPC) and make it point to webapps/antiPC directory.

PS: If its not windows OS – eclipse might start throwing tantrums saying file not found. In your terminal editor navigate to the directory where webapps is. See the ownership of antiPC directory – it might be root. So do a
sudo chown -R youruserid antiPC

build.xml – the oxygen for your struts app !! It cannot live without it.

In eclipse navigate to antiPC/WEB-INF/src/build.xml

The build.xml cleans,compiles,generates javadoc and creates war file for deploying. We can remove the last 2 steps for now.

So make these changes

from :

<!-- Build entire project -->

<target name="project" depends="clean,prepare,compile,javadoc"/>

to:

<!-- Build entire project -->

<target name="project" depends="clean,prepare,compile"/>

and line 1 :

from :

<project name="blank" basedir="../" default="all">

to:

<project name="blank" basedir="../" default="project">

Click on the WEB-INF/src directory and click on Run as ant build. It should say something like this :

Buildfile: /usr/local/tomcat/webapps/antiPC/WEB-INF/src/build.xml
clean:
[delete] Deleting directory /usr/local/tomcat/webapps/antiPC/WEB-INF/classes
[mkdir] Created dir: /usr/local/tomcat/webapps/antiPC/WEB-INF/classes
prepare:
resources:
[copy] Copying 1 file to /usr/local/tomcat/webapps/antiPC/WEB-INF/classes
compile:
project:
BUILD SUCCESSFUL
Total time: 4 seconds

And thennnnn?

You are all set to play with your struts application. Next I will explain how to set up a login page, connect to database, etc.

Exposé

I am a multi tasker. I have a minimum of 5 applications running simultaneously. Exposé offers a beautiful solution to manage this mess. It has spoilt me !! Now I open even more apps and do not bother to close them. Ah the power of OS X in managing memory. These are the applications I was running when I took this screen shot.

Eclipse
Mail
Safari
ITunes
2 Text documents
Terminal
CocoaMySQL
2 Finder windows

Now when I have to switch between apps all I have to do is click my middle mouse button or press F9 or move my mouse to the left most top corner ( my choice ) – and everything zooms out neatly to this ( see screenshot below) – and I just click on the window I need. Fast simple easy.

OS X started small. Jaguar did not have much fancy stuff other than the eye candy gui. With a solid foundation built with Jaguar, OS X engineers started innovating on top of it. Exposé was introduced in Panther. Things are just getting started. On April 29th Tiger is being released. It will be a while before I lay my hands on it. Things are going to get pretty interesting in macworld. Longhorn is no slouch and I am eagerly awaiting for it to hit the market soon. I recently read that the new KDE is having some new GUI effects – linux/GNU developers have caught up with the mainstream OSes( Windows,Solaris,OS X) and now have started innovating. It seems it has a wobbly effect whenever windows are minimized,maximized. Gotto try it out sometime.

There seems to be a shareware app called Winexpose but its not GPU driven – where the graphical processing unit (GPU) takes care of doing such calculations and zooming out the windows. So the movement is jerky and buggy – if CPU load gets high it does not perform as expected. When I am building my java project machine crawls ( its a G3 800Mhz laptop – and ofcourse – its a mac 😉 ) but when I hit expose button it zooms out as if the machine is just sitting bored doing nothing.

Bottomline when it comes to exposé and because of it the productivity levels I reach :

Mac – 1, Rest – 0.

Heres a screenshot.

Picture 1

Macs can do more

Starting today, I am going to write about how on a mac you can do more things. Of course you can do the same or even more in Windows – but you have to bend your back. I will examine each of the applications, tricks I enjoy in my mac – also to be fair will try to find equivalent application in windows and linux and review them.

Mac sets you free…!!

UML

UML is like the Grand Unified Theory (GUT) for physicists – only difference is UML has been developed and is being used everywhere. I started this book a few days ago and I am hooked. I will be telling you some of the interesting things I discover in this journey.

As a briefer : UML defines nine types of diagrams

1. Class diagrams
2. Sequence diagrams
3. Collaboration diagrams
4. Object diagrams
5. Statechart diagrams
6. Activity diagrams
7. Use case diagrams
8. Component diagrams
9. Deployment diagrams

Incredibles


Incredibles poster
Originally uploaded by haikvr.

I remember 1992 when I saw Terminator 2 – I thought this is it – we have reached the end of graphics and imagination – nothing else can be made which can top this. 12 years later I want to laugh at that kvr who watched Terminator in awe.

Sangeeta and I went to the 12.00am show on Nov 5th to see Incredibles. There was a sparse crowd so we could choose our seats. The movie rolled and it looked like some ordinary cartoon movie. The movements were smooth, animation was amazing but still nothing ground breaking. You have to wait till the jungle scenes to realize Pixar’s prowess. The leaves and greenery were real. I dont know if they used real shots and mixed it with the virtual scenery – or they recreated it. I gotto wait for the making of Incredibles DVD to know how they did it. The greenery was real. The rocket, the buildings – I had to keep telling myself this is all CGI and its not real. Thats for the graphics and animation.

The story, humour, conversation, the contradictions in character’s minds, the thrill and suspense – this movie can compete with the last second thrill of Indiana Jones, or any James Bond Movie. An intelligent well made movie. I loved it and lapped it up.

So whats bad about this movie? A little bit long – not that I noticed – but I had a kind of itch when the story would drag on the pitiable boring lifestyle of our ex-superhero. My mind was waiting on and on for the action scenes to begin – and when it did I forgave my mini-grudge with all my heart. There were 2 scenes which stand out when I think of the movie. The chase of the kid – who can run sooo fast – and the instant when our kid discovers he can run on water – wow. It was like that instant when I realized am riding the bicycle without any support. The second when our elastigirl gets into a complicated situation. Difficult to describe this sticky situation – am just wondering how the screenplayer, story writer and director sat and discussed this scene.

Incredibles will be in the top 100 movies of my list.

Here is the life story of a speck

Close Bitnami banner
Bitnami