How can i read cookies of the client system for Java web application.
My application on server and i want to fetch some information of client system.
Use the following in your servlet/jsp to get cookies
javax.http.servlet.Cookie[] cookies = request.getCookies();
i want to make cookies with some information which user enters and next time i use that information
An example of what you want to do is here
You get cookies from the client system by using a javascript (on the client) to read the cookie and formulate a web request to send to your server.
Use something like the javascript XMLHTTPRequest function (on the client) to send the request to your server (including the cookie value).
Related
I have a REST API made with Spring Boot. I can add entities (Persons) to a database using HTTP requests.
I would like to have a web page display those entities dynamically: whenever a new Person is stored in the database, the page displays it on top of the list. No need to refresh the page.
What are the ways to achieve this? Would JSP be enough? Do I need javascript?
The simpler the better.
What are the ways to achieve this? Would JSP be enough? Do I need
javascript?
Yes you need javascript. But you can embed it in JSP too.
This is a solved problem, you will find numerous examples in the web if you search for "websocket spring". Example https://spring.io/guides/gs/messaging-stomp-websocket/
If you are not stuck in JVM-land and RDBMS, I would encourage you to take a look at firebase-angular stack (3-way binding)
Further read
Read this https://stackoverflow.com/a/12855533/6785908
Shameless copy since posting just a link is not advisable as link may break in future
In the examples below the client is the browser and the server is the webserver hosting the website.
Before you can understand these technologies, you have to understand
classic HTTP web traffic first.
Regular HTTP:
A client requests a webpage from a server.
The server calculates the response
The server sends the response to the client.
Ajax Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at
regular intervals (e.g. 0.5 seconds).
The server calculates each response and sends it back, just like normal HTTP traffic.
Ajax Long-Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
The server does not immediately respond with the requested information but waits until there's new information available.
When there's new information available, the server responds with the new information.
The client receives the new information and immediately sends another request to the server, re-starting the process.
HTML5 Server Sent Events (SSE) / EventSource:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
The server sends an event to the client when there's new information available.
Real-time traffic from server to client, mostly that's what you'll need
You'll want to use a server that has an event loop
Not possible to connect with a server from another domain
If you want to read more, I found these very useful: (article), (article), (article), (tutorial).
HTML5 Websockets:
A client requests a webpage from a server using regular http (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
The server and the client can now send each other messages when new data (on either side) is available.
Real-time traffic from the server to the client and from the client to the server
You'll want to use a server that has an event loop
With WebSockets it is possible to connect with a server from another domain.
It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to
implement the client side, which is very easy!
If you want to read more, I found these very useful: (article), (article) (tutorial).
Comet:
Comet is a collection of techniques prior to HTML5 which use streaming
and long-polling to achieve real time applications. Read more on
wikipedia or this article.
I know how to get cookies value from HTTP request with that way
> httpReq.getheader()
but now i want to access and get cookies values that set in browser wherever and use it in java classes without sending HTTP request?
is it possible ?
It is not possible ... unless your Java code is actually running in the web browser. The Java Tutorials include a page on Accessing Cookies in an Applet or JNLP application.
However, if your Java code is running in the web, then you have a problem because:
most browsers have already dropped support for Java plugins,
Oracle has deprecated browser-side Java as of Java 9.
If your Java code is server-side, then the HTTP request is the only way that information (such as cookies) is passed from the client (browser) to server. The server cannot send requests to the browser. The best it can do is send an HTTP reply that causes the browser to send another request. (Or open a WebSocket ...)
What is required to create same session on different hosts?
There are two stand alone servers. I do not want to setup a cluster environment. I just want the session created in one server is recognized by other
When I request server A with client C, that(server A) will request to server B (with a flag set in header so that it wont go in loop). When I request server B with client C the session is not recognized on server B. what else do I have to do?
I tried setting jsession id
I am using apache httpclient to send the request to other server. I
have added header and I can see cookie header is same in both server,
still its not recognized
request.setHeader("cookie", req.getHeader("cookie"));
Please suggest
#Aadam did you tried HttpSession ?
My objective is to set a cookie from within a servlet called from a java client, and get the cookie when a different servlet is called from the browser.
The java client has an authenticated session with the server.
The server runs locally.
I tried suggestions from a different question and rename put an alias domain name in my hosts file.
I manually set the domain of the cookie to the alias domain, but it still won't return on the server.
request.getRemoteHost() returns 127.0.0.1 in both servlets.
Any help would be appreciated.
A cookie is held in memory (or on disk, but at a specific place) by each client. A Java client and a browser don't share cookies. Two different browsers (IE and Firefox, for example) don't share cookies either. There is no way to do what you want.
I would like to know if it is possible to maintain an authentication (like a session with login and password in php) on a website from a java program, and if anyone had any lead on the subject or some reading for me, that would be great.
thanks
You can use the HttpClient class. Once you have authenticated with the website the server will send you a cookie that you have to send with each successive request in order for the server to think that you are logged in.
all you need is cookies, and java URLConnection. I use this all the time http://www.rgagnon.com/javadetails/java-0092.html
you use readcookies after you open a new urlconnection and writecookies when creating a new one.