Let me refrase it. i am writing a new http-server using sockets in java-language(not javascript) and i want to tell client to set cookie. I would like to do that in javascript(setin reading the cookie). If that is not possible, Are there other solutions? Thank you all.
Your question/requirement is extremely vague. Are you really not confusing JavaScript with Java itself or maybe JSP? Or are you using server-side JavaScript? Why would you like to do that for a "http server"? Why would you like to write a HTTP server rather than using en existing one like Apache HTTP Server or Apache Tomcat?
At least, in JavaScript you can manage cookies by document.cookie
. In Java it depends on in which context you're talking. If in Servlet context, use HttpServletResponse#addcookie()
and HttpServletRequest#getCookies()
. If in JSP EL context, use ${cookie}
. If in URL streaming context, use URLConnection#addRequestProperty()
and URLConnection#getHeaderFields()
. If in "raw" Socket context just adhere the HTTP state management specification RFC 2965 and indirectly also the HTTP 1.1 specification RFC 2616 while writing the "raw" data through the socket. But you should already know that as you intended to create a HTTP server.
If you are writing your own HTTP server, then you should be able to set the HTTP header directly:
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
You could also send javascript to the browser to do this (by including your script in a HTML page which you give to the client), but I don't really see the advantage of doing it that way.
The example was taken from the following wikipedia page: