Java - How to insert www in naked domain?

Lets consider you want to insert www in url forcefully for certain reasons. Its easy task you can handle by simple script that I'm gonna give you. Keep in mind you need to change the below code for your domain as per your need.

Scenario: I'm considering the home page domain.com will be redirected to www.domain.com . If you want your page domain.com/forum/view.jsp?thread=124 will be redirect to www.domain.com/forum/view.jsp?thread=124 then you must add this code to every page. Simple idea is make checkURL.jsp and paste the code in this page and include checkURL.jsp in every page.