FMC
FMC

Reputation: 660

Embedded jetty webserver JSP support not configured

I currently have Jetty embedded into an application. The Jetty server starts up and I can see the files in the project, however when I try to view a JSP file it says

HTTP ERROR 500
Problem accessing /web/index.jsp. Reason:
JSP support not configured

I have read some other posts on here and some tutorials online and most of them talk about start.jar and maven. I'm not using either of those I'm just using what is built into eclipse.

I have three classes

AppContextBuilder

public class AppContextBuilder
{

private WebAppContext webAppContext;


public WebAppContext buildWebAppContext()
{
    webAppContext = new WebAppContext();
    webAppContext.setDescriptor( webAppContext + "/WEB-INF/web.xml" );
    webAppContext.setResourceBase( "." );
    webAppContext.setContextPath( "/" );
    return webAppContext;
}

JettyServer

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

public class JettyServer
{

private Server server;


public JettyServer()
{
    this( 8585 );
}


public JettyServer(Integer runningPort)
{
    server = new Server( runningPort );
}


public void setHandler( ContextHandlerCollection contexts )
{
    server.setHandler( contexts );
}


public void start() throws Exception
{
    server.start();
}


public void stop() throws Exception
{
    server.stop();
    server.join();
}


public boolean isStarted()
{
    return server.isStarted();
}


public boolean isStopped()
{
    return server.isStopped();
}
}

ServerControl

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

public class ServerControl
{

public static void main( String[] args )
{
    Logger.getLogger().logMethodEntryMessage();

    ContextHandlerCollection contexts = new ContextHandlerCollection();

    contexts.setHandlers( new Handler[]
    { new AppContextBuilder().buildWebAppContext() } );

    final JettyServer jettyServer = new JettyServer();
    jettyServer.setHandler( contexts );

    start( jettyServer );

    Logger.getLogger().logMethodExitMessage();        
}


private static void start( final JettyServer jettyServer )
{
    Logger.getLogger().logMethodEntryMessage();

    try
    {
        jettyServer.start();
        Logger.getLogger().debug( "Jetty server started!" );
    }
    catch( Exception e )
    {
        Logger.getLogger().error( e.getMessage() );
    }

    Logger.getLogger().logMethodExitMessage();
}

}

I've never tried to use jetty embedded before so I may be missing something. I followed this tutorial online though and it seems to work for them.

Any help would be great, thanks!

Upvotes: 0

Views: 10301

Answers (2)

FMC
FMC

Reputation: 660

Managed to resolve this.

All I had to do was create a WAR and then add the following lines of code to the WebBuilderContext class.

webAppContext.setExtractWAR( true );    
webAppContext.setWar( "path/to/your/war/file/here" );

I'm using ANT for my builds so just added a new WAR target to generate the WAR file each time I build.

Hope this helps someone else.

Upvotes: 1

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49545

If that code snippet represents how you are initializing your webapp, then you are missing a lot of JSP initialization steps.

There's an example project produced by the Jetty Project showing how to use JSP with embedded Jetty at

https://github.com/jetty-project/embedded-jetty-jsp

Pay attention to ...

  • The required ClassLoader type
  • The scratchDir declaration
  • The ServletContainerInitializer setup
  • The Jsp Servlet Holder and mappings
  • The InstanceManager

(just to name a few big ones)

Upvotes: 0

Related Questions