本文整理匯總了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);
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
}
示例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 !");
}
}
示例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;
}
示例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:/";
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
}
示例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);
}
}
}
}