The two are not mutually exclusive. Someone can be rehabilitated and punished for their crimes at the same time. It does not need to be an either or proposition. Jail should ready people to return to the real world, but it should not be an overly enjoyable experience either. We don't want it to be so pleasant that people want to come back!
There are some circumstances where this works. I have a friend that has turned his life around through the rehabilitation that was provided in the jail. I think you need to look at it on a case by case basis and realize that for some it will not help, but for some it can make a difference.
While there are indeed many lost causes that go through the prison system, many people there are responsible for an idiotic mistake that they are deeply remorseful for and want a chance to do right. People that are sincere in that should in most cases be given the tools to do so.
I think jail can function as a rehabilitation for petty crimes. I don't think that in all instances rehabilitation works. In cases of multiple murders, I would not want to see that person released back into society. I also think age can play a huge factor in this. There is a huge difference between committing a crime at age 11 or age 35.