-2

一段时间以来一直试图为此寻找解决方案,我在 html 文件中有一个表单

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

提交时如上所示的当前来源只是转到一个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}' 之一是预期的。

4

3 回答 3

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" >
于 2013-04-09T18:22:05.290 回答
1

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

表单数据通过getParameter(String)方法在请求中可用。

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

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

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

于 2013-04-09T18:23:48.607 回答
-1

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

于 2013-04-10T10:14:15.590 回答