Home > OS >  Is it possible to use one Servlet to dispatch data to multiple JSP's using Request Dispatcher?
Is it possible to use one Servlet to dispatch data to multiple JSP's using Request Dispatcher?

Time:11-14

So I've got an "online shop" set up using Java where I have one .jsp 'Products Page' which shows 8 different products. The user can click on any of those products and be brought to another .jsp page for that individual product. I have a servlet set up like this:

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get data from data class
        ArrayList<Product> products = ProductData.getProduct();
       
        //add products to request object
        request.setAttribute("product-list", products);
        
        //get requesr dispatcher
        RequestDispatcher dispatcher = request.getRequestDispatcher("ProductPage.jsp");
        
        //forward to jsp
        dispatcher.forward(request, response);
        
    }

It works so far with me being able to see the products on the 'Product Page' but I'm now trying to use the same servlet to send the same info to each of the 8 individual product pages as well. Is this possible ? Or do I need to create 8 individual servlets too for each jsp page?

I know it would be possible to use a session but to save time explaining why, just assume for this project that I can't use a session.

Thanks!

CodePudding user response:

You don't need eight servlets. One ProductServlet should be enough. You just need to have some logic in your servlet to go to one JSP or to another.

You can add links to each product in your ProductPage.jsp that maybe link to the address http://your.domain/yourServletMapping?productId=1, or http://your.domain/yourServletMapping?productId=2, and so on. Then in your servlet you just need to see if the request contains a productId parameter and then you know you have to display a detail page for the product. Your code could look like:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
    if (request.getParameter("productId") != null) {
        // use the value from request.getParameter("productId") to get the ID of the product
        int id = ...
        
        // get the data from the data class for this product
        Product product = ...
        
        //add products to request object
        request.setAttribute("product-details", product);
        
        //get request dispatcher
        request.getRequestDispatcher("ProductDetail.jsp").forward(request, response);
        
    } else {
        // we are not looking for a particular product, so list all products
        
        // get data from data class
        ArrayList<Product> products = ProductData.getProduct();
       
        //add products to request object
        request.setAttribute("product-list", products);
        
        //get request dispatcher
        request.getRequestDispatcher("ProductPage.jsp").forward(request, response);
    }
}

You basically need to differentiate between the requests based on the presence or absence of certain parameters and then do the corresponding logic. It's just like you differentiate between the type of request (GET vs POST) by having two methods in your servlet to handle them.

You can also have different URLs to handle each situation, like http://your.domain/products to list the products and http://your.domain/products/{productId} that you can map to the same servlet and extract the path parameter from there, instead of a request parameter.

You can also have multiple servlets if things get more complicated, but considering the question you asked is simple, just try to keep things simple.

  • Related