如果 "metadata-complete" 设置为 false,Tomcat 不会 运行

Tomcat doesn't run if "metadata-complete" is set as false

我正在为 Java EE 开发人员使用 Eclipse。我正在尝试 运行 这个 servlet:

package br.com.caelum.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@WebServlet(
          name = "OiServlet3",
          urlPatterns = {"/oi"},
          initParams = {
            @WebInitParam(name = "param1", value = "value1"),
            @WebInitParam(name = "param2", value = "value2")}
          )
    public class OiMundo extends HttpServlet {
        protected void service (HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            PrintWriter out = response.getWriter();

        // escreve o texto
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Primeira Servlet</title>");
        out.println("</head>");
        out.println("<body>");

        ServletConfig config = getServletConfig();

        String parameter1= config.getInitParameter("param1");
        out.println("Value of parameter 1: " + parameter1);

        String parameter2 = config.getInitParameter("param2");
        out.println("<br>Value of parameter 2: " + parameter2);

        out.println("<h1>Hi Servlet!</h1>");
        out.println("</body>");
        out.println("</html>");

        out.close();


    }
}   

如果我在 web.xml 文件中设置 metadata-complete="false"(或不设置),我在尝试启动时遇到此错误 Tomcat:

'Publishing to Tomcat v8.0 at Locahost...' has  encountered a problem.
Publishing the configuration...

我把它设为true,它运行s。然而,它被打印出来:

value of parameter 1: null
value of parameter 2: null

因为注释被忽略了(当您将 metada-complete 设置为 true 时会发生这种情况)。

求助!

您应该再次检查所有内容(尤其是 web.xml)。空 metadata-complete,我 运行 你的代码:

package br.com.caelum.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
        name = "OiServlet3",
        urlPatterns = {"/oi"},
        initParams = {
                @WebInitParam(name = "param1", value = "value1"),
                @WebInitParam(name = "param2", value = "value2")}
)
public class OiMundo extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        // escreve o texto
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Primeira Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        ServletConfig config = getServletConfig();
        String parameter1 = config.getInitParameter("param1");
        out.println("Value of parameter 1: " + parameter1);
        String parameter2 = config.getInitParameter("param2");
        out.println("<br>Value of parameter 2: " + parameter2);
        out.println("<h1>Hi Servlet!</h1>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }

}


没问题,有效: