本文整理汇总了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);
}
}
}
}