Servlet代码在不同的场景中转发到同一页面而不是另一个页面

时间:2016-10-31 15:12:39

标签: java jsp servlets

根据我的要求,主页面包含TextField和单选按钮。

场景1:当用户输入" locationno"在TextField和提交servlet中将调用DB并从数据库中获取输入locationno的记录并转发到locationDetails页面。工作正常

场景2:当用户在TextField中同时输入locationno时,单击单选按钮servlet将调用DB并获取特定locationno的所有详细信息并转发到locationAllDetails页面。这是正确获取记录但是它始终转发到locationDetails页面而不是locationAllDetails页面。 当我在日志中输入TextField和单选按钮后单击提交按钮时,它将Outputtype(单选按钮名称)显示为" ALL"但它总是转发到locationDetails页面而不是locationAllDetails页面

以下代码中的错误我怀疑是由于这一行

 request.setAttribute("locationbean", locResultsList);

请找到以下代码并提供帮助。

          if (strSearchType != null && strSearchType.equals("locationNo")){
            inLoop = "siteID : ";
            RequestDispatcher rd = null;
            String locationNo = null;

            String flow = null;
            String pagePosition = null;
            String sitePagePositon = null;
            String outputType = null;

            List locResultsList = null;



            try{
                pagePosition = request.getParameter("pagePosition");

                if(pagePosition != null){
                    request.setAttribute("pagePosition",pagePosition);
                    rd = this.getServletContext().getRequestDispatcher("/jsp/locationDetails.jsp");
                    rd.forward(request, response);
                }
else{                                   
                    flow = "locationSearchFlow";
                    locationNo = request.getParameter("locationNo");
                    session.setAttribute("locationNo",locationNo);
                    outputType = request.getParameter("outputType");

                    rd = this.getServletContext().getRequestDispatcher("/jsp/SiteSearchResults.jsp");

                    LocationCodeDAOBean locDaoBean = new LocationCodeDAOBean();

           List locResultsList = locDaoBean.getLocationResults(locationNo); 

         request.setAttribute("locationbean", locResultsList);

     if(locResultsList.size() == 0){
                            rd =     this.getServletContext().getRequestDispatcher("/jsp/ExceptionPage.jsp");
                        request.setAttribute("errorMessage","No Records Returned for Site Item Location Details, Req Number = "+siteID);
                        rd.forward(request, response);
                    }else if(outputType.equals("ALL")){

                        locationNo = request.getParameter("locationNo ");
                        session.setAttribute("locationNo ",locationNo );                        

                        LocationCodeDAOBean locDaoALLBean = new LocationCodeDAOBean();
   List locAllResultsList = locDaoALLBean .getALLLocationResults(locationNo);

   request.setAttribute("pagePosition", "0");
                        request.setAttribute("locationALLbean",    locAllResultsList);
                        session.setAttribute("flow",flow);
                        rd = this.getServletContext().getRequestDispatcher("/jsp/LocationSearchAllResults.jsp");
                        rd.forward(request, response);
      }

                    else{
                        rd = this.getServletContext().getRequestDispatcher("/jsp/ExceptionPage.jsp");
                        request.setAttribute("errorMessage","No Records Returned for Site Item Location Details, Req Number = "+siteID);
                        rd.forward(request, response);
                    }
                }       

1 个答案:

答案 0 :(得分:2)

每次forward后返回。

if (pagePosition != null) {
    request.setAttribute("pagePosition", pagePosition);
    rd = this.getServletContext().getRequestDispatcher("/jsp/locationDetails.jsp");
    rd.forward(request, response);
    return;
}