request.getRequestDispatcher(“URL”)。forward(request,response)Not Working

时间:2014-04-08 09:43:07

标签: jsp servlets

我在尝试在数据篡改时发现输入验证错误时重定向到jsp。当我更改其中一个字段的值时,它可以正常工作,但是当变量metricId [i]更改为“”时,它会执行request.getRequestDispatcher行的代码,但不会将其重定向到所需的页面。请帮忙。

代码如下:

    public void validateGenerateCustomReportCommand(HttpServletRequest request,
        HttpServletResponse response,
        CustomReportCommand customReportCommand) {
    try {
        boolean invalidInputFound = false;
        System.out.println("Inside validateGenerateCustomReportCommand");
        String[] metricIds = customReportCommand.getMetricIds();

        String[] attrIdValueId = customReportCommand.getAttrValueIds();

        for (int i = 0; i < attrIdValueId.length; i++) {
            if (!(regexMatcher.validateSearch(attrIdValueId[i]))) {
                invalidInputFound = true;
                logger.error("Invalid input for Attribute Set");
            }
        }

        for (int i = 0; i < metricIds.length; i++) {

            if(metricIds[i] == null){
                invalidInputFound = true;
            }

            if(metricIds[i] == ""){
                request.getRequestDispatcher("/pages/moncoeError.jsp").forward(
                        request, response);
            }

            if (!(regexMatcher.validateMetricId(metricIds[i]))) {
                invalidInputFound = true;
            }

        }

        if (customReportCommand.getUnitValue() != null
                && customReportCommand.getUnitValue() != "") {
            Long unitValue = Long.parseLong(customReportCommand
                    .getUnitValue());
            if (unitValue < 1 || unitValue > 365) {
                invalidInputFound = true;
            }
        } 

        if (customReportCommand.getFromDate() != null
                && customReportCommand.getToDate() != null
                && customReportCommand.getFromDate() != ""
                && customReportCommand.getToDate() != "") {

            if (!(regexMatcher.validateDate(customReportCommand
                    .getFromDate()) || (!(regexMatcher
                    .validateDate(customReportCommand.getToDate()))))) {
                invalidInputFound = true;
            }

        }

        if(customReportCommand.getUnit() != "Days"){
            invalidInputFound = true;
        }

        if(customReportCommand.getName()==null || customReportCommand.getName() == ""){
            invalidInputFound = true;
        }else if(customReportCommand.getName().length() > 250){
            invalidInputFound = true;

        }

        if(customReportCommand.getDescription()==null || customReportCommand.getDescription() == ""){
            invalidInputFound = true;
        }else if(customReportCommand.getDescription().length() > 500){
            invalidInputFound = true;

        }





        if (invalidInputFound) {
            System.out.println("Invalid input found");
            request.getRequestDispatcher("/pages/moncoeError.jsp").forward(
                    request, response);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

改变你的状况

    if(customReportCommand.getUnit() != "Days"){
        invalidInputFound = true;
    }

    if(customReportCommand.getUnit().equals("Days")){
        invalidInputFound = true;
    }