简体   繁体   English

无法使Jetty HelloWorld示例工作

[英]Unable to get Jetty HelloWorld example to work

Edit: It appears that this problem may be localized to the version of javax.servlet I'm using. 编辑:看来此问题可能已本地化为我正在使用的javax.servlet版本。 I got 2.5 from the site and it wasn't working, though using a different version I got from a demo project my prof published, it seems to work. 我从该站点获得了2.5,但它无法正常工作,尽管使用的是我从教授发布的演示项目获得的其他版本,但它似乎可以正常工作。 Not sure exactly which version it is though. 不确定确切是哪个版本。


Using the following code from https://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld , I seem to be getting nowhere, and I have no idea why... 使用来自https://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld的以下代码,我似乎一无所获,也不知道为什么...

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import java.io.IOException;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorld extends AbstractHandler
{
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response) 
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }

    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.setHandler(new HelloWorld());

        server.start();
        server.join();
    }
}

I've downloaded the two jar files it says to, and compiled and ran the program with the following: 我已经下载了它说的两个jar文件,并使用以下命令编译并运行了该程序:

Q:\Documents\Development\Eclipse\3715a5\src>javac -cp "..\lib\*;." HelloWorld.java
Q:\Documents\Development\Eclipse\3715a5\src>java -cp "..\lib\*;." HelloWorld

I have the following output on my standard output when I run: 运行时,我的标准输出上有以下输出:

2014-03-18 00:09:57.825:INFO::main: Logging initialized @89ms
2014-03-18 00:09:57.872:INFO:oejs.Server:main: jetty-9.1.3.v20140225
2014-03-18 00:09:57.913:INFO:oejs.ServerConnector:main: Started ServerConnector@edd9de{HTTP/1.1}{0.0.0.0:8080}
2014-03-18 00:09:57.915:INFO:oejs.Server:main: Started @182ms

But when I use any web browser to browse to http://localhost:8080 , I get nothing. 但是,当我使用任何Web浏览器浏览到http://localhost:8080 ,我什么也没得到。 I try using putty to connect to localhost:8080 via telnet, and I get "Software cause connection abort". 我尝试使用腻子通过telnet连接到localhost:8080,但出现“软件导致连接中止”。 I've disabled my windows firewall just to be certain, and I'm not getting anywhere. 可以肯定的是,我已经禁用了Windows防火墙,但是我什么也没有。

The problem appears to be related to the version of javax.servlet I was using. 该问题似乎与我使用的javax.servlet的版本有关。 I downloaded version 2.5 from the linked website, and since tried a different version (not sure which one) that was given to me by my prof for this assignment, and it is working. 我从链接的网站下载了2.5版,并且由于尝试了由我的教授给我分配的另一版本(不确定哪个版本),因此可以正常工作。

Try like this: 尝试这样:

Server serverJetty = new Server();

SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(jettyPort);
connector.setHost(jettyHost);
serverJetty.setConnectors(new Connector[] { connector });

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

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