Programmatically keep HTTP Session Alive without browser

12,940

Solution 1

1.

 <session-config>
         <session-timeout>-1</session-timeout>
 </session-config>

Simply paste this piece if code in your deployment descriptor (DD).
If you want to keep your session alive for a particular duration of time replace -1 with any positive numeric value.
Time specified here is in minutes.

2.

If you want to change session timeout value for a particular session instance without affecting the timeout length of any other session in the application :

session.setMaxInactiveInterval(30*60);


**********************
Note :

1.In DD, the time specified is in minutes.
2.If you do it programatically, the time specified is in seconds.

Hope this helps :)

Solution 2

I guess below code can help you, if you can pass JSESSIONID cookie then your container will take responsibility to keep the session alive if its same session that might be created from some other browser.

find the link below that explained a lot.

Click Here

Code snippet from the link above

    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "97E3D1012B3802114FA0A844EDE7B2ED");
    cookie.setDomain("localhost");
    cookie.setPath("/CookieTest");
    cookieStore.addCookie(cookie);
    HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();

    final HttpGet request = new HttpGet("http://localhost:1234/CookieTest/CookieTester");

    HttpResponse response = client.execute(request);
Share:
12,940
Fazal
Author by

Fazal

Working in COre Java and worked on concepts like I18N, JavaCC, GWT, DB and mostly in ambit of web applications

Updated on June 05, 2022

Comments

  • Fazal
    Fazal almost 2 years

    For one of our requirements I am talking between two servers using HTTP protocol. The interaction is long running, where a user might not interact with the other site for pretty long intervals.

    When they come to the page, the log in into the remote site. Every time user tried to interact with the remote site, internally I make a HTTP call (authetication is done based on sessionId).

    I was wondering if there is a way to also refresh the session and ensure that it does not expire.

    As per my limited understanding, browser handles this by passing keep-alive in header or cookie (which I don't understand completely). Can anyone suggest a programmatic way in Java to achieve keep-alive behavior

  • Fazal
    Fazal over 12 years
    Thanks.. I like this idea. But my intention here is to simulate browser behavior. So I would only keep the session alive if I keep getting request from the remote server. But I don't know what header to set in the http request from the remote server to keep the session alive