user1595858 user1595858 - 3 months ago 16
JSON Question

Call Servlet doPost method from JSF Bean (Primefaces)

I have a servlet method where i need to pass a json object. This json object will be created in one of the managed bean(JSF). I just tried injecting the bean into the servlet to get the json object, but i am getting runtime exception, may be it is not possible that way. So i want to call the servlet from JSF bean. Any ideas how to call from the bean?


private static final long serialVersionUID = 1L;
TopicController topicController;
TopicBean topicBean;
List<JsonTopicObj> jsonTopicList;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {

// here code for subscription

Meteor mateor =
new MyAtmosphereResourceEventListener());

boolean isLongPolling = true;
if (mateor.transport() == LONG_POLLING) {
isLongPolling = true;
} else {
isLongPolling = false;
System.out.println(" Now in the Get method of Atmosphere");
// create the broadcaster for the particular topic
String topicId = "default"; // for all users right now
Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(
topicId, true);
//set that broeadcaster
mateor = mateor.resumeOnBroadcast(isLongPolling);


public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException {}


@ManagedBean(name = "testController")
public void listAllTopics() {
JsonTopicObj jsonTopicObj;
StringWriter out = new StringWriter();
JsonFactory jfactory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper();
jsonTopicList = new ArrayList<JsonTopicObj>();

for (int i = 0; i < topicBean.getTopicVOArray().length; i++) {
jsonTopicObj = new JsonTopicObj();

mapper.writeValue(out, jsonTopicList);

now i need to call the servlet dopost method and send "out.toString()"

Answer Source

Where is your first control? Does it go to managed bean and then servlet and this bean is responsible for forwarding to the servlet? If it is so, you can create the JSON object, get your request and response from FacesContext and dispatch the request to the servlet.

FacesContext give your request and response and from that you can dispatch your request.