Shilohcity
Technical User
Hi there
I have been working on reading and writing cookies using JSP. I have some code which works fine as part of a JSP page. I decided it would be nice to have this as a bean and to make calls to it as needed. After many trials and tribulations I finally have the bean setup and working except....it returns null no matter what cookie I get it to read. Even when I try to return all cookies and all values I get null. Placing the original JSP code into the same page gives valid results. Any ideas or advice on the code below would be most appreciated.
package test;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class cookie extends HttpServlet {
protected final static String cookiePref= "pref";
protected final static String cookieCnt= "cnt";
protected String pref = null;
protected String prefFull = null;
protected String cnt = null;
protected String cntFull = null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i< cookies.length; i++) {
Cookie cookie = cookies ;
String name = cookie.getName();
if (name.equals("pref") {
pref = cookie.getValue();
if (name.equals("cnt") {
cnt = cookie.getValue();
}
}
}
}
}
public String getPrefCookie() { return pref; }
public String getCntCookie() { return cnt; }
} "Creativity is the ability to introduce order into the randomness of nature." Eric Hoffer
Visit me at
I have been working on reading and writing cookies using JSP. I have some code which works fine as part of a JSP page. I decided it would be nice to have this as a bean and to make calls to it as needed. After many trials and tribulations I finally have the bean setup and working except....it returns null no matter what cookie I get it to read. Even when I try to return all cookies and all values I get null. Placing the original JSP code into the same page gives valid results. Any ideas or advice on the code below would be most appreciated.
package test;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class cookie extends HttpServlet {
protected final static String cookiePref= "pref";
protected final static String cookieCnt= "cnt";
protected String pref = null;
protected String prefFull = null;
protected String cnt = null;
protected String cntFull = null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i< cookies.length; i++) {
Cookie cookie = cookies ;
String name = cookie.getName();
if (name.equals("pref") {
pref = cookie.getValue();
if (name.equals("cnt") {
cnt = cookie.getValue();
}
}
}
}
}
public String getPrefCookie() { return pref; }
public String getCntCookie() { return cnt; }
} "Creativity is the ability to introduce order into the randomness of nature." Eric Hoffer
Visit me at