在項(xiàng)目中內(nèi)嵌jetty web server

在maven中添加依賴

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.1.4.v20140401</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>9.1.4.v20140401</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-continuation</artifactId>
            <version>9.1.4.v20140401</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-jsp</artifactId>
            <version>9.1.4.v20140401</version>
        </dependency>

運(yùn)行示例代碼

public class Main {
    public static class HelloServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
            resp.getWriter().println("hello world");
        }
    }
    public static void main( String[] arg) throws Exception {
        Server server = new Server(8111);


        ServletContextHandler servletContextHandler = new ServletContextHandler();
        servletContextHandler.setContextPath("/");
        servletContextHandler.addServlet(HelloServlet.class, "/hello");

        HandlerList handlerList = new HandlerList();
        handlerList.setHandlers(new Handler[]{servletContextHandler});
        server.setHandler(handlerList);

        server.start();
        server.join();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 使用指導(dǎo) 如何添加外部依賴jar包 在Maven工程中添加依賴jar包,很簡(jiǎn)單,只要在POM文件中引入對(duì)應(yīng)的<de...
    靜默虛空閱讀 2,921評(píng)論 0 13
  • 最近在看 Gradle 相關(guān)的知識(shí),看到介紹 Gradle 的開(kāi)篇總是會(huì)拿來(lái)和 Maven 做對(duì)比,于是就好奇了解...
    zachaxy閱讀 417評(píng)論 0 2
  • 轉(zhuǎn)自:http://www.cnblogs.com/crazy-fox/archive/2012/02/09/23...
    晴天哥_王志閱讀 2,421評(píng)論 2 27
  • 云,總是瞬息萬(wàn)變 時(shí)而靜謐 時(shí)而深藏幻想 時(shí)而靈動(dòng) 時(shí)而神秘 在萬(wàn)千變化中,慢看云卷云舒。
    蘭若兮雪閱讀 127評(píng)論 0 3
  • 2019.02.23 星期六 雨 晚飯后與瓜子玩東南西北的游戲 猜到是畫(huà)一幅畫(huà) 于是與瓜子同一時(shí)間開(kāi)始分別完成一...
    空空tzy閱讀 685評(píng)論 0 0

友情鏈接更多精彩內(nèi)容