當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpServletRequest.getCookies方法代碼示例

本文整理匯總了Java中javax.servlet.http.HttpServletRequest.getCookies方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletRequest.getCookies方法的具體用法?Java HttpServletRequest.getCookies怎麽用?Java HttpServletRequest.getCookies使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.servlet.http.HttpServletRequest的用法示例。


在下文中一共展示了HttpServletRequest.getCookies方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: echoCookies

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
@RequestMapping(value = "/cookies", method = {GET, POST})
public List<Cookie> echoCookies(HttpServletRequest request, HttpServletResponse response) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return Collections.emptyList();
    } else {
        String domain = request.getParameter("domain");
        for (Cookie cookie: cookies) {
            if (domain != null) {
                cookie.setDomain(domain);
            }
            response.addCookie(cookie);
        }
        return Arrays.asList(cookies);
    }
}
 
開發者ID:intuit,項目名稱:karate,代碼行數:17,代碼來源:SearchController.java

示例2: getCookieValue

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
/**
 * 得到Cookie的值,
 * 
 * @param request
 * @param cookieName
 * @return
 */
public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {
    Cookie[] cookieList = request.getCookies();
    if (cookieList == null || cookieName == null) {
        return null;
    }
    String retValue = null;
    try {
        for (int i = 0; i < cookieList.length; i++) {
            if (cookieList[i].getName().equals(cookieName)) {
                retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);
                break;
            }
        }
    } catch (UnsupportedEncodingException e) {
    	 e.printStackTrace();
    }
    return retValue;
}
 
開發者ID:xubinux,項目名稱:xbin-store,代碼行數:26,代碼來源:CookieUtils.java

示例3: service

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
    Cookie cookies[] = req.getCookies();
    for (Cookie cookie : cookies) {
        resp.getWriter().write(cookie.getName() + "=" +
                cookie.getValue() + "\n");
    }
    resp.flushBuffer();
}
 
開發者ID:sunmingshuai,項目名稱:apache-tomcat-7.0.73-with-comment,代碼行數:11,代碼來源:TestCookiesDisallowEquals.java

示例4: getCookie

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
/**
 * 獲取cookie
 * 
 * @param request
 *            HttpServletRequest
 * @param name
 *            cookie名稱
 * @return 若不存在則返回null
 */
public static String getCookie(HttpServletRequest request, String name) {
	Assert.notNull(request);
	Assert.hasText(name);
	Cookie[] cookies = request.getCookies();
	if (cookies != null) {
		try {
			name = URLEncoder.encode(name, "UTF-8");
			for (Cookie cookie : cookies) {
				if (name.equals(cookie.getName())) {
					return URLDecoder.decode(cookie.getValue(), "UTF-8");
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	return null;
}
 
開發者ID:wenjian-li,項目名稱:spring_mybatis_shiro,代碼行數:28,代碼來源:CookieUtils.java

示例5: createCookieMap

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
/**
 *
 * Creates the Map that maps cookie name to the first matching
 * Cookie in request.getCookies().
 **/
public static Map<String, Cookie> createCookieMap (PageContext pContext)
{
  // Read all the cookies and construct the entire map
  HttpServletRequest request = (HttpServletRequest) pContext.getRequest ();
  Cookie [] cookies = request.getCookies ();
  Map<String, Cookie> ret = new HashMap<String, Cookie> ();
  for (int i = 0; cookies != null && i < cookies.length; i++) {
    Cookie cookie = cookies [i];
    if (cookie != null) {
      String name = cookie.getName ();
      if (!ret.containsKey (name)) {
        ret.put (name, cookie);
      }
    }
  }
  return ret;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:23,代碼來源:ImplicitObjectELResolver.java

示例6: getCookie

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
public String getCookie(HttpServletRequest request, String key) {
    Cookie[] cookies = request.getCookies();

    if (cookies == null) {
        return null;
    }

    for (Cookie cookie : cookies) {
        if ((cookie == null) || (cookie.getName() == null)) {
            continue;
        }

        if (cookie.getName().equals(key)) {
            return cookie.getValue();
        }
    }

    return null;
}
 
開發者ID:zhaojunfei,項目名稱:lemon,代碼行數:20,代碼來源:DeviceFilter.java

示例7: removeAllCookie

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
/**
 * 銷毀所有cookie
 * 
 * @param request
 * @param response
 */
public static void removeAllCookie(HttpServletRequest request, HttpServletResponse response) {
	Cookie[] cookies = request.getCookies();
	if (null != cookies) {
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			cookie.setValue(null);
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}
}
 
開發者ID:lemon-china,項目名稱:lemon-framework,代碼行數:18,代碼來源:CookieUtil.java

示例8: doFilter

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    LOGGER.info("Checking client, request: {}", request);

    if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        if (!httpServletRequest.getServletPath().equals("/echo")) {
            chain.doFilter(request, response);
            return;
        }

        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("accessToken") && cookie.getValue().equals("top-secret-token")) {
                    LOGGER.info("Finish checking client - client is authenticated");
                    chain.doFilter(request, response);
                    return;
                }
            }
        }

        LOGGER.warn("Finish checking client - client is unauthenticated !");
        httpServletResponse.setStatus(401);
        httpServletResponse.getWriter().print("Invalid token !");
    }
}
 
開發者ID:asafalima,項目名稱:guice-websocket,代碼行數:30,代碼來源:AuthenticationFilter.java

示例9: readCookieMap

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
private static Map<String, Cookie> readCookieMap(HttpServletRequest request) {
    Map<String, Cookie> cookieMap = new HashMap<>();
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            cookieMap.put(cookie.getName(), cookie);
        }
    }
    return cookieMap;
}
 
開發者ID:ldlood,項目名稱:SpringBoot_Wechat_Sell,代碼行數:11,代碼來源:CookieUtil.java

示例10: logoutPage

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
@GetMapping("/logout")
public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null){
        new SecurityContextLogoutHandler().logout(request, response, auth);
    }
    Cookie[] cookies = request.getCookies();
    for(int i = 0,len = cookies.length; i < len; i++) {
        Cookie cookie = new Cookie(cookies[i].getName(),null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
    return "redirect:/";
}
 
開發者ID:hulvyou,項目名稱:spring-cloud-template,代碼行數:16,代碼來源:LoginController.java

示例11: getCookie

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
/**
 * Get cookie by name.
 *
 * @param request
 * @param name
 * @return returns null if not found.
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return null;
    }
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals(name)) {
            return cookie;
        }
    }
    return null;
}
 
開發者ID:febit,項目名稱:febit,代碼行數:20,代碼來源:ServletUtil.java

示例12: loginSuccess

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
@Override
public void loginSuccess(HttpServletRequest request, HttpServletResponse response,
                         Authentication successfulAuthentication) {

    User user = RepositoriesAccess.usersRepository
            .findByLogin(SecurityContextHolder.getContext().getAuthentication().getName());

    if (request.getCookies().length > 1)
        CookiesDAO.deleteCurrentCookies(request, response);

    String codeCookieWithCookie = Coder.getUniqueCode();
    String codeCookieWithUser = Coder.getUniqueCode();

    Cookies mainCookie = new Cookies(codeCookieWithCookie, codeCookieWithUser);
    Cookies rememberMeCookie = new Cookies("remember", codeCookieWithCookie);
    user.setCookieCode(codeCookieWithUser);

    Cookie c = new Cookie(mainCookie.getName(), mainCookie.getValue());
    c.setMaxAge(604800);
    response.addCookie(c);
    Cookie c1 = new Cookie("remember", rememberMeCookie.getValue());
    c1.setMaxAge(604800);
    response.addCookie(c1);

    mainCookie.setDate(CookiesDAO.getEndCookieDate(7));
    rememberMeCookie.setDate(CookiesDAO.getEndCookieDate(7));

    RepositoriesAccess.cookiesRepository.save(mainCookie);
    RepositoriesAccess.cookiesRepository.save(rememberMeCookie);
    RepositoriesAccess.usersRepository.save(user);
}
 
開發者ID:xSzymo,項目名稱:Spring-web-shop-project,代碼行數:32,代碼來源:CustomRememberMeServices.java

示例13: autoLogin

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
@Override
public Authentication autoLogin(HttpServletRequest request, HttpServletResponse response) {
    if (!CookiesDAO.checkCookiesExistsAndDeleteIfNo(request, response))
        return null;
    if (CookiesDAO.isCookieStillActualDeleteIfNo(request, response))
        return null;

    Cookies a = null;
    Cookies a1 = null;
    User user = null;

    if (request.getCookies().length < 2)
        return null;

    for (Cookie x : request.getCookies())
        if (x.getName().equals("remember")) {
            a = RepositoriesAccess.cookiesRepository.findByValue(x.getValue());
            a1 = RepositoriesAccess.cookiesRepository.findByName(x.getValue());
            user = CookiesDAO.findConnectUserWithCookie(a1);
        }

    if (a == null || a1 == null || user == null)
        return null;

    if (user.getCookieCode().equals(a1.getValue()))
        if (a1.getName().equals(a.getValue()))
            return UserDAO.login(user.getLogin(), user.getPassword());
    return null;
}
 
開發者ID:xSzymo,項目名稱:Spring-web-shop-project,代碼行數:30,代碼來源:CustomRememberMeServices.java

示例14: deleteCookiesIfDoeasntExists

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
public static void deleteCookiesIfDoeasntExists(HttpServletRequest request, HttpServletResponse response) {
    for (Cookie x : request.getCookies()) {
        if (x.getName().equals("JSESSIONID"))
            continue;
        Cookie cookie = new Cookie(x.getName(), x.getValue());
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
 
開發者ID:xSzymo,項目名稱:Spring-web-shop-project,代碼行數:10,代碼來源:CookiesDAO.java

示例15: clearCookies

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
private void clearCookies (HttpServletRequest request, 
   HttpServletResponse response)
{
   if (request.getCookies () != null)
   {
      for (Cookie c : request.getCookies ())
      {
         if (!"JSESSIONID".equals (c.getName ()))
         {
            c.setMaxAge (0);   
            response.addCookie (c);
         }
      }
   }
}
 
開發者ID:SentinelDataHub,項目名稱:dhus-core,代碼行數:16,代碼來源:PreAuthFilter.java


注:本文中的javax.servlet.http.HttpServletRequest.getCookies方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。