0

October 14

web application state

Posted by Ajay

Just the other day i started thinking about application state in our web application. HTTP is essentially a stateless protocol, which basically means that every request between the client and server is essentially a self contained unit with all the information required for each transactions embedded within the request.

Consider the figure above, that represents a typical Http model. Every HttpRequest will encode the information in the HttpRequest header and sends it over to the server which does processing and returns the response back as HTML.

But then there is more to Http than meets the eye. Http has been extended to allow state information to be stored. How many times has the word “session” been thrown around. What exactly is this session?

The HTTP model has three major scoped containers - HttpRequest, HttpSession and ServletContext. We will look at ServletContext some other time. HttpRequest of course works on a per request basis. All variables will have scope only for the lifetime of the request. HttpSession is a container where the scope of variable is essentially global. But how does this HttpSession work? The magic behind HttpSession is a concept called cookies and URL rewriting (if cookies are disabled).

Esentially there are three major ways to maintain the state between multiple stateless protocol requests.

  • cookies - store state information client side, and send that state to server everytime a request is made
  • hidden form fields - store data hidden in forms and send the form everytime a request is sent to the server
  • URL rewriting - encode the state information within the URL. This is usually done using a jsessionid name value pair appended to the URL by the server at the end of each request.

Essentially once a cookie is created with session information, it is stored at the client, for future requests to look it up and regain the state. Alternatively using hidden form fields, the state itself can be transferred from one request to another. Last but not the least, URL rewriting can also do the trick.

Maintaining state is done by the servlet container like Tomcat. Tomcat maintains HttpSession either using client side cookies, or if cookies are disabled through URL rewriting.

Tags: ,

 
0

October 6

how i used Spring at ma’ workplace - Part 1 - Soft Introduction

Posted by Ajay

I’ve been relearning some of the base technologies we use in our project ( there are a lot fo them) but the most interesting one is Spring. I am sure all of u know what Spring is and how it works. Let me just talk about one interesting aspect of spring that we actually use in our project - dependency injection or in technical terms, the Spring IOC Container.

let’s talk about Inversion of Control first. It’s very popularly known as the “Hollywood principle” because of it’s underlying concept which is “you dont call me, I’ll call you“. These are the important principles behind the Hollywood Principle

  • loose coupling - imagine a slice of bread. A slice of bread is loosely coupled because in itself, it is just bread. People buy bread as it is. But then the coolness is when bread gets converted to a vegetable sandwich or bread becomes bread omelet sandwich. That, my dear watson, is called loose coupling. There is a very minimal assumption about the interface of bread. If you decide to make a wheat bread instead of white bread, you can still make a vegetable sandwich or a bread omelet sandwich. Similarly we write a Java object that is independent by itself and has no dependencies. It is exposed through an interface. Any changes in the object will not have an effect on the classes that use this object.
  • sacrifice of control - let the restaurant chef decide what to put in the bread rather than letting the bread maker dictates terms as to what should a bread contain. Similarly we let the system define how it wants to use the object and what it wants to add to the object.

So Inversion of Control is a broad term that encompasses the Hollywood Principle. It is a general principle of most frameworks. Simply stated the framework takes the responsibility of invoking a module’s functionality away fromt he module and puts it in the controlling framework itself. There are many different example of IOC in action. EJBs, JUnit uses IOC , dependency injection in Spring, etc.

What is the difference between Inversion of Control and Dependency Injection?

How about you wait for ma next post :-) ??

References:

  1. http://martinfowler.com/bliki/InversionOfControl.html
  2. http://en.wikipedia.org/wiki/Inversion_of_control

Tags: , ,

 
0

October 1

Does java passes by value or reference?

Posted by Ajay

So the other day someone asked me

“Does Java pass by reference or by value?”

Hey I answered, I ought to know this,

“By value of course.”

So imagine an object Person P passed into a function doModifyPerson(Person p). I call P’s setter, say p.setName(”initalName”) and i then invoke p.setName(”new Name”) inside the function doModifyPerson.

Now the question is does Person p get modified?

So i answered,

“No way jose, Java is pass by value aint it”

Guess what amigos!!…I was dead wrong. Java is pass by value, of course, but in this case the Person object’s reference is passed by value, not the value itsef. So we have two references pointing to the same Person object, so u modify the reference’s value of course the object’s value changes.

But then why does this not happen with Strings, Strings are objects too, but they dont get modified inside the function. Guess what Strings are immutable.

So another instance of not getting my concepts right.

Tags:

Copyright © 2010 “An Image of My Life” blog series All rights reserved. Theme by Laptop Geek.

Total hits: 59911