2

我是码头的新手。我创建了一个应用程序,在其中嵌入了 jetty Web 容器。当我从 Eclipse 运行应用程序时,它运行完美,没有任何问题。但是,当我使用所有必需的库导出项目并从命令行运行它时,我无法像以前在 eclispe 中那样访问 index.jsp 网页。这是运行 jetty web 容器的文件。

public class JettyServer {

// The folder containing all the .jsp files
private final static String WEB_ROOT = "src/WebContent";

// Instance of the Jetty server
private final static Server SRV = new Server();

// Context Path
private final static String CONTEXT_PATH = "/smpp";

// Logging 
private final static org.slf4j.Logger logger = LoggerFactory.getLogger(JettyServer.class);

/**
 * @param args
 * @throws ConfigurationException 
 */
public static void main(String[] args) throws ConfigurationException {
    logger.info("Initializing Web Server......");

    // Servlet Context
    final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

    // Set the security constraints
    context.setContextPath(CONTEXT_PATH);
    context.setResourceBase(WEB_ROOT);

    context.setClassLoader(Thread.currentThread().getContextClassLoader());

    context.addServlet(DefaultServlet.class, "/");
    context.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");

    String [] welcomeFiles = {"index.jsp"};
    context.setWelcomeFiles(welcomeFiles);
    // Set the .jsp servlet handlers
    final ServletHolder jsp = context.addServlet(JspServlet.class, "*.jsp");
    jsp.setInitParameter("classpath", context.getClassPath());

    // Session Manager
    SessionHandler sh = new SessionHandler();       
    context.setSessionHandler(sh);

    /* Http Request Handlers */
    context.addServlet(HttpRequestProcessor.class, "/HttpHandler");

    // Server configuration setup
    // Connector setup
    //  We explicitly use the SocketConnector because the SelectChannelConnector locks files
    Connector connector = new SocketConnector();
    connector.setHost("localhost");
    connector.setPort(Integer.parseInt(System.getProperty("jetty.port", new PropertiesConfiguration("smpp-config.properties").getString("http_port").trim())));
    connector.setMaxIdleTime(60000);

    JettyServer.SRV.setConnectors(new Connector[] { connector });
    JettyServer.SRV.setHandler(context);
    JettyServer.SRV.setAttribute("org.mortbay.jetty.Request.maxFormContentSize", 0);
    JettyServer.SRV.setGracefulShutdown(5000);
    JettyServer.SRV.setStopAtShutdown(true);

    logger.info("Starting Jetty Web Container....");
    try{
        JettyServer.SRV.start();
    } 
    catch(Exception ex){
        logger.error("Jetty Web Container failed to start [CAUSE : " + ex.getMessage() + "]");
        return;
    }


    logger.info("Jetty Web Container running....");
    while(true){
        try{
            JettyServer.SRV.join();
        }
        catch(InterruptedException iex){
            logger.error("Jetty Web Container interrupted [CAUSE : " + iex.getMessage() + "]");
        }
    }       
}   
}

代码格式正确

4

2 回答 2

1

您在 中使用相对路径context.setResourceBase("src/WebContent");会给您带来问题。

使用完整的、绝对的 URI 引用和context.setResourceBase(String).

请注意,您可以使用以下 URI 方案:fileftpjar,甚至http

于 2013-01-28T17:51:48.053 回答
0

而不是这个

final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

你能用这个吗?

WebAppContext root = new WebAppContext();

其余代码作为示例:

String webappDirLocation = "src/Webcontent/";

Server server = new Server(8080);


root.setContextPath(CONTEXT_PATH);
root.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
root.setResourceBase(webappDirLocation);

root.setParentLoaderPriority(true);

server.setHandler(root);
于 2013-01-29T05:06:39.740 回答