簡體   English   中英

Java 等效於 C# HttpContext

[英]Java equivalent for C# HttpContext

什么是 C# 中的 HttpContext 等效的 Java? 我需要將以下內容轉換為 Java。 這段代碼基本上是在提取一個 cookie。

// This method returns the username from the login cookie, or null if no user is logged in.
public string ExtractUser(HttpContext context)
{
   // Get the correct cookie from the request
   var Cookie = context.Request.Cookies["dummyUser"];
 
   // Return the cookie's value if it exists
   if ((Cookie != null) && (Cookie.Value != null))
      return Cookie.Value;
 
   // Return null otherwise
   return null;
}

來自這里的代碼庫: https://sisense.dev/guides/sso/jwt/#actions

試試HttpServletRequest這里getCookies()方法

public static String getCookie(HttpServletRequest req,String name) {
  Cookie[] cookies = req.getCookies();
  if(cookies!=null) {
    for (Cookie cookie : cookies) {
      if(cookie.getName().equals(name)) {
        return cookie.getValue();
      }
    }
  }
  return null;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM