嵌入式码头 9 不适用于@Webservlet
Embedded jetty 9 doesn't work for @Webservlet
我在我的 javaEE 应用程序中使用 java 11 和嵌入式码头 9,我正在尝试使用 @Websevlet 注释来发布我的 servlet,但它不起作用,我不知道不知道为什么。
我的开始classjava
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.*;
public class Start {
public static void main(String[] args) throws Exception {
Server server = new Server(80);
WebAppContext wacHandler = new WebAppContext();
wacHandler.setConfigurations(new Configuration[]
{
new AnnotationConfiguration(),
new WebInfConfiguration(),
new WebXmlConfiguration(),
new MetaInfConfiguration(),
new FragmentConfiguration(),
new JettyWebXmlConfiguration()
});
server.setHandler(wacHandler);
server.start();
server.join();
}
}
我的你好世界class
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;
@WebServlet( "/getservlet")
public class ServletX extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hi there..</h1>");
}
}
我没有 web.xml 配置,我应该这样做吗?
如果 ServletX
在 war 文件中,意味着它在 WEB-INF/classes/
存档目录中,那么您声明的配置(特别是 AnnotationConfiguration
)将执行WAR 文件的字节码扫描并加载 @WebServlet
注释。
另请注意,WebAppContext
需要指向此 WAR 文件,您的代码示例不会这样做。
WebAppContext wacHandler = new WebAppContext();
waxHandler.setWar("/path/to/myapp.war");
// ... more setup
但是!如果 ServletX
不在 WAR 文件中,而是包含在 embedded-jetty Start
class 中,那么您需要公开 servlet 容器由字节码扫描步骤扫描。
您始终可以为命名记录器 org.eclipse.jetty
打开 DEBUG/FINE 级别的日志记录,并查看正在执行的关于部署和字节码扫描的 activity。
我在我的 javaEE 应用程序中使用 java 11 和嵌入式码头 9,我正在尝试使用 @Websevlet 注释来发布我的 servlet,但它不起作用,我不知道不知道为什么。 我的开始classjava
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.*;
public class Start {
public static void main(String[] args) throws Exception {
Server server = new Server(80);
WebAppContext wacHandler = new WebAppContext();
wacHandler.setConfigurations(new Configuration[]
{
new AnnotationConfiguration(),
new WebInfConfiguration(),
new WebXmlConfiguration(),
new MetaInfConfiguration(),
new FragmentConfiguration(),
new JettyWebXmlConfiguration()
});
server.setHandler(wacHandler);
server.start();
server.join();
}
}
我的你好世界class
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;
@WebServlet( "/getservlet")
public class ServletX extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hi there..</h1>");
}
}
我没有 web.xml 配置,我应该这样做吗?
如果 ServletX
在 war 文件中,意味着它在 WEB-INF/classes/
存档目录中,那么您声明的配置(特别是 AnnotationConfiguration
)将执行WAR 文件的字节码扫描并加载 @WebServlet
注释。
另请注意,WebAppContext
需要指向此 WAR 文件,您的代码示例不会这样做。
WebAppContext wacHandler = new WebAppContext();
waxHandler.setWar("/path/to/myapp.war");
// ... more setup
但是!如果 ServletX
不在 WAR 文件中,而是包含在 embedded-jetty Start
class 中,那么您需要公开 servlet 容器由字节码扫描步骤扫描。
您始终可以为命名记录器 org.eclipse.jetty
打开 DEBUG/FINE 级别的日志记录,并查看正在执行的关于部署和字节码扫描的 activity。