CirezD February 2016

How to populate jsp page dynamically?

Im working on website, using servlets and jsp.

I have jsp page with different categories and when i click on one category, servlet redirects on another jsp page with components (Desktops, laptops, tablets...) but there is all categories on that page, i need to do, if i click on (desktops) category to show only desktops (components) in jsp page.

this is my jsp page and servlet for components:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Collection<Components> components;
    components= componentsdao.getAll() -- geting all components from database

    request.setAttribute("components", komponenta);
    request.getRequestDispatcher("pcitem.jsp").forward(request, response);
}

and this is part of jsp page

           <c:forEach var="i" items="${requestScope.components}">         
                    <p>
                     ${i.nameofcomponent}
                    </p>
                    <div >
                    <h6>Description ${i.descriptionoofcomponent}</h6>
                    </div>
                    <div ">
                    <h6>Price ${i.priceofcomponent}</h6>
                    </div>
                    <div >
                    <h6>Official site ${i.link}</h6>
                    </div>
        </c:forEach>  

Answers


Sheetal Mohan Sharma February 2016

You have multiple options but your best bet will be to use Ajax framework e.g JQuery. You can make ajax request and pull the data from server and use java script to render that data on the view. Define event on drop down change -> Ajax call to get next drop down data-> next call. This will ensure your page does not refresh and UX is better.

Otherwise go old fashioned way - get all the data ( all drop down data) on page load - use JS arrays to define arrays and get data on event change.


Sudhakar February 2016

When you click on the categories the servlet may returns to new page. But you can do this by following way in the jsp file.

  1. Have the categories type and set from the Servlet and if you click on desktops then set as shown below. eg:

    request.setAttribute("categoryType", "desktops");

  2. In the jsp file before looping thorough the elements you can check the 'categoryType' and displays the items. Eg:

    if("desktops".equals(request.getAttribute("categoryType"))){ // display items. }

or

You can have a variable in Component class like 'componentType' in the for loop you can check based on this also.

Post Status

Asked in February 2016
Viewed 3,506 times
Voted 9
Answered 2 times

Search




Leave an answer