English 中文(简体)
Embedded jetty ServletTester serving single static file
原标题:

I m unit testing with jetty and I want to serve not only my servlet under test but a static page as well. The static page is needed by my application. I m initializing jetty like this

tester = new ServletTester();
tester.setContextPath("/context");
tester.addServlet(MyServlet.class, "/servlet/*");
tester.start();

What I need now, is something like

tester.addStaticPage("local/path/in/my/workspace", "/as/remote/file");

Is this possible with jetty?

最佳回答

I don t think you can do this with ServletTester. ServletTester creates a single Context for the servlet. You need to set up embedded jetty with at least two contexts: one for the servlet, and one for the static content.

If there was a full WebAppContext, you d be set, but there isn t.

You could make a copy of ServletTester and add hair, or you can just read up on the API and configure the necessary contexts. Here s a code fragment to show you the basic idea, you will not be able to compile this as-is. You will need to create a suitable context for the static content.

        server = new Server();

        int port = Integer.parseInt(portNumber);
        if (connector == null) {
            connector = createConnector(port);
        }
        server.addConnector(connector);

        for (Webapp webapp : webapps) {
            File sourceDirFile = new File(webapp.getWebappSourceDirectory());
            WebAppContext wac = new WebAppContext(sourceDirFile.getCanonicalPath(), webapp.getContextPath());
            WebAppClassLoader loader = new WebAppClassLoader(wac);
            if (webapp.getLibDirectory() != null) {
                Resource r = Resource.newResource(webapp.getLibDirectory());
                loader.addJars(r);
            }
            if (webapp.getClasspathEntries() != null) {
                for (String dir : webapp.getClasspathEntries()) {
                    loader.addClassPath(dir);
                }
            }
            wac.setClassLoader(loader);
            server.addHandler(wac);
        }
        server.start();
问题回答

Set the resource base to the directory containing your static content, and add the jetty "default servlet" to serve that content. I have added the appropriate code to your example below.

tester = new ServletTester();
tester.setContextPath("/context");
tester.setResourceBase("/path/to/your/content");
tester.addServlet(MyServlet.class, "/servlet/*");
tester.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/*");
tester.start();




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签