嵌入式Jetty 9不适用于@Webservlet。

huangapple go评论67阅读模式
英文:

Embedded jetty 9 doesn't work for @Webservlet

问题

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();
    }
}
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 配置,需要吗?

英文:

I'm using java 11 and embedded jetty 9 foor my javaEE application,I'm trying to use @Websevlet annotation to publish my servlet but it doesn't work i don't know why.
My start class java

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

My hello world 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( &quot;/getservlet&quot;)
public class ServletX extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
        response.setContentType(&quot;text/html&quot;);
        PrintWriter out = response.getWriter();
        out.println(&quot;&lt;h1&gt;Hi there..&lt;/h1&gt;&quot;);
    }

}

I don't have a web.xml configuration ,Should i do?

答案1

得分: 0

如果ServletX位于war文件中,也就是说它位于WEB-INF/classes/存档目录中,那么您声明的配置(特别是AnnotationConfiguration)将对WAR文件进行字节码扫描,并加载@WebServlet注释。

还要注意,WebAppContext将需要指向这个WAR文件,而您的代码示例并未这样做。

WebAppContext wacHandler = new WebAppContext();
waxHandler.setWar("/path/to/myapp.war");
// ... 更多设置

但是!如果ServletX不在WAR文件中,而是与您的内嵌Jetty Start类一起使用,则您需要暴露Servlet容器以供字节码扫描步骤扫描。

您总是可以为名为org.eclipse.jetty的记录器打开DEBUG/FINE级别的日志记录,以查看与部署和字节码扫描相关的活动。

英文:

If ServletX is in the war file, meaning it's in WEB-INF/classes/ archive directory, then the configuration you have declared (specifically the AnnotationConfiguration) will perform a bytecode scan of the WAR file and load the @WebServlet annotation.

Also note that the WebAppContext will need point to this WAR file, which your code examples do not do.

WebAppContext wacHandler = new WebAppContext();
waxHandler.setWar(&quot;/path/to/myapp.war&quot;);
// ... more setup

But! if the ServletX is not in the WAR file, but is instead housed with your embedded-jetty Start class, then you'll need to expose the servlet container to be scanned by the bytecode scanning step.

You can always turn on DEBUG/FINE level logging for the named logger org.eclipse.jetty and see the activity being performed with regards to the deployment and bytecode scanning.

huangapple
  • 本文由 发表于 2020年9月10日 00:24:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63815748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定