Attach your class instance as request attribute before forwarding to your JSP and use it in the view afterwards. Also, be sure not to do business job in the getter (and return an object if you claim to).
All in all, the class code could look like:
class BeanManager{
private JSONObject jsonObject;
private String jsonObjectString;
public BeanManager() {
jsonObject=new JSONObject();
jsonObject.put("name","jack Daniel");
jsonObject.put("age","3");
jsonObjectString = jsonObject.toString();
}
public JSONObject getJsonObjectString() {
return jsonObjectString;
}
}
The relevant servlet part could look like:
BeanManager bm = new BeanManager();//manipulate it the way you want
request.setAttribute("bean", bm);
request.getRequestDispatcher("/WEB-INF/view.jsp").forward(request, response);
The view could contain the following part:
<script>
var json = ${bm.jsonObjectString};
</script>
This way, json variable will be available in JavaScript context.
In case you'd like to deal with AJAX requests, take a look at How to use Servlets and Ajax? question and its answer. There, JSON object is returned from servlet and later parsed into HTML document.