简体   繁体   English

Jetty Servlet网址和端口

[英]Jetty servlet url and port

What is the method call to figure out what is the url and port a jetty servlet is running on? 调用Jetty Servlet的URL和端口是什么的方法调用是什么? So that I can just print it on the screen, and use it in the client to connect: 这样我就可以在屏幕上打印它,并在客户端中使用它进行连接:

url = new URL("trying to figure this out");

I am running both the client and the server locally, in eclipse, this is a school project. 我在本地运行客户端和服务器,日食,这是一个学校项目。

Relevant code: 相关代码:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class ServerMain {

    public static void main(String[] args){
        Server server = new Server();
        ServletContextHandler context = 
                new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("Servlet");

        context.setResourceBase("etc/docroot");

        server.setHandler(context);
        context.addServlet(new ServletHolder(new MyServer()), "/game");
        DefaultServlet staticFileServlet = new DefaultServlet();
        context.addServlet(new ServletHolder(staticFileServlet), "/*");
        System.out.println("context: " +  context.getContextPath());
        //System.out.println("One valid Port = "
                  + context.getServer().getConnectors()[0].getPort());
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Since you are doing this embedded you have a couple of options. 由于您正在执行此嵌入式操作,因此有两种选择。

Server server = new Server(8080); 服务器服务器=新服务器(8080);

This will start the server on port 8080, which is fine if that is what you want but in unit tests you generally want this to be a random port to avoid issues with things like CI's or parallel tests tripping on each other. 这将在端口8080上启动服务器,如果这是您想要的端口,那很好,但是在单元测试中,您通常希望它是一个随机端口,以避免诸如CI或并行测试相互跳闸之类的问题。

Server server = new Server(0); 服务器服务器=新服务器(0);

This will start the connector on a random port, but how to get that port? 这将在随机端口上启动连接器,但是如何获得该端口?

server.getConnectors()[0].getLocalPort(); server.getConnectors()[0] .getLocalPort();

The port is really coming from the connector, and typically you only are setting up one connector here, so this gets you that port. 该端口实际上来自连接器,通常您仅在此处设置一个连接器,因此可以使用该端口。

Now localhost works for testing well, but if you wanted the name of the host that the connector is on you can use this: 现在,localhost可以很好地进行测试,但是如果您想要连接器所在的主机的名称,则可以使用以下命令:

server.getConnectors()[0].getHost(); server.getConnectors()[0] .getHost();

Chain that stuff up and you get how we do most of our unit testing in jetty itself, spinning up the server itself, wiring up whatever handler or webapps we want and then assert behaviors, requests, responses, etc. 将这些东西链接起来,您将了解到我们如何在码头本身中完成大部分单元测试,旋转服务器本身,连接所需的任何处理程序或Web应用程序,然后断言行为,请求,响应等。

We have a number of embedded examples here that you can look at for different ways to wire up jetty inside code, and the jetty.xml format is just a thin layer of xml over java so you can map the startup of jetty easily through the code by reading the xml file with a java hat on. 我们这里有许多嵌入式示例,您可以查看以各种方式在代码内连接jetty,并且jetty.xml格式只是java上xml的一薄层,因此您可以通过代码轻松地映射jetty的启动。通过戴着Java帽子阅读xml文件。 There is an embedded example in here for bootstrapping jetty based on that xml format as well if you want to keep the config there. 如果您想将配置保留在那里,这里还有一个嵌入式示例,用于基于该xml格式引导码头。

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded

For a good page on embedding jetty, look here: http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html 有关嵌入码头的好页面,请访问此处: http : //www.eclipse.org/jetty/documentation/current/embedding-jetty.html

Most likely the jetty server is running on 8080 as default. 默认情况下,码头服务器最有可能在8080上运行。 If not you can go and check the server.xml file which should tell you what the port configuration is. 如果不是,则可以检查server.xml文件,该文件应告诉您端口配置是什么。

I'm not sure what you're asking here but if you had some servlet ie Hello : 我不确定您在这里要问什么,但是您是否有一些servlet,例如Hello:

public final class Hello extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws IOException, ServletException {

        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();        
        writer.println("URL is " + request.getRequestURL() + " port is " request.getServerPort());
    }
} 

Additionally if this is not info what you're looking for please see this : 此外,如果这不是您要查找的信息,请参见:

http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/HttpServletRequest.html http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/HttpServletRequest.html

http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/servlet/http/HttpServletRequest.html http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/servlet/http/HttpServletRequest.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM