servlet 之间通信时出现空指针异常

发布于 2024-11-05 20:50:41 字数 2423 浏览 0 评论 0原文

我在一个servlet中有登录代码:LoginGtalkServlet..

 XMPPConnection connection;

 //@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)


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

    String userName = request.getParameter("username");
    String password = request.getParameter("password");

    System.out.println(userName);
    System.out.println(password);

    //ProxyInfo proxyInfo = new      
    ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222,"gmail.com");
    connection = new XMPPConnection(config);
    config.setSASLAuthenticationEnabled(false);
    try {
        connection.connect();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    try {
        connection.login(userName, password);
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    boolean status=connection.isAuthenticated();
    if(status==true)
    {
        System.out.println("Success");
        response.sendRedirect("GetRoster");
    }
    else
    {
        response.sendRedirect("Loginfailed.html");
    }

并且我的GetRosterServlet有用于检索名册列表的代码

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
     Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
    for (RosterEntry r : entries) {
        String user = r.getUser();
        String name = r.getName();
        System.out.println(name + user);
    }
    roster.addRosterListener(new RosterListener() {
        // Ignored events public void entriesAdded(Collection<String>
        // addresses) {}
        public void entriesDeleted(Collection<String> addresses) {
        }

        public void entriesUpdated(Collection<String> addresses) {
        }

        public void presenceChanged(Presence presence) {
            System.out.println("Presence changed: " + presence.getFrom()
                    + " " + presence);
        }

        @Override
        public void entriesAdded(Collection<String> arg0) {

        }
    });

}

现在问题是我的GetRosterServlet没有被告知我已经登录到Gtalk..ie LoginGtalkServlet没有与GetRosterServlet通信,因此没有连接。 getRoster() 抛出空指针异常...

我如何让名册 servlet 知道用户已登录到 gtalk 并因此获得好友列表

I have login code in one servlet:LoginGtalkServlet..

 XMPPConnection connection;

 //@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)


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

    String userName = request.getParameter("username");
    String password = request.getParameter("password");

    System.out.println(userName);
    System.out.println(password);

    //ProxyInfo proxyInfo = new      
    ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222,"gmail.com");
    connection = new XMPPConnection(config);
    config.setSASLAuthenticationEnabled(false);
    try {
        connection.connect();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    try {
        connection.login(userName, password);
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    boolean status=connection.isAuthenticated();
    if(status==true)
    {
        System.out.println("Success");
        response.sendRedirect("GetRoster");
    }
    else
    {
        response.sendRedirect("Loginfailed.html");
    }

And my GetRosterServlet has the code for retreving the roster list

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
     Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
    for (RosterEntry r : entries) {
        String user = r.getUser();
        String name = r.getName();
        System.out.println(name + user);
    }
    roster.addRosterListener(new RosterListener() {
        // Ignored events public void entriesAdded(Collection<String>
        // addresses) {}
        public void entriesDeleted(Collection<String> addresses) {
        }

        public void entriesUpdated(Collection<String> addresses) {
        }

        public void presenceChanged(Presence presence) {
            System.out.println("Presence changed: " + presence.getFrom()
                    + " " + presence);
        }

        @Override
        public void entriesAdded(Collection<String> arg0) {

        }
    });

}

Now the isssue is my GetRosterServlet is not informed that I haved logged into Gtalk..i.e LoginGtalkServlet is not communicating with GetRosterServlet and hence connection.getRoster() is throwing Null pointer exception...

How do I let the roster servlet know that the user is logged into gtalk and hence get the friends list

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-11-12 20:50:41

请注意,我熟悉 XMPP/GTalk。

当您在 LoginGtalkServlet 中登录时,是否有可能返回某种会话 ID?如果是这样,您可以将其作为查询参数或 POST 正文放入重定向 URL 中,然后 GetRosterServlet 可以从请求中提取会话 ID 并将其用于 Gtalk该会话 ID。

注意:我刚刚所做的编辑只是为了修复一些错误的语法。没有实质性的更改。)

Note that I'm not familiar with XMPP/GTalk.

When you log in to it in LoginGtalkServlet, is it possible for you to be handed back some sort of session ID? If so, you could put that into the redirect URL as a query parameter or in a POST body and then GetRosterServlet could extract the session ID from request and use it to Gtalk via that session ID.

(Note: the edit I just made was just to fix some bad grammar. No substantive changes.)

朕就是辣么酷 2024-11-12 20:50:41

您可以将连接对象保存在会话属性中。

session .setAttribute("connection", connectionObj);

并可以通过使用 session.getAttribute("connection");

You can save the connection object in the session attribute.

session .setAttribute("connection", connectionObj);

And can get by using session.getAttribute("connection");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文