Java JSP 验证和重定向:如何验证表单输入并将错误转发回原始页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2231817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:01:53  来源:igfitidea点击:

JSP Validating and Redirecting: how to validate form input and forward the errors back to the original page?

javajspvalidation

提问by Kiril

I'm taking a class on JSP and I have an assignment... we have to write a JSP page that takes user input, validate the input and then forward it to a different web site. To be more precise, we were asked to implement a rudimentary version of the FareFinder functionality of Amtrak's web site.

我正在上 JSP 课程,我有一个作业……我们必须编写一个 JSP 页面,该页面接受用户输入,验证输入,然后将其转发到不同的网站。更准确地说,我们被要求实施 Amtrak 网站的 FareFinder 功能的基本版本。

There are 2 main purposes to this assignment:
(a) to write JSP which performs as middleware;
and (b) to write JSP which validates form data.

这个任务有两个主要目的:
(a) 编写作为中间件的 JSP;
(b) 编写验证表单数据的 JSP。

I have a general question about the principles of doing the validation. Currently I have a JSP that has a form and a submit button. When the user clicks on the submit button I forward them to Validate.jsp. The Validate.jsp will then validate the data and if the input is OK it will automatically redirect the request to the Amtrak web site with all the parameters filled out.

我有一个关于进行验证的原则的一般性问题。目前我有一个 JSP,它有一个表单和一个提交按钮。当用户单击提交按钮时,我会将它们转发到 Validate.jsp。然后 Validate.jsp 将验证数据,如果输入正常,它将自动将请求重定向到 Amtrak 网站,并填写所有参数。

FareFinder.jsp-> Validate.jsp-> Amtrak
(click on the file name to see all my code in a pastie)

FareFinder.jsp-> Validate.jsp-> Amtrak
(点击文件名查看我的所有代码)

Briefly, the main thing that I'm doing FareFinder.jsp:

简而言之,我在 FareFinder.jsp 中做的主要事情:

<FORM METHOD=POST ACTION="Validate.jsp">
    <!-- all the input fields are up here -->
    <P><INPUT TYPE=SUBMIT></P>
</FORM>

The main thing I'm doing in Validate.jsp:

我在 Validate.jsp 中做的主要事情:

<%@ page import="java.util.*" import="java.io.*"%>
<%
    // retreive all the parameters
    String origin = request.getParameter("_origin");
    String depmonthyear = request.getParameter("_depmonthyear");
    String depday = request.getParameter("_depday");
    String dephourmin = request.getParameter("_dephourmin");
    String destination = request.getParameter("_destination");
    String retmonthyear = request.getParameter("_retmonthyear");
    String retday = request.getParameter("_retday");
    String rethourmin = request.getParameter("_rethourmin");
    String adults = request.getParameter("_adults");
    String children = request.getParameter("_children");
    String infants = request.getParameter("_infants");
    String searchBy = request.getParameter("_searchBy");

    // validate the data

    // redirect to Amtrak or back to FareFinder.jsp
%>

I have several questions:
How do I return to FareFinder.jsp from Validate.jsp and reflect the errors found in the validation page?
Once I have found errors- do I redirect the response back to FareFinder.jsp?
How could I transmit the error(s) back to FareFinder.jsp?

我有几个问题:
如何从 Validate.jsp 返回到 FareFinder.jsp 并反映在验证页面中发现的错误?
一旦发现错误,我是否将响应重定向回 FareFinder.jsp?
我如何将错误传输回 FareFinder.jsp?

A generic answer would be fine too, but I'm giving my code as an example.

通用答案也可以,但我以我的代码为例。

Note: the validation must be performed on the server side and I can't use javascript.

注意:验证必须在服务器端执行,我不能使用 javascript。

采纳答案by BalusC

OK, as per the comments, Servlets are covered as well. Now, create one and implement doPost()like follows (semi pseudo):

好的,根据评论,也涵盖了 Servlet。现在,创建一个并实现doPost()如下(半伪):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Map<String, String> errors = new HashMap<String, String>();

    String origin = request.getParameter("origin");
    if (origin does not validate) {
       errors.put("origin", "Put error message here.");
    }

    // Repeat for all parameters.

    if (errors.isEmpty()) {
        // No errors, redirect to Amtrak.
        response.sendRedirect("http://amtrak.com");
    } else {
        // Put errors in request scope and forward back to JSP.
        request.setAttribute("errors", errors);
        request.getRequestDispatcher("FareFinder.jsp").forward(request, response);
    }
}

Map this servlet in web.xmlon an url-patternof /validateFareso that you can invoke it by http://example.com/context/validateFare.

将此 servlet 映射到 的web.xmlurl-pattern/validateFare以便您可以通过 调用它http://example.com/context/validateFare

Now do in JSP something like:

现在在 JSP 中做一些类似的事情:

<form action="validateFare" method="post">
    <label for="origin">Origin</label>
    <input id="origin" name="origin" value="${param.origin}">
    <span class="error">${errors.origin}</span>

    <!-- Repeat other fields here. -->
</form>

You see that the form actionalready points to the servlet. The ${param.origin}in input values will retain the submitted value by doing request.getParameter("origin")under the hoods. The ${errors.origin}will show any associated error message by roughly doing pageContext.findAttribute("errors").get("origin").

您会看到表单action已经指向 servlet。该${param.origin}输入值将做保留提交的值request.getParameter("origin")的罩下。在${errors.origin}将大致做显示任何关联的错误消息pageContext.findAttribute("errors").get("origin")

This must get you started :) Good luck.

这必须让你开始:) 祝你好运。