Cookies

Hi How do .jsp file know that browser supports cookies or not?Vinay
[96 byte] By [psvinayram] at [2007-9-26 1:27:52]
# 1

You can test it:

1. establish a cookie:

Date dtoday = new Date();

String stoday = dtoday.toString();

Cookie lastVisit = new Cookie("lastVisit", todayString);

response.addCookie(lastVisit);

2. access the cookie:

Cookies[] myCookies = request.getCookies();

for (int i=0; i<myCookies.length; i++) {

out.println("Cookie name:" + myCookies[ i ].getName());

...

}

You get your Cookie, if the browser supports cookies!

Andreas Spall>

spallnet at 2007-6-29 1:12:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi Do u want me to do step 1 in one jsp and step 2 in second jsp file?Thanks for ur responseVinay
psvinayram at 2007-6-29 1:12:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Hi Do u want me to do step 1 in one jsp and step 2 in second jsp file?Thanks for ur responseVinay
psvinayram at 2007-6-29 1:12:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Yes, you should.
crusca_lucio at 2007-6-29 1:12:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

No you shouldnt

try:

YourJSP.jsp

********

Begin

********

<%@ page language="java" contentType ="text/html" %>

<%@ page import="java.util.*" %>

<%

StringsIsFirstRun= request.getParameter("questionedpos");

booleanbresult = false;

if (sIsFirstRun == null) {

Date dtoday = new Date();

String stoday = dtoday.toString();

Cookie lastVisit = new Cookie("lastVisit", todayString);

response.addCookie(lastVisit);

//sIsFirstRun = "NoIsNot";

%>

<meta http-equiv="refresh" content="0; URL=orderprocess.jsp?sIsFirstRun=NoIsNot">

<%

} // eo if(sIsFirstRun..

else {

Cookies[] myCookies = request.getCookies();

for (int i=0; i<myCookies.length; i++) {

String sName = "" + myCookies[ i ].getName());

if(sName.equals("lastVisit")

bresult = true;

} // eo for(int...

} // eo else

if (bresult == true) {

// *****************************

// Your jsp

// *****************************

}

********

End

********

Info:

<meta http-equiv="refresh" content="0; URL=orderprocess.jsp?sIsFirstRun=NoIsNot">

This can be critical

Andreas Spall

spallnet at 2007-6-29 1:12:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
ERROR:String sIsFirstRun = request.getParameter("questionedpos");CORRECTString sIsFirstRun = request.getParameter("sIsFirstRun");SORRY
spallnet at 2007-6-29 1:12:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

hi,

checking whether your browser supports cookies or not is fine.

but ultimately you want to maintain sessions. the best way for this is to use the concept of URLRewriting in which your web server itself checks ip your browser compatibility and accordingly either wirtes cookies to the browser if it supports or will append it to the url if it doesn't. this solves all the headaches about cookies and the security breach.

regards

srini

vams00 at 2007-6-29 1:12:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...