从Managed bean调用Servlet doPost方法

时间:2012-09-05 16:01:43

标签: json jsf servlets primefaces atmosphere

我有一个servlet方法,我需要传递一个json对象。这个json对象将在其中一个托管bean(JSF)中创建。我只是尝试将bean注入servlet以获取json对象,但我得到运行时异常,可能是不可能的。所以我想从JSF bean调用servlet。任何想法如何从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);  

现在我需要调用servlet dopost方法并发送“out.toString()”

1 个答案:

答案 0 :(得分:0)

你的第一个控制在哪里?它是去托管bean还是servlet 这个bean负责转发到servlet?如果是这样, 您可以创建JSON对象,从FacesContext获取您的请求和响应 并将请求分派给servlet。

FacesContext提供您的请求和响应,然后您可以发送您的请求和响应 请求。