将JSP映射到servlet

时间:2015-05-07 01:33:26

标签: java xml jsp servlets

尝试将jsp提交给servlet。 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>

文件夹结构

enter image description here

但是,当我尝试在服务器上运行index.jsp时,我收到"server cannot be started"错误

从web.xml中删除servlet映射时出现错误

Validate.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并且在控制台上没有任何内容。

1 个答案:

答案 0 :(得分:1)

问题是你正在使用注释映射和web.xml,所以你最好删除其中一个以使你的代码工作。我相信注释一个更好。

@WebServlet(description="validation", urlPatterns={"/Validate"})