从Android的访问Servlet时维护HTTP会话Android、Servlet、HTTP

2023-09-06 00:37:58 作者:大游戏家

我不能够保持会话,当我访问一个servlet的Andr​​oid系统。我只是通过参数应用随着URL到Servlet从数据库中,并将其存储在会话中收集数据,但我不能够reterive它在后续的请求。

I am not able to maintain session when I access a servlet from Android. I just pass the parametres along with the URL to the servlet which gathers data from database and stores it in the session, but I am not able to reterive it on subsequent requests.

请问会议GET过期当我关闭的PrintWriter 在servlet?

Does the session get expired when I close the PrintWriter in the servlet?

推荐答案

这是在客户端的一个问题。 HTTP会话是由一个cookie维持。客户端需要确保其正确地发送的后续请求按照HTTP规范该cookie背面。 HttpClient的API提供 的CookieStore 类,这里面,你需要在的 的HttpContext ,你又需要通过对每一个 的HttpClient#的execute() 调用。

This is a problem in the client side. The HTTP session is maintained by a cookie. The client needs to ensure that it properly sends the cookie back on the subsequent requests as per the HTTP spec. The HttpClient API offers the CookieStore class for this which you need to set in the HttpContext which you in turn need to pass on every HttpClient#execute() call.

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);
// ...

要了解更多关于会话的作品,读这样的回答:java的servlet实例化和会话变量

To learn more about how sessions works, read this answer: java servlet instantiation and session variables