Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

A problem with Set-Cookie

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a web application where I want to use Cookies. I Managed to learn how to create Cookies, but know I need to receive a Cookie in a Swing Controller; after many hours in the Internet I have only found references that I should use "Set-Cookie:name=userName" in HTTP response header, but I have not been able to determine how to do it. Do I do it in HTML or in Java Script? I haven't found anything.
 
Rancher
Posts: 3926
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is some discussion here:  https://stackoverflow.com/questions/44222643/building-an-http-response-header
 
Marshal
Posts: 25930
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from that link, here's some basic information. An HTTP response header is part of an HTTP response, which will be sent by your server in response to an HTTP request.

So I'm a bit confused by your post. First... you're writing a web application. So that would be the server part of the client-server relationship, right? But then... you need to receive a cookie in a Swing controller. That sounds a lot like the client part.

Anyway the cookie is generated and sent by the server. So JavaScript isn't going to be involved (unless you're using server-side JavaScript, that is). And neither is HTML, because the HTTP response headers are sent before the response body, which is where any HTML would be located.

Does this help?
 
Norm Radder
Rancher
Posts: 3926
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A cookie can also be sent in the request header.
https://stackoverflow.com/questions/57116725/java-how-to-get-and-add-cookies-to-request-header-correctly
 
Paul Clapham
Marshal
Posts: 25930
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True enough. So clarification of requirements is definitely needed.
 
Alejandro Barrero
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for the replies. First I apologize for writing "Swing" where I should have written "Spring".
In my controller I have

But so far I am only getting "Atta".

Thanks too for the links, but I didn't find the answer there.

Yesterday, I found something promising in a web page https://community.apigee.com/questions/6587/modify-set-cookie-header-addchange-in-javascript.html; it says
"I am able to set multiple cookies in JavaScript callouts,

example which works:

context.setVariable('response.header.set-cookie.1',
  'Oatmeal=delicious; path=/;');
context.setVariable('response.header.set-cookie.2',
  'Trefoil=donotwant; path=/;');
 
resulting outbound response headers:
Set-Cookie: Oatmeal=delicious; path=/;
Set-Cookie: Trefoil=donotwant; path=/;"

Unfortunatelly, the JavaScript code

Run on a click to Submmit does't work because "context" is not define.



Staff note (Paul Clapham) :

I edited the link so that it worked properly.

 
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://dzone.com/articles/how-to-use-cookies-in-spring-boot
 
Paul Clapham
Marshal
Posts: 25930
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alejandro Barrero wrote:First I apologize for writing "Swing" where I should have written "Spring".



Ah! So let's put this thread into the Spring forum.
 
Alejandro Barrero
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all, but I solved my problem without a cookie; the web page just returns the userName  with a thymeleaf variable

The controller gets the value as a request param
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this 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