【问题标题】:Map JSP to servlet将 JSP 映射到 servlet
【发布时间】: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 方法实现。

标签: java xml jsp servlets


【解决方案1】:

问题是您同时使用注释映射以及web.xml,因此您最好删除其中一个以使您的代码正常工作。我相信注释一个更好。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 2016-02-05
    • 1970-01-01
    • 2011-11-09
    • 2018-05-25
    • 2011-01-18
    • 2013-09-04
    • 2015-06-03
    相关资源
    最近更新 更多