java – 从Android访问servlet时维护HTTP会话

前端之家收集整理的这篇文章主要介绍了java – 从Android访问servlet时维护HTTP会话前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当我从Android访问servlet时,我无法维护会话.我只是将参数和URL一起传递给servlet,servlet从数据库中收集数据并将其存储在会话中,但我无法在后续请求中检索它.

当我关闭servlet中的PrintWriter时,会话是否会过期?

最佳答案
这是客户端的问题. HTTP会话由cookie维护.客户端需要确保根据HTTP规范正确地将cookie发送回后续请求. HttpClient API为此提供了CookieStore类,您需要在HttpContext中设置该类,而您需要在每次HttpClient#execute()调用时传递该类.

HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
// ...

HttpResponse response1 = httpClient.execute(yourMethod1,httpContext);
// ...

HttpResponse response2 = httpClient.execute(yourMethod2,httpContext);
// ...

要了解有关会话如何工作的更多信息,请阅读以下答案:How do servlets work? Instantiation,sessions,shared variables and multithreading

原文链接:https://www.f2er.com/android/430419.html

猜你在找的Android相关文章