2

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

zdesam
  • 2,936
  • 3
  • 25
  • 32
Arsene
  • 1,037
  • 4
  • 20
  • 47
  • @zdesam Thank you. Please can you assist me? – Arsene Jan 28 '13 at 10:32
  • provide any error / exception if encoutered at server-side – TheWhiteRabbit Jan 28 '13 at 10:34
  • @TechExchange When I run the command java -jar this the output: 2013-01-28/10:36:01.212 INFO: Http.JettyServer - Initializing Web Server...... 2013-01-28/10:36:01.558 INFO: Http.JettyServer - Starting Jetty Web Container.... 2013-01-28/10:36:01.561 INFO: server.Server - jetty-8.y.z-SNAPSHOT 2013-01-28/10:36:01.761 INFO: server.AbstractConnector - Started SocketConnector@localhost:8080 2013-01-28/10:36:01.767 INFO: Http.JettyServer - Jetty Web Container running.... . When I try accessing the page it seems is going to load the page but nothing comes. – Arsene Jan 28 '13 at 10:43
  • @TechExchange However from within eclipse it works. – Arsene Jan 28 '13 at 10:46
  • 2013-01-28/10:48:36.020 DEBUG: server.session - session=null 2013-01-28/10:48:36.024 DEBUG: servlet.ServletHandler - servlet /smpp|/|null -> org.eclipse.jetty.servlet.DefaultServlet-1 2013-01-28/10:48:36.028 DEBUG: servlet.ServletHandler - chain=null 2013-01-28/10:48:36.032 DEBUG: servlet.DefaultServlet - Resource /=null 2013-01-28/10:48:36.035 DEBUG: servlet.DefaultServlet - uri=/smpp/ resource=null 2013-01-28/10:48:36.039 DEBUG: server.Server - RESPONSE /smpp/ 404 handled=true – Arsene Jan 28 '13 at 10:51
  • What I observe is that eclipse prepend the app root folder to the webroot whilst in the jar file when I run the app root folder is not prepend to the webroot. Any help???????? Guys please help – Arsene Jan 28 '13 at 12:47
  • what is the complete url you are trying ? – TheWhiteRabbit Jan 28 '13 at 14:10
  • @TechExchange localhost:8080/smpp. It seems that it doesn't load the src/WebContent because when I run it from the CLI I can see that /src/WebContent instead of /src/WebContent. – Arsene Jan 28 '13 at 16:37
  • See the solution I found for my question: [What is correct URL to specify ResourceBase of JAR “resources/webapp” folder for embedded Jetty?](http://stackoverflow.com/questions/25154708/what-is-correct-url-to-specify-resourcebase-of-jar-resources-webapp-folder-for?noredirect=1#comment39167596_25154708). That will work for you! – will Sep 02 '14 at 04:06

2 Answers2

1

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

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
0

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);
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • Hello TechExchange I did what yo have asked me but I am still not able to load the jsp page. I created the web.xml file and setup the various servlet in it. – Arsene Jan 29 '13 at 09:02