在嵌入式Jetty上使用DefaultServlet提供静态html文件 [英] Serving static html files using DefaultServlet on embedded Jetty

查看:418
本文介绍了在嵌入式Jetty上使用DefaultServlet提供静态html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要自包含的项目,所以我决定在我的应用程序中嵌入Jetty。我将要提供静态HTML页面,几个JSP页面,还将使用一些自定义的servlet。我发现了一个完美的例子,说明如何设置嵌入式Jetty来完成所有这一切(http://thinking.awirtz.com/2011/11/03/embedded-jetty-servlets-and-jsp/),但是,因为这是我第一次使用Jetty并使用JSP页面或servlet,我有几个基本的问题。

I'm working on a project that needs to be self contained so I've decided to embed Jetty in my app. I'm gonna be serving static HTML pages, a few JSP pages, and also will be using some custom servlets. I've found a perfect example of how to setup the embedded Jetty to accomplish all of this (http://thinking.awirtz.com/2011/11/03/embedded-jetty-servlets-and-jsp/), however, since this is the first time I'll be using Jetty and working with JSP pages or servlets, I've got a few basic questions.

这里是代码在上面的链接):

Here's the code (as found in the link above):

class RunServer {
  public static void main(String args[]) {

    System.out.println("Initializing server...");
    final ServletContextHandler context =
      new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase("webapp");

    context.setClassLoader(
        Thread.currentThread().getContextClassLoader()
    );

    context.addServlet(DefaultServlet.class, "/");

    final ServletHolder jsp =
      context.addServlet(JspServlet.class, "*.jsp");
    jsp.setInitParameter("classpath", context.getClassPath());

    // add your own additional servlets like this:
    // context.addServlet(JSONServlet.class, "/json");

    final Server server = new Server(8080);
    server.setHandler(context);

    System.out.println("Starting server...");
    try {
        server.start();
    } catch(Exception e) {
        System.out.println("Failed to start server!");
        return;
    }

    System.out.println("Server running...");
    while(true) {
        try {
            server.join();
        } catch(InterruptedException e) {
            System.out.println("Server interrupted!");
        }
    }
  }
}

我添加了一个额外的自定义servlet,但是我的工作很棒,但是我有静态内容(即:HTML文件)的问题。尝试访问它时,我会收到找不到文件错误。我很确定是因为我不能100%肯定我应该放置我的HTML文件。我假设说context.setResourceBase(webapp);是告诉各种servlet在哪里查找文件(即:资源)的行,但是我不知道webapp实际上指向什么。是否是文件夹,类名称,实际文件?

I've added an additional custom servlet and I got that working great, however, I'm having issues with the static content (ie: HTML files). I keep getting "file not found" errors when trying to access them. I'm pretty sure that's because I'm not 100% sure where I should be placing my HTML files. I'm assuming that the line that says "context.setResourceBase("webapp");" is the line that tells the various servlets where to look for the files (ie: resources), however, I'm not sure what "webapp" actually points to. Is it a folder, a class name, an actual file?

我非常感谢任何有关webapp实际(或指向)的帮助,以及哪里把我的静态文件放在我的项目中。

I highly appreciated any help on what "webapp" actually is (or points to), and also where to put my static files in my project.

谢谢,
哈利

推荐答案

webapp是您应该创建的目录,并将静态文件放入。:)如果您有一个maven项目,那么在项目根目录中将是src / main / webapp。

webapp is the directory you should create and place your static files in. :) If you have a maven project it'll be "src/main/webapp" in the root of your project.

希望有帮助。

干杯,
托马斯

Cheers, Thomas

这篇关于在嵌入式Jetty上使用DefaultServlet提供静态html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆