Sep 18 2009

Difference between request.getAttribute and request.getParameter

Okay, so going by the name of this blog, one post regarding request.getAttribute was due, and here it is. It’s a newbie question which I will answer in this post.

Difference between request.getAttribute() and request.getParameter()

First of all, what is a parameter? If you are posting a form to the server (using get or post), then the name/value pairs are passed as parameters. e.g.: Your form looks like this:


<form method="post" action="/blah/"  name="loginForm">

Email ID:
<input name="email" id="email"type="text"/> <br/>
Password:
<input id="password" name="password" type="password" />
<input type="submit" value="LOG IN"/>
</form>

When this form is posted to the server, to fetch the values of “email” and “password” in your serlvet, you would need to use:

String email=request.getParameter("email");
String password=request.getParameter("password");

Note that the request.getParameter() method returns you a String. Simple enough!

Coming to request.getAttribute(), first let us look at what a request attributes. A request object can store objects in its attribute, which can be set as

request.setAttribute("user", userObject);

This is typically required when your servlet needs to pass on an object to another servlet/jsp. And the servlet needing this object can get it throught request.getAttribute(“blah”);
Note that this returns an object, which will need to be type-casted back. i.e. In your servlet, you will need to use the a similar code as:

User user = (User)request.getAttribute("user");

In this way, you can get back the object in your present servlet/jsp which you stored in another servlet/jsp.
Also note, that attributes can be passed in the request only on server, and cannot be passed to the client in this way.


Sep 4 2009

Photographer of the year competition

Better photography, a well known photography magazine of India organizes the “Photographer of the year” competition every year. And year after year, they have organized it successfully offline, accepting entries from the best photographers across India and abroad. This year, however they decided to take this online and canvera.com being the associate partner had to come up with a web application that would enable photographers to submit their entries through a brand new site.

Three weeks was the allotted time for the completion of the project in which the design (and its thousand iterations), development, and QA and all the tweaks would all had to be completed. And my team, i.e. Mayur (a.k.a the designer), Kanupriya (product manager) and myself (sole developer) were given the responsibility for the web application. It was definitely a roller coaster ride; new designs being discussed daily, new functional requirements and the architecture slowly but surely building up.

I had an option of choosing Ruby on Rails or php or Java EE to build this application, and for a two weeks time frame the former two would have definitely been faster. But we decided for Java, and built the entire application using jsp/struts/spring/hibernate. It’s a standard web application: A home page which briefly introduces the application, some static pages which explain the competition in the detail, a registration page (with forgot password functionality), login, user profile page, category page, theme page and upload image page. Image storage and thumbnail generation have been given a lot of importance, and is pretty scalable.

A javascript library is needed in every modern application, and although a big fan of YUI I decided on jquery this time. And I am amazed by the variety and support in jquery plugins, and slowly becoming my favorite. Although this is the first application where I hadn’t used any AJAX in the whole application, the whole experience is pretty smooth in my opinion. The image upload could have been a little smoother(and it will be, I promise :) ) with the flash uploader. Some more small features like an ajax feedback modal dialog box and editing image details might follow soon.

All in all, it was a very fulfilling experience developing the application, which is arguably the one in which the best Indian photographers, young photographers and wedding photographers will upload their best shots! I hope that the best photographers in India would find participating in the contest simple and enjoyable.