Java 如何检查 tomcat 服务器是否正在侦听特定端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30796658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:11:48  来源:igfitidea点击:

How to check whether the tomcat server is listening on particular port?

javatomcatservletsintellij-idea

提问by Avinash Raj

I'm trying to run a servlet program with tomcat as server. But the tomcat server is not running. I looked at the services.msc but it's not there. Below is the log created while running the servlet program via intelj

我正在尝试使用 tomcat 作为服务器运行 servlet 程序。但是tomcat服务器没有运行。我查看了 services.msc,但它不在那里。下面是通过intelj运行servlet程序时创建的日志

"E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23\bin\catalina.bat" run
[2015-06-12 11:15:57,837] Artifact servlet:war exploded: Server is not connected. Deploy is not available.
Using CATALINA_BASE:   "C:\Users\Van\.IntelliJIdea14\system\tomcat\Tomcat_8_0_23_servlet"
Using CATALINA_HOME:   "E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23"
Using CATALINA_TMPDIR: "E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk1.8.0_45"
Using CLASSPATH:       "E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23\bin\bootstrap.jar;E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23\bin\tomcat-juli.jar"
12-Jun-2015 11:15:59.582 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.23
12-Jun-2015 11:15:59.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          May 19 2015 14:58:38 UTC
12-Jun-2015 11:15:59.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.23.0
12-Jun-2015 11:15:59.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 8
12-Jun-2015 11:15:59.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            6.2
12-Jun-2015 11:15:59.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
12-Jun-2015 11:15:59.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Java\jdk1.8.0_45\jre
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_45-b15
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\Van\.IntelliJIdea14\system\tomcat\Tomcat_8_0_23_servlet
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
12-Jun-2015 11:15:59.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\Van\.IntelliJIdea14\system\tomcat\Tomcat_8_0_23_servlet\conf\logging.properties
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23\endorsed
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\Vanuston\.IntelliJIdea14\system\tomcat\Tomcat_8_0_23_servlet
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=E:\Avinash\Avinash\Inteljidea projects\apache-tomcat-8.0.23\temp
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.1.33 using APR version 1.5.1.
12-Jun-2015 11:15:59.586 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
12-Jun-2015 11:16:00.718 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.1m 19 Mar 2015)
12-Jun-2015 11:16:01.278 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
12-Jun-2015 11:16:01.292 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
12-Jun-2015 11:16:01.293 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 2252 ms
12-Jun-2015 11:16:01.348 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
12-Jun-2015 11:16:01.349 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.23
12-Jun-2015 11:16:01.355 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
12-Jun-2015 11:16:01.362 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
12-Jun-2015 11:16:01.363 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 69 ms
Connected to server
[2015-06-12 11:16:01,503] Artifact servlet:war exploded: Artifact is being deployed, please wait...
[2015-06-12 11:16:02,178] Artifact servlet:war exploded: Artifact is deployed successfully
[2015-06-12 11:16:02,178] Artifact servlet:war exploded: Deploy took 675 milliseconds

It actually opened the webpage but fails to print the message hello world. I think tomcat is not running on the specified port 8080.

它实际上打开了网页,但无法打印消息hello world。我认为 tomcat 没有在指定的端口上运行8080

web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>servlet</servlet-name>
        <servlet-class>Servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/myservlet</url-pattern>
    </servlet-mapping>

</web-app>

Update:

更新:

localhost:8080/myservletdisplays the hello worldpage but we need to enter the address manually. But upon running servlet project directly from intelj, it opens localhost:8080page which gives an empty page.

localhost:8080/myservlet显示hello world页面,但我们需要手动输入地址。但是在直接从 intelj 运行 servlet 项目时,它会打开一个localhost:8080页面,该页面给出了一个空页面。

code:

代码:

@WebServlet(name = "Servlet")
public class Servlet extends HttpServlet {
    private String message;
    private String mes;
    public void init() throws ServletException
    {
        // Do required initialization
        message = "Hello World.";
        mes = "Welcome to our new Page..";
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html");

        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>" + message + "</h1></br>" + "<h2>" + mes + "</h2>");
    }
    public void destroy()
    {
        // do nothing.
    }
}

回答by vinayc

Check your server.xmlfile for Connector definition. That should tell you what port your server is listening on.

检查您的server.xml文件以获取连接器定义。这应该告诉您服务器正在侦听的端口。

Another way to find out is using TCPView. This tool will list out the TCP ports for a given process.

找出答案的另一种方法是使用TCPView。此工具将列出给定进程的 TCP 端口。

回答by Thisara Alawala

  1. Seems the application has deployed successfully. Try to access the application using the machine name and the port.
  1. 似乎应用程序已成功部署。尝试使用机器名称和端口访问应用程序。

E.g.:- < pc-name >:8080

例如:- < pc-name >:8080

  1. Else check in the tomcat server configuration connector details in;

    /conf/server.xml

  1. 否则检查 tomcat 服务器配置连接器的详细信息;

    /conf/server.xml

In the server xml look for the connector details as below;

在服务器 xml 中查找连接器详细信息,如下所示;

<Connector port="8080" address="localhost"
           maxHttpHeaderSize="8192" maxPostSize="262144"
           maxThreads="250" minSpareThreads="25"
           enableLookups="false" redirectPort="8443" acceptCount="100"
           connectionTimeout="60000" disableUploadTimeout="true" />

There should be a connector which allows your browser listen to port 8080 and address to access the server.

应该有一个连接器,允许您的浏览器侦听端口 8080 和地址以访问服务器。

Please try with above details.

请尝试上述详细信息。