Arsene
Arsene

Reputation: 1067

Embedded Jetty application

I a newbie in Jetty. I have created an application in which I embed the jetty web container. When I run the the application from eclipse it runs perfectly without any issues. However when I export the project with all the required libraries and run it from command line I cannot access the index.jsp web page like I used to in eclispe. This is the file that run the jetty web container.

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() + "]");
        }
    }       
}   
}

code formatted properly

Upvotes: 2

Views: 1564

Answers (2)

TheWhiteRabbit
TheWhiteRabbit

Reputation: 15768

Instead of this

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

Can you use this ?

WebAppContext root = new WebAppContext();

and rest of the code as example :

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);

Upvotes: 0

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49555

Your use of relative paths in the context.setResourceBase("src/WebContent"); will cause you problems.

Use a full, and absolute, URI reference with context.setResourceBase(String).

Note that you can use the following URI schemes: file, ftp, jar, and even http

Upvotes: 1

Related Questions