简体   繁体   中英

How to track session of android request in Java servlets

My server code is:

public Testing() {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    Integer accessCount=(Integer)session.getAttribute("sessionCount");

      if(accessCount==null){
          accessCount=new Integer(1);
          System.out.println("Welcome for first time....");


      }else{
      System.out.println("Welcome for "+ accessCount+" time to our website....");
      System.out.println("The request id"+session.getId());
      accessCount=new Integer(accessCount.intValue()+1);
    }
      session.setAttribute("sessionCount", accessCount);

}

When I hit my server from a browser it tracks the session correctly. The output is:

Welcome for first time....
Welcome for 2 time to our website....
The request id: 00A24FAF40E130E09F38D52311EF8F1D
Welcome for 3 time to our website....
The request id: 00A24FAF40E130E09F38D52311EF8F1D
Welcome for 4 time to our website....
The request id: 00A24FAF40E130E09F38D52311EF8F1D
Welcome for 5 time to our website....
The request id: 00A24FAF40E130E09F38D52311EF8F1D

But when I hit it from Android Studio using Android Emulator the output is:

Welcome for first time....

Welcome for first time....

Welcome for first time....

Welcome for first time....

My code for hitting the servlet is

    private String getServerResponse(String json){
    HttpPost post= new HttpPost("http://10.0.2.2:23130/FirstServlet/welcome");
    try {
        StringEntity entity=new StringEntity(json);
        post.setEntity(entity);
        post.setHeader("Content-type", "application/json");
        DefaultHttpClient client=new DefaultHttpClient();
        BasicResponseHandler handler=new BasicResponseHandler();
        try {
            String response=client.execute(post,handler);
            return response;

        } catch (IOException e) {
            Log.d("JWP", e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        Log.d("JWP", e.toString());
    }


    return "Unable to contact server......";

}

I give the sign up data, and press signUp button continuously in Android side, so it prints the output as I mentioned on server side.

So my question is, How do I track the session of an Android request using HttpSession , in Java servlet?

It looks like the problem is that you are not handling cookies on the android side, so the JSESSIONID is not being stored / sent back to the server.

It's been years since I used low-level HTTP code like HttpPost, but if I remember correctly, you can set up cookie handling with CookieHandler.setDefault(new CookieManager());

There are some really good higher-level HTTP and REST libraries out there - you should do yourself a favor and learn at least one of them. If you're doing JSON REST (which it looks like you are), I'd suggest the excellent Retrofit library.

Of course, you'd have to figure out how to get Retrofit to use cookies, too, which involves OkHttp interceptors - this should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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