gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:32:45 25 4
gpt4 key购买 nike

我正在为 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,它会运行。然而,它被打印出来:

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();
}

}


没问题,它有效:

enter image description here

关于java - 如果 "metadata-complete"设置为 false,Tomcat 不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28287142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com