Link to home
Start Free TrialLog in
Avatar of cool_baba
cool_baba

asked on

Alternate for Cookies and Session variables

Hi,
 Both Session Variables and Cookies require client to have cookies enabled on their computer. Is there any alternate which doesn't require cookies enabled and stores session values.
 
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Both Session Variables and Cookies require client to have cookies enabled on their computer
       
     A misconcept here. Actually session variables are stored ONLY in server side and have nothing to do with cookies or url rewriting.

    other than that, yes, as objects said , 2 ways to implement session:
    1. cookie
    2. URL rewriting so called jsessionid


acton
Avatar of cool_baba
cool_baba

ASKER

>> A misconcept here. Actually session variables are stored ONLY in server side and have nothing to do with cookies or url rewriting.
Its true that session variables are stored only on server side but it needs cookies enabled on client as it use a cookie called SessionID. A bit annoying but that is how it works.
  I am going to work on JSessionid I hope it doesn't require cookies enabled on client
 
Baba
url rewriting does not use cookies
Isn't it something like GET for HTML forms, with limitations on data transfer to the server. If anybody knows ViewState using the JSP.
 
basically just involves passing the session id in the query string
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Colr__ I am using firefox. it doesn't allow session variables or use URL rewriting.
Im using firefox too and Ive never had a problem with setting session variables. The session variabels are not 'allowed' by the browser, it is a function of the J2EE container. If you attemp to use a session variable, and the container detects that the browser wont accept cookies, the container will then use URL rewriiting. This all happens transparently to the browser.

As far as how URL rewriting works, I dont know that - Ive never came across a situation where Ive had to delve into the implementation of it - all you need to know is that session variables will work, how it does it is not important.

So, using session variables, it doesnt matter that your breowser may nor may not accept cookies, as if it doesnt, then the container will automatically switch to URL rewriting.

colr__
Use this code in mozilla firefox, with cookies disabled.

<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

code for SaveName.jsp

<%
   String name = request.getParameter( "username" );
   session.setAttribute( "theName", name );
%>
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>

code for NextPage.jsp

<HTML>
<BODY>
Hello, <%= session.getAttribute( "theName" ) %>
</BODY>
</HTML>


I have tried it in netscape as well .. if you don't allow cookies value is always null (exception internet explorer)

Thanks for your time and concern Colr__
Very strange, I've never seen this before.

Can you confirm when using FF and netscape that the SaveName.jsp URL defenetaly has the value set in its URL? For example it should look like this:

SaveName.jsp?username=colr__

Im just wondering if the lack of quotation marks around the html input's are confusing FF and Netscape??

colr__
>>Im just wondering if the lack of quotation marks around the html input's are confusing FF and Netscape??
    I just didn't understand it. could you modify the code and write it here.

>>Can you confirm when using FF and netscape that the SaveName.jsp URL defenetaly has the value set in its URL? For example it should look like this:
>>SaveName.jsp?username=colr__

It doesn't show username in url ..
 
Syed
Opera doesn't support either ..
ok, if it doesnt show the username in the url, then the username is not being passed correctly to SaveName.jsp in all but IE.

Try modifying your first html page this:

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="SaveName.jsp">
What's your name? <INPUT TYPE="TEXT" NAME="username" SIZE=20>
<P><INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>

colr__
still not working..
Ive just realised, your using post,. so you wouldnt see the variabels in the URL of SaveName.jsp. Try changing the methof type to GET and see if the URL is showing the variables in erach of the browsers:

<FORM METHOD="GET" ACTION="SaveName.jsp">

should result in the URL:

SaveName.jsp?username=colr__

colr__
yes, variable value is transferred to second page. I mean using GET shows result as: SaveName.jsp?username=colr__

One last test - change SaveName.jsp to the following

<HTML>
<BODY>
Hello, <%= session.getAttribute( "username" ) %>
</BODY>
</HTML>

Let me know if this wokr son all brosers. Im on a mission now...

colr__
Doesn't work for GET or POST. I don't thnk we can access GET or POST variables on page other than target page.
Sorry, im an idiot, its suppoed to be this:

<HTML>
<BODY>
Hello, <%= request.getParameter( "username" ) %>
</BODY>
</HTML>

colr__
Yes it works .. So variables are accessable on second page.
it works for all browsers.
helloo Colr__ are u still working on your mission ?

Here is how i solved my problem..
 source : http://home.earthlink.net/~alxdark/software/wcd-guide/ch05s03.html

If the user has disabled cookies then session variables doesn't work as client browser doesn't allow to set cookie called SESSIONID, which carries the session id information for the server. To overcome this problem urlrewriting  is used  by every URL written out by a servlet or JSP page should be run through the

   public String HttpServletResponse.encodeURL( String url );
   public String HttpServletResponse.encodeRedirectURL( String url );

methods in the servlet API. first one (encodeURL) just attaches the sessionid with the url, second (encodeRedirectURL) rewrites url if cookies are disabled otherwise, sessionid cookie is stored on computer.

Code i posted above should be changed like this:

<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

code for SaveName.jsp

<%
   String name = request.getParameter( "username" );
   session.setAttribute( "theName", name );
%>
<HTML>
<BODY>

<A HREF="<%= response.encodeRedirectURL("NextPage.jsp") %>">Continue</A>   //    CHANGE IN CODE
</BODY>
</HTML>

code for NextPage.jsp

<HTML>
<BODY>
Hello, <%= session.getAttribute( "theName" ) %>
</BODY>
</HTML>