Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4096728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:05:02+00:00 2026-05-20T20:05:02+00:00

In ASP.net, I have the following code. I’m making a website in JSP and

  • 0

In ASP.net, I have the following code. I’m making a website in JSP and using Java classes. Basically I want to incorporate the same functionality of this constructor.

What is Java code for the following c# code?

public class ShoppingCart
{
    #region ListCart

    public List<CartItem> Items { get; private set; }

    #endregion

    #region CartSession


    public static readonly ShoppingCart Instance;

    static ShoppingCart()
    {

        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
        {
            Instance = new ShoppingCart();
            Instance.Items = new List<CartItem>();
            HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
        }
        else
        {
            Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:05:03+00:00Added an answer on May 20, 2026 at 8:05 pm

    In Java, you will have to play with HttpServletRequest or HttpSession (which is most preferred) since you’re storing a ShoppingCart into a session.

    I would not create a ShoppingCart class to store instance of it in a Session, since ShoppingCart can exists outside of Web context. A Simple way to do what you requested is to create a utility class to store / retrieve a shopping cart in/from the session.

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    public class ShoppingCartUtil {
    
        public static ShoppingCart getShoppingCart(HttpServletRequest request, String sessionName) {
            return getShoppingCart(request.getSession(), sessionName);
        }
    
        public static ShoppingCart getShoppingCart(HttpSession session, String sessionName) {
            return (ShoppingCart)session.getAttribute(sessionName);
        }
    
        public static void addShoppingCartToSession(HttpServletRequest request, String sessionName, ShoppingCart cart) {
            addShoppingCartToSession(request.getSession(), sessionName, cart);
        }
    
        public static void addShoppingCartToSession(HttpSession session, String sessionName, ShoppingCart cart) {
            session.removeAttribute(sessionName);
            session.setAttribute(sessionName, cart);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET 3.5 with iTextSharp and I have the following code: var
I am using ASP.NET and in my .aspx page I have the following code,
I have the following code in my web application asp.net using C#: bool isValid
In asp.net website, inside .aspx i have following code <script type=text/javascript> function callme(option) {
i have the following code in c#. I'm using ASP.NET MVC 3. public override
I am using JQuery mobile in asp.net web form website. I have the following
I have following code sample in ASP.NET using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain)) {
I have the following code which posts file to ASP.net page: using (var Client
I'm prototyping a chart using ASP.NET Chart Controls. I have the following code to
I have the following code for use in my asp.net website: CalendarService service =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.