![](/img/trans.png)
[英]Java equivalent of C#'s HttpContext.Current.Items.set/get
[英]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;
}
試試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.