• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Using a method to retrieve a cookie

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'm doing some experimenting with cookies. Below is code from a "Murach's Java Servlets & JSP" exercise to create a cookie that stores the first name of a user and adds it to the browser:





Yes, I know it's pretty "ugly" and i've spent hours trying to figure this out :-). Where am I going wrong? Please help. Thanks in advance.

[ September 17, 2008: Message edited by: Derek Harper ]
[ September 17, 2008: Message edited by: Derek Harper ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I am going to do this in steps.

The first step is for academic purposes, I don't support actually doing this.

1) In JSPs, methods need to be declared in JSP Declarations, they are surround with <%! %> not <% %>. So your getKookieValue method should look like this:


This doesn't actually call the method, it just declares it, so use an Expression (<%= %>) to call the method and display the results:


Like I said, I don't support using that, as it requires a lot of Java Code inside the JSP. This makes the JSP hard to manage and error-prone. The next option is the better one:

2) Use a controller servlet that gets called before this page (or every page). Use the Servlet to get the cookie value and store it in the request. Then use the JSP to retrieve the value from the request and display it:


[ August 08, 2008: Message edited by: Steve Luke ]
 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Steve. I'll make sure to take into consideration what you recommended. I'm sorta "new" at this so I want to make sure I fully understand what's happening first.

Thanks again!
 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic