Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java equivalent for C# httpcontext

What is the Java Equivalent for HttpContext in C#? I need to convert the following to Java. This code is basically extracting a 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;
}

Code base from here: https://sisense.dev/guides/sso/jwt/#actions

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try HttpServletRequest
See Here getCookies() method

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading