Communities deserve protection from offenders, but if prison is to work in the interests of the community it must rehabilitate as well as punish and protect. We know that most offenders have low levels of literacy and numeracy, few skills and poor training, while around half of those entering prison are serious drug users.

Our response in the past decade has been to make prisons far more constructive institutions - and they continue to be so in spite of current pressures - by investing huge amounts in rehabilitation, covering drug treatment, work programmes, training and education.

Locking someone up costs around £37,000 a year. That is £37,000 of taxpayers' money, taxpayers who may ask: "What is in it for me?" The answer comes in two parts - first, that prison works not only to keep the public safe but also to reduce reoffending; and second, that prison itself becomes a constructive part of the community. In other words, it can work for everyone - first and foremost for the law-abiding, taxpaying majority, but also for offenders who take the opportunities available to turn away from crime.

But enhanced training and education is not simply a handout. It needs to be earned. This is the essence of a new "contract" between offenders and the community that the prisons minister David Hanson is developing. The idea is to balance the opportunities we give offenders to turn away from crime with what the community expects in return.

That means meeting certain standards of behaviour while in prison or on release - getting off drugs and staying off, for instance. It also means giving the community a greater role in setting out what is expected of offenders. There will be incentives for those who take the chances offered to them, as there will be penalties for those who do not.

I am convinced that ideas such as this "contract" will build confidence in the criminal justice system as a whole - as will the development of community courts, which work with the police, crown prosecution service, probation officers, drug teams and others. The first community courts - one with a full-time judge in the North Liverpool community justice centre, and a magistrates-based project in Salford - have been a huge success. There are now 11 other pilots operating across the country, from Middlesbrough to Plymouth.

The success of the community justice approach is rooted in the belief that the courts need to understand the problems of that community. They need to have contact with the community. They need to respond to the community. And that level of contact and responsiveness increases confidence in the courts within the community.

Prisons can learn from this approach by playing a more active role in helping build community confidence in the justice system. Gloucester prison, for instance, has been running a scheme where the hours of work a prisoner clocks up contribute to a "timebank" which stores pledged hours of volunteering. In this way the work done in prison - in Gloucester's case repairing bicycles that are sent to developing countries - benefits the local community. An hour's work on the inside can mean a lift to the shops for an elderly person on the outside, or a hospital visit or time spent by volunteers to clean the local park.

Through such projects a prison becomes an active part of a community, which is constructive for offenders and for the communities they have wronged. It is not just the prison walls themselves that will offer a visible symbol of justice being done.

To build confidence in the system we need more such schemes. We need courts, prisons and probation services that are a visible part of the community - as the rule and not the exception. Above all this will require a change in culture and attitude.

What is needed is to extend the momentum of reform in the culture of public services - which we have seen, for example, with education and health - to the entire criminal justice system, including the courts, prisons and probation service.

Securing a safe and fair society requires more than the operation of the law-enforcement agencies and the criminal justice system. The system must provide collective benefit: punishment and rehabilitation for offenders; justice for victims and for local people; and value for the taxpayer. A system working for everyone.

· This is an edited version of a speech given last night by Jack Straw, the secretary of state for justice, to the Royal Society for the Encouragement of the Artsstrawj@parliament.uk