shjung 2 ani în urmă
părinte
comite
f2efad6367
1 a modificat fișierele cu 17 adăugiri și 3 ștergeri
  1. 17 3
      src/main/java/com/its/utils/CookieUtils.java

+ 17 - 3
src/main/java/com/its/utils/CookieUtils.java

@@ -3,11 +3,20 @@ package com.its.utils;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 
 public class CookieUtils {
 
     public static void setCookie(HttpServletResponse response, String key, String value, int maxAge, String path) {
-        Cookie cookie = new Cookie(key, value);
+        String encodeValue = value;
+        try {
+            encodeValue = URLEncoder.encode(value, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            //throw new RuntimeException(e);
+        }
+        Cookie cookie = new Cookie(key, encodeValue);
         //cookie.setMaxAge(maxAge);
         cookie.setPath(path);
         response.addCookie(cookie);
@@ -30,15 +39,20 @@ public class CookieUtils {
     }
 
     public static String getCookie(HttpServletRequest request, String key) {
+        String result = "";
         Cookie[] cookies = request.getCookies();
         if (cookies != null) {
             for(int ii = 0; ii < cookies.length; ii++) {
                 if (key.equals(cookies[ii].getName())) {
-                    return cookies[ii].getValue();
+                    try {
+                        result = URLDecoder.decode(cookies[ii].getValue(), "UTF-8");
+                    } catch (UnsupportedEncodingException e) {
+                        //throw new RuntimeException(e);
+                    }
                 }
             }
         }
-        return "";
+        return result;
     }
 
 }