提供多个Web服务URL

时间:2015-01-21 10:48:27

标签: undertow

我在地址上想要这样的东西。 我怎样才能将servlet1,servlet2,servlet3绑定到localhost:8080所以我可以有不同的url来调用,如

本地主机:8080 / servlet的

本地主机:8080 / servlet1

本地主机:8080 / servlet2

本地主机:8080 / servlet3

我有这个代码只适用于最后一个url(servlet)添加

DeploymentInfo servletBuilder = deployment()
                .setClassLoader(ServletServer.class.getClassLoader())
                .setContextPath(MYAPP)
                .setDeploymentName("test.war")
                .addServlets(
                        servlet(servletName,  x)
                        .addInitParam("message", output)
                        .addMapping("/"+servletName.toLowerCase()));

        DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
        manager.deploy();

        HttpHandler servletHandler = manager.start();
        path = Handlers.path(Handlers.redirect(MYAPP))
                .addPrefixPath(MYAPP, servletHandler);

2 个答案:

答案 0 :(得分:0)

DeploymentInfo servletBuilder = deployment()
.addServlets(
 servlet("servlet1", YourServlet1.class).addInitParam("param1", "Value A")
.addMapping("/servlet1/*"),
 servlet("servlet2", YourServlet2.class).addInitParam("param2", "Value B")
.addMapping("/servlet2/*"),
 servlet("servlet3", YourServlet3.class).addInitParam("param2", "Value C")
.addMapping("/servlet3/*")  // as many as you like.
);

使用此处的信息: https://github.com/undertow-io/undertow/tree/master/examples/src/main/java/io/undertow/examples/servlet

答案 1 :(得分:0)

从Undertow 1.2.5示例代码修改:

来自此网址的 Runnable Project将于2015年6月10日到期: https://www.hightail.com/download/bXBaTXRRaFJQb0xIRHRVag

package servlet;

import javax.servlet.ServletException;

import io.undertow.Handlers;
import io.undertow.Undertow;

import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

import static io.undertow.servlet.Servlets.defaultContainer;
import static io.undertow.servlet.Servlets.deployment;
import static io.undertow.servlet.Servlets.servlet;

/**
 * @author Stuart Douglas
 */
public class ServletServer {

    public static final String MYAPP = "/myapp";

    public static void main(final String[] args) {
        try {

            DeploymentInfo servletBuilder = deployment()
                    .setClassLoader(ServletServer.class.getClassLoader())
                    .setContextPath(MYAPP)
                    .setDeploymentName("doesnotmatter.war")
                    .addServlets(
                            servlet("MessageServletA", MessageServletA.class)
                            .addInitParam("message", "Hello AAA\n")
                            .addMapping("/msgA/*"),
                            servlet("MessageServletB", MessageServletB.class)
                            .addInitParam("message", "Hello BBB\n")
                            .addMapping("/msgB/*"),
                            servlet("MessageServletC", MessageServletC.class)
                            .addInitParam("message", "Hello CCC\n")
                            .addMapping("/msgC/*")               
                    );

            DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
            manager.deploy();

            HttpHandler servletHandler = manager.start();
            PathHandler path = Handlers.path(Handlers.redirect(MYAPP))
                    .addPrefixPath(MYAPP, servletHandler);
            Undertow server = Undertow.builder()
                    .addHttpListener(8080, "localhost")
                    .setHandler(path)
                    .build();
            server.start();
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }
}

请使用shell中的此命令进行测试:

# Runs in background
java -cp target/undertowservlet-jar-with-dependencies.jar servlet.Servletrver &

curl http://localhost:8080/myapp/msgA/
curl http://localhost:8080/myapp/msgB/
curl http://localhost:8080/myapp/msgC/