0

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

@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()”

4

1 回答 1

0

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

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

于 2012-09-06T10:51:20.240 回答