Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Bean returns null

Status
Not open for further replies.

Shilohcity

Technical User
Jul 12, 2000
136
GB
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(&quot;pref&quot;)) {
pref = cookie.getValue();
if (name.equals(&quot;cnt&quot;)) {
cnt = cookie.getValue();

}
}
}
}
}

public String getPrefCookie() { return pref; }
public String getCntCookie() { return cnt; }

} &quot;Creativity is the ability to introduce order into the randomness of nature.&quot; Eric Hoffer

Visit me at
 
The line

Cookie cookie = cookies ;

should read

Cookie cookie = cookies ;
 
Thanks for the comment.

Forgive me if I'm being blind but the two lines above seem to be the same. What do I need to change about this line?

Cookie cookie = cookies ;

Thanks
Justin X-) &quot;Creativity is the ability to introduce order into the randomness of nature.&quot; Eric Hoffer

Visit me at
 
Maybe

Cookie cookie = cookies ;

needs to be

Cookie cookie = cookies ;

??
 
OK. Something is stripping the left square bracket, &quot;i&quot; and right square bracket from our responses. Maybe with spaces in between...

Cookie cookie = cookies [ i ];
 
Sorry, didn't mean to hit submit. All code should be placed instead the Tek-Tips code tags. When you hit preview it will show a limited list of tags below the post and there is a link for a complete list. If the code tag is not used then things like brackets may not show.

Without code tag:
Cookie cookie = cookies;

With code tag:
Code:
Cookie cookie = cookies[i];
 
Ahhhhh...

Suddenly it has all become clear. I thought I might have been going a little mad comparing the lines of code.

Actually now that I look at my code however it already says

Cookie cookie = cookies ;

Does anybody have any further ideas?

Thanks
Justin X-) &quot;Creativity is the ability to introduce order into the randomness of nature.&quot; Eric Hoffer

Visit me at
 
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
for (int i = 0; i< cookies.length; i++)
{
Cookie cookie = cookies [ i ];
String name = cookie.getName();
if (name.equals(&quot;pref&quot;))
{
pref = cookie.getValue();
if (name.equals(&quot;cnt&quot;)) <== Did you intend pref.equals(&quot;cnt&quot;) ?
{
cnt = cookie.getValue();
}
}
}
}
 
Hi there

I have got the code going at last. I hate to say this but the problem was very simple (as usual). Being new to JSP and java I had not quite got my head around the structure of the code. All that was happening was that the &quot;doGet&quot; function above was not actually being run at any stage so the return value was never set. Ooooops!! :)

Thanks for all the help above.

Also as I said I am fairly new to JSP and Java so I'm a little confused by a lot of the naming conventions and import declarations for beans does anybody know of a good place to read up on this?

Thanks
Justin X-) &quot;Creativity is the ability to introduce order into the randomness of nature.&quot; Eric Hoffer

Visit me at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top