【发布时间】:2015-07-17 09:34:42
【问题描述】:
试图向 servlet 提交一个 jsp。 web.xml 映射时出错。
我有
index.jsp
<form method="POST" action="Validate">
<input type="submit" value="Submit" />
</form>
web.xml
<servlet>
<servlet-name>validate</servlet-name>
<servlet-class>com.test.Validate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validate</servlet-name>
<url-pattern>/Validate</url-pattern>
</servlet-mapping>
文件夹结构
但是,当我尝试在服务器上运行 index.jsp 时,我收到 "server cannot be started" 错误
从 web.xml 中删除 servlet 映射时出错
验证.java
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Validate
*/
@WebServlet(description="validation", urlPatterns={"/Validate"})
public class Validate extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Validate() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
response.setContentType("text/html");
System.out.println("validate called");
}
当我在 Validate.java 上作为服务器运行时它可以工作
但是,当我在index.jsp 上作为服务器运行并单击提交时。它重定向
到http://localhost:8080/TestApp/Validate,控制台上什么也没有。
【问题讨论】:
-
可以分享
Validate.java的代码吗? -
您在点击提交时正在执行 HTTP POST,但您的 servlet 中没有 doPost 方法实现。