servlet 之间通信时出现空指针异常
我在一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,我不熟悉 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 thenGetRosterServlet
could extract the session ID fromrequest
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.)
您可以将连接对象保存在会话属性中。
并可以通过使用
session.getAttribute("connection");
You can save the connection object in the session attribute.
And can get by using
session.getAttribute("connection");