0

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?

Servlet

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 = Meteor.build(req).addListener(
            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.setBroadcaster(broadcaster);
        mateor = mateor.resumeOnBroadcast(isLongPolling);
        mateor.suspend(-1);

}

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

JSF BEAN

@ManagedBean(name = "testController")
@RequestScoped
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()"

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
user1595858
  • 3,700
  • 15
  • 66
  • 109

1 Answers1

0

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.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
benz
  • 4,561
  • 7
  • 37
  • 68