【问题标题】:Call servlet from jsp and use if从 jsp 调用 servlet 并使用 if
【发布时间】:2013-04-09 17:49:29
【问题描述】:

一段时间以来一直在尝试解决这个问题,我在 html 文件中有一个表单

      <form id="form" name="form" method="post" ACTION="First_Q.jsp" >

提交时如上所示的当前from只是转到一个jsp...

但是,我有一个表单 servlet,我想将表单数据传递到将执行 IF 以决定用户是否返回的位置。如其他示例所示,我已尝试将 ACTION 多次更改为 Game.FormServlet 和 Game/FormServlet ,但这不起作用。

Game 文件夹与 src 文件夹中的其他 servlet 一起放置。

基本上我想知道 1.如何将表单数据传递给 servlet 2.如何对 servlet 进行编程以获取 if 语句的结果并基于该移动到另一个 jsp..

设置设置 The_Quiz.RegistrationServlet.java

如其中一个答案中所建议的,会引发以下错误

给我错误

cvc-complex-type.2.4.a:发现以元素“servlet-path”开头的无效内容。 '{"http://java.sun.com/xml/ns/j2ee":servlet-class, "java.sun.com/xml/ns/j2ee":jsp-file}' 之一是预期的。

【问题讨论】:

    标签: jsp servlets tomcat6


    【解决方案1】:

    必须声明一个 servlet 并将其映射到 webapp 的 web.xml 文件中的 URL 模式。

    <servlet>
         <servlet-name>SomeUniqueName</servlet-name>
         <servlet-path>the.fully.qualified.name.of.the.ServletClass</servlet-path>
    </servlet>
    <servlet-mapping>
         <servlet-name>SomeUniqueName</servlet-name>
         <url-pattern>/foobar</url-pattern>
    </servlet-mapping>
    

    完成后,您可以使用所选的 URL 来调用 servlet:

    <form id="form" name="form" method="post" action="${pageContext.request.contextPath}/foobar" >
    

    【讨论】:

    • 酷,谢谢,尝试了几种不同的方法,但都不起作用,我现在就这样做
    • 设置The_Quiz.RegistrationServlet.java
    • 给我错误 cvc-complex-type.2.4.a:发现以元素 'servlet-path' 开头的无效内容。需要 '{"http://java.sun.com/xml/ns/j2ee":servlet-class, "java.sun.com/xml/ns/j2ee":jsp-file}' 之一。
    【解决方案2】:

    表单操作映射到 URL,而不是类。 URL 通过web.xml 或注释映射到类。

    可通过getParameter(String) method 获取表单数据。

    获得数据后,您可以根据需要对其进行操作,并使用RequestDispatcher 转发到相应的 JSP:

    ServletContext context = getServletContext();
    RequestDispatcher rd = context.getRequestDispatcher("/WEB-INF/jsp/result.jsp");
    rd.forward(request, response);
    

    任何 servlet/JSP 教程都会为您提供更多信息;我建议您先阅读一本,然后再继续深入——您会为自己节省很多时间。根据您的应用容器,您可能还想利用新的注释。

    【讨论】:

      【解决方案3】:

      我添加了一个doPost,然后清理了解决问题的解决方案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 2021-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多