如何在servlet的doGet方法中从单个servlet将请求分派给不同的JSP页面

时间:2016-03-14 18:07:41

标签: jsp servlets

我有一个基于doPost中的getParameters调用多个方法的servlet。但我面临的问题是如何在doGet方法中将请求分派给不同的jsp页面。

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

    Enumeration<String> en=request.getParameterNames();
    List<String> list = new ArrayList<String>();
    while(en.hasMoreElements())
    {
        Object paramNames = en.nextElement();
        String param=(String)paramNames;
        list.add(param);

    }   
    for (String string : list) {
        if(string.equalsIgnoreCase("minPrice")|| string.equalsIgnoreCase("maxPrice")){
            String minPrice = request.getParameter("minPrice");
            String maxPrice = request.getParameter("maxPrice"); 

            request.setAttribute("minamt", minPrice);
            request.setAttribute("maxamt", maxPrice);   
            request.setAttribute("totalProducts", filter.filterByPrice(min, max).size());
            request.setAttribute("filterByPrice", filter.filterByPrice(min, max));

            if(!filter.filterByPrice(min, max).isEmpty()){
                request.getRequestDispatcher("/WEB-INF/Views/filteredPricePage.jsp").forward(request, response);
            }else{
                request.setAttribute("errorMessage", "Apologies..Products not available in this range, Try giving different range");
                request.getRequestDispatcher("/WEB-INF/Views/priceFilter.jsp").forward(request, response);
            }
        }else if (string.equalsIgnoreCase("minPromotion")|| string.equalsIgnoreCase("minPromotion")) {

            String minPromo = request.getParameter("minPromotion");
            String maxPromo = request.getParameter("maxPromotion"); 
            request.setAttribute("filterByPromotions", filter.filterByPromotion(min, max));
            request.setAttribute("totalProducts", filter.filterByPromotion(min, max).size());

            if(!filter.filterByPromotion(min, max).isEmpty()){
                request.getRequestDispatcher("/WEB-INF/Views/filteredPromotion.jsp").forward(request, response);
            }else{
                request.setAttribute("errorMessage", "Apologies..Products not available in this range, Try giving different range");
                request.getRequestDispatcher("/WEB-INF/Views/promotionFilter.jsp").forward(request, response);
            }

所以我有两个方法在这里调用,我有一个navaigation链接列表,按下它应该去不同的jsps

谢谢.. !!

1 个答案:

答案 0 :(得分:0)

抛出了什么样的错误?

您的代码看起来不错,但我认为它可能是JSP的URI:

request.getRequestDispatcher("/WEB-INF/Views/filteredPricePage.jsp").forward(request, response);

在大多数caces中,URI /WEB-INF/Views/filteredPricePage.jsp不可访问。移动您的&#34;观看&#34;文件夹&#34; web&#34;或者查看web.xml文件中为视图注册的路径。

希望有所帮助。