简体   繁体   English

将数据从Servlet Java发送到JSP

[英]send data from servlet java to jsp

I'm doing a project in java in which I implemented a chat, everything works perfectly only when I receive messages I can not print the web page. 我正在用Java实现一个聊天项目,只有当我收到无法打印网页的消息时,所有内容才能正常运行。 In my Servlet I have a callback method that is invoked when messages arrive, in fact if you see mold them in the console, but if you are sending them to the jsp using the RequestDispatcher can not get them to see. 在我的Servlet中,我有一个回调方法,当消息到达时会调用该方法,实际上,如果您在控制台中看到了它们,但是如果使用RequestDispatcher将它们发送到jsp,则无法看到它们。

I would like to know if there is a system that the jsp page listens for a callback method in the servlet? 我想知道jsp页面是否在servlet中侦听回调方法的系统?

Obviously, this system should not be constantly invoke the class I have something absurd like that. 显然,这个系统不应该经常调用我有这样荒谬之类的类。

So that I can print the messages I receive. 这样我就可以打印收到的消息。


This is my code I put a comment where I should print eventually find in jsp page, or do a redirect by passing parameters post or get 这是我的代码,我在其中添加了注释,最终应该在jsp页面中进行打印,或者通过传递参数post或get进行重定向。

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    String strJid = (String) request.getParameter("jid");
    String strMessage = (String) request.getParameter("textMessage");


    Connection connection = (Connection)getServletContext().getAttribute("classConnection");

    ChatManager chatManager = connection.getChatManager();
    Chat newChat = chatManager.createChat(strJid, new MessageListener(){


        @Override
        public void processMessage(Chat chat, Message message){

     //from here I have to print the message.getBody () in jsp page,
    //how can I do? I'm happy also reload the page and pass as a parameter to get or post

        }
    });

    try{
        newChat.sendMessage(strMessage);
    }
    catch(XMPPException e){
        System.out.println("Errore invio messaggio");
    }


}

To implement a callback method in your servlet to receive chat messages is the wrong approach. 在servlet中实现回调方法以接收聊天消息是错误的方法。 A servlet will be called once for a browser request, creates the HTML page and send it back to the browser. 一个servlet将针对浏览器请求被调用一次,创建HTML页面并将其发送回浏览器。 After that there is no such kind of a connection between the servlet and the web page. 此后,servlet与网页之间没有这种连接。

In traditional web programming all communication between browser and server is intiated by the client. 在传统的Web编程中,浏览器与服务器之间的所有通信都是由客户端发起的。 There is no way for the server to send a message to the client. 服务器无法将消息发送到客户端。 Workarounds are long polling requests. 解决方法是长时间轮询请求。

But nowadays you can use Websockets . 但是如今,您可以使用Websockets There are several frameworks supporting Websockets both for client and server side. 有几种支持客户端和服务器端Websocket的框架。 One of them is Atmosphere . 其中之一就是大气 Their tutorial is a chat application. 他们的教程是一个聊天应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM