3

I have a Java/Jsp web application where at search_list.jsp page I am setting the result in the session object like this --

<%

  Object resultdata = req.getSession().getAttribute("search_result");
  if(resultdata == null)
  { 
    resultdata  = (SearchResult)getSearchResult();
     request.getSession().setAttribute("search_result", resultdata);
 }

%>

From web browser when I hit another page and comeback to search_list.jsp retrieving of result object from session works fine , but same scenario always return > resultdata null in android native webview. Following is the code of webview from android native app.

public class HomeActivity implements OnClickListener {

  public WebView mWebView;
  public String sessionCookie =  null;

  protected void onCreate(Bundle savedInstanceState) {

            mWebView = (WebView) findViewById(R.id.webView1);
            WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webSettings.setLoadsImagesAutomatically(true);
              mWebView.loadUrl(appPrefer.getAppURL());          
         mWebView.setWebViewClient(new WebViewClient());

   }

  public void saveSession()
         CookieSyncManager cookieSyncManager =             CookieSyncManager.createInstance(mWebView.getContext());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    if(mWebView!=null)
    {
            sessionCookie = cookieManager.getCookie(getCurrentURLLoad());               

    } 

}

private void addSession(String url) 
  {                 
    CookieSyncManager cookieSyncManager =  CookieSyncManager.createInstance(mWebView.getContext());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);            
    if (sessionCookie != null) {            
            cookieManager.setCookie(url, sessionCookie);
            CookieSyncManager.getInstance().sync();         
        }

 }

This is button click of native where (its kind of shortcut to go result back) i am loading search_list.jsp again for show result. But at web level we already search and shows results and viewing the profile.jsp page. So on click of native listview(url is search_list.jsp) I add session as follow:

public void onClick(View v) {
if(v.getId().== R.id.listview) 
   {
  saveSession();
  mWebView.loadUrl(HOST_URL + SEARCH_LIST );
      addSession();
    }
 }

My problem is its saves/pass all other session cookie values but values set as request.session.attribute not come up they all are >null, after url changes on click from native at jsp level resultdata is always null.

I have tried the method mention in answer, also tried the - https://gist.github.com/arpit/610754 way and I always see all my cookies correct value(which is already working on my posted que..) .. but Attribute set in HTTPRequest session is not there .. i donot think its part of cookie its a httpRequets attribute ( HttpServletRequest->httpSession->setAttribute() ) and my conclusion is its not come at Android webview OR httpclient side. Please someone let me know is that correct or there is a way to read the http request session attribute???

Neha
  • 1,548
  • 2
  • 10
  • 13

1 Answers1

0

After a while debugging and understanding my problem more better I found following things :

1) If you set the object/value in request.getSession().setAttribute() that's saved at server side and not carry in the http req/res so its not possible to access them at native side.

2) But every server crate JSESSIONID that's the key to get data for every hit or unique hit at server memory.

So que is When I am saving all cookie and passing (which have the JSESSIONID cookie also) still why it's not able to get the correct data at server end??

SO found issue at my end: [not sure it's a common issue]

That every time at native side user refresh or start webview fresh with URL (kind of on button click I just re-load url at webview with starting new activity and closing last activity).This always create new JSESSIONID at server side.. ;( I try many way to pass cookie so it will not start new Session, even saved last JSESSIONID cookie and injected in new load url but nothing works. :-( ..

So at last I have to add header/parameter in URL to indicate the server that native is reloading and session will be new so handle accordingly and donot show error of not found result.

Neha
  • 1,548
  • 2
  • 10
  • 13