Home > Java > javaTutorial > body text

How to solve the problem that Servlet Cookie cannot get the value in java

怪我咯
Release: 2017-06-30 10:39:18
Original
2039 people have browsed it

This article mainly introduces the relevant information on the solutions to the reasons why the value cannot be obtained in Servlet Cookie in java. Friends who need it can refer to it

Solution to the reason why Servlet Cookie cannot get the value in java

Phenomena:

When testing the HTTP request with Cookie, it was found that the server used request. getHeader("cookie") can get the value; but request.getCookies() cannot

Reason:

I checked the specific cookie value of the browser and found


http://localhost:8080/

When accessing, the cookie value is placed under localhost, and the SESSIONID automatically generated by the server is also stored under the localhost path.

http://127.0.0.1:8080/

When accessing, the cookie uid value is placed under 127.0.0.1:8080, and the cookie under 127.0.0.1:8080 The Servlet on the server side can never get it; and the SESSIONID automatically generated by the server is under 127.0.0.1, which is different from the uid storage location. So when the server sends a cookie,

 Cookie mycookies[] = request.getCookies();
Copy after login

needs to be changed to

String host=request.getHeader("host");
Copy after login

or abandon the setting

if(host.indexOf(":")>-1){
  host=host.split(":")[0];
  }
Copy after login

so that the cookie value is also saved at 127.0 Under .0.1, it is not related to the port number


Of course, if the server already has a domain name, there will be no cookie value that cannot be obtained under the port number

Solution :

mycookie.setDomain(host);
Copy after login
Add the previous line to get it,

The above is the detailed content of How to solve the problem that Servlet Cookie cannot get the value in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template