如果“metadata-complete”设置为false,则Tomcat不会运行

时间:2015-02-02 21:31:18

标签: java eclipse tomcat servlets metadata

我正在将Eclipse用于Java EE开发人员。我正在尝试运行这个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,它运行。但是,打印出来:

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

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

帮助!

1 个答案:

答案 0 :(得分:0)

您应该再次检查所有内容(尤其是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();
    }

}


没问题,它有效:

enter image description here