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 7723411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:26:11+00:00 2026-06-01T04:26:11+00:00

I am learning Servlets/JSP/MVC and have a question about architecting a simple application. I

  • 0

I am learning Servlets/JSP/MVC and have a question about architecting a simple application. I have a site where people can vote on products.

  • In the Application Context I have an ArrayList of the Products (model class is called Product)

  • I have a home Servlet that draws all the Products (reads list from context, forwards as attribute to JSP). The items are drawn out using JSTL.

  • I have another servlet called ProductRate that can be called when viewing a Product. On doPost() is supposed to record the rating the user gives to a product (specified in a form) and saves this in Session.

I want to be able to join the information on the Home servlet to show not only the product information (stored in Application Context), but also any vote that the user as given to it. If there is no value, then I don’t show any thing saying “Thanks for your vote of X” or similar.

Should I add a field to my Product model class called “userRating” or something, and when reading the list on the Home Servlet, look at any session values stored for it and assign the vote value to the “userRating” Model value?

So in effect, I have a fixed list of objects the application always uses, but when drawing them out in a JSP, I want a nice clean way to marry any information stored in session (vote value) so handling the Model in the JSP is a breeze

  • 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-06-01T04:26:12+00:00Added an answer on June 1, 2026 at 4:26 am

    You could store the rankings in a map in the session with the product ID as the key and then the jsp would look something like:

    <c:forEach items="${products}" var="product">
      <h2>${product.name}</h2>
      ${product.description} <br/>
      <c:choose>
          <c:when test="${not empty ratings[product.id]}">
              ${ratings[product.id].stars} <br/>
              ${ratings[product.id].comments} <br/>
          </c:when>
          <c:otherwise>
              You have not rated this product
          </c:otherwise>
      </c:choose>
    </c:forEach>
    

    In your POST servlet/controller:

    Map<Long,Rating> ratings = new HashMap<Long,Rating>();
    ratings.put(product.getId(),new Rating(1,"I think it's really bad.."));
    session.setAttribute("ratings",ratings);
    

    If you are interested in keeping the users’ ratings or all for the products to be managed by the web app, I would use a database rather than store data in config files and the session. Consider the following database tables:

    Product: id, description, etc..
    Rating: id, stars, comments, productId, userId
    User: id, name, email, etc…

    If you go with a database, use an ORM tool, like Hibernate to map your tables to objects. Your objects corresponding to the tables listed above would look like:

    public class Product {
        Long id;
        String description;
        Set<Rating> ratings;
    }
    public class Rating{
        Long id;
        Integer stars;
        String comments;
        User user;
        Product product;
    }
    public class User {
        ...
    }
    

    Then with hibernate, you would query for the product and get all of the information associated with it in object form for you to work with. You could pass the necessary info on to the view. Of course there is some configuration you would have to do to set hibernate up.

    You mentioned MVC. Are you using an MVC framework? I would look into Spring MVC or Struts. That will help you separate the layers of your app.

    The flow you mentioned seems right. Servlet/controller handles GET request to main page. Fetches all products (or specifics) along with their votes from the database. Sets them as parameters. JSP displays content using JSTL.

    Have fun!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am training in web developement and am learning about JSP & Servlets .
i have some knowledge of css,jQuery,Grails,django,servlets and jsp still i can't see me designing
I'm working on a small Servlets & JSP application while learning web development. I
I am learning Servlets and JSP. I am wondering about the doGet and other
I'm currently learning Java EE, or more specifically, Servlets and .jsp. In my programming,
I just starting learning about servlets yersterday so I'm a newbie. I read a
I have just started learning java and i have about 8 months time. My
I have a decent working web application (Java/Servlet/Jsp) that I would like to improve
I have inherited a Java application (servlets) that runs under Tomcat. For historical reasons,
I am trying to create a simple application using SpringMVC for learning purpose. I

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.