js-cookie/java-cookie — 63★ trên GitHub (Java). A simple Java API for handling cookies
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
IMPORTANT! when deleting a cookie, you must pass the exact same path, domain and secure attributes that were used to set the cookie, unless you're relying on the default attributes.
JSON Data Binding
java-cookie provides unobtrusive JSON storage for cookies with data binding.
When creating a cookie, you can pass a few supported types instead of String in the value. If you do so, java-cookie will store the stringified JSON representation of the value using jackson databind.
Consider the following class that implements the CookieValue interface:
public class Person implements CookieValue {
private int age;
public Person( int age ) {
this.age = age;
}
public int getAge() {
return age;
}
}
And the following usage:
Cookies cookies = Cookies.initFromServlet( request, response );
cookies.set( "name", new Person( 25 ) );
When reading a cookie with the default get() api, you receive the string representation stored in the cookie:
Cookies cookies = Cookies.initFromServlet( request, response );
String value = cookies.get( "name" ); // => "{\"age\":25}"
If you pass the type reference, it will parse the JSON into a new instance:
Cookies cookies = Cookies.initFromServlet( request, response );
Person adult = cookies.get( "name", Person.class );
if ( adult != null ) {
adult.getAge(); // => 25
}
Encoding
This project is RFC 6265 compliant. All special characters that are not allowed in the cookie-name or cookie-value are encoded with each one's UTF-8 Hex equivalent using percent-encoding.
The only character in cookie-name or cookie-value that is allowed and still encoded is the percent % character, it is escaped in order to interpret percent input as literal.
To override the default cookie decoding you need to use a converter.
Cookie Attributes
The default cookie attributes can be set globally by setting properties of the .defaults() instance or individually for each call to .set(...) by passing an Attributes instance in the last argument. Per-call attributes override the default attributes.
Define when the cookie will be removed. Value can be an Expiration.days() which will be interpreted as days from time of creation, a java.util.Date or an org.joda.time.DateTime instance. If omitted, the cookie becomes a session cookie.
Default: Cookie is removed when the user closes the browser.
Define whether your cookie should be restricted to a first party or same-site context
Default: not set
Note that more recent browsers are making "Lax" the default value even without specifying anything here.
Examples:
Cookies cookies = Cookies.initFromServlet( request, response );
cookies.set( "name", "value", Attributes.empty().sameSite( "Lax" ) );
cookies.get( "name" ); // => "value"
Converter
Create a new instance of the api that overrides the default decoding implementation.
All methods that rely in a proper decoding to work, such as remove() and get(), will run the converter first for each cookie.
The returning String will be used as the cookie value.
Example from reading one of the cookies that can only be decoded using the Javascript escape function:
// document.cookie = 'escaped=%u5317';
// document.cookie = 'default=%E5%8C%97';
Cookies cookies = Cookies.initFromServlet( request, response );
Cookies escapedCookies = cookies.withConverter(new Cookies.Converter() {
@Override
public String convert( String value, String name ) throws ConverterException {
ScriptEngine javascript = new ScriptEngineManager().getEngineByName( "JavaScript" );
if ( name.equals( "escaped" ) ) {
try {
return javascript.eval( "unescape('" + value + "')" ).toString();
} catch ( ScriptException e ) {
throw new ConverterException( e );
}
}
return null;
}
});
escapedCookies.get( "escaped" ); // => 北
escapedCookies.get( "default" ); // => 北
escapedCookies.get(); // => {escaped=北, default=北}
Instead of passing a converter inline, you can also create a custom strategy by implementing the ConverterStrategy interface:
class CustomConverter implements ConverterStrategy {
@Override
public String convert( String value, String name ) throws ConverterException {
return value;
}
}
Cookies cookies = Cookies.initFromServlet( request, response );
Cookies cookiesWithCustomConverter = cookies.withConverter( new CustomConverter() );
js-cookie/java-cookie có 63 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/js-cookie/java-cookie. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
js-cookie/java-cookie có những chủ đề gì?
GitHub topics của js-cookie/java-cookie: "cookies", "java". TopGit xếp repo vào nhóm Backend.
js-cookie/java-cookie còn đang phát triển không?
Commit gần nhất trên js-cookie/java-cookie là 3.9 năm trước (theo timestamp GitHub). Repo có 21 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
js-cookie/java-cookie là gì?
js-cookie/java-cookie (js-cookie/java-cookie) là dự án Java trên GitHub. Theo mô tả gốc: A simple Java API for handling cookies
js-cookie/java-cookie so với các dự án Backend khác thế nào?
js-cookie/java-cookie được TopGit xếp vào nhóm Backend, với 63 sao GitHub và viết bằng Java. Xem trang chủ đề Backend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
js-cookie/java-cookie viết bằng ngôn ngữ gì?
js-cookie/java-cookie chủ yếu viết bằng Java. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao js-cookie/java-cookie được xếp vào nhóm Backend?
TopGit xếp js-cookie/java-cookie vào nhóm Backend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "cookies", "java"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về java-cookie?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về java-cookie.