6

I've have the following code to use an embedded Jetty server alongside a simple servlet and .jsp webpage. However, after compiling and running the code:

javac -cp lib/servlet-api.jar:lib/jetty-all.jar com/test/MyServlet.java 
javac -cp lib/servlet-api.jar:lib/jetty-all.jar com/test/ServerMain.java 
java -cp .:lib/servlet-api.jar:lib/jetty-all.jar com/test/ServerMain

I get an error:

INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet

And navigating to /index.jsp gives a 500 error.

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

I've read this post but I don't think the solution applies here because I'm running Jetty embedded rather than using the start.jar.

How can I resolve this error so that the server will run and serve .jsp pages successfully?

ServerMain.java

package com.test;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class ServerMain {

    public static void main(String[] args) throws InterruptedException {

        Server server = new Server(8080);
        WebAppContext webApp = new WebAppContext();
        webApp.setDescriptor("web.xml");
        webApp.setResourceBase("");
        webApp.setParentLoaderPriority(true);
        server.setHandler(webApp);

        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
        server.join();
    }
}

MyServlet.java

package com.test;

import java.io.IOException;
import java.util.Properties;

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

public class MyServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, this is a testing servlet. \n\n");
        Properties p = System.getProperties();
        p.list(resp.getWriter());

    }
}

web.xml

   <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC "-//Oracle Corporation//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.test.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

This is my project structure:

webapp
----com
    ----test
        ----MyServlet.java
        ----ServerMain.java
        ----index.jsp
        ----web.xml
----lib
    ----jetty-all.jar
    ----servlet-api.jar  
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • You need to call java with the class name, so `com.test.ServerMain` instead of `com/test/ServerMain` (for javac its correct) – msrd0 Oct 06 '14 at 13:24
  • Thanks @msrd0, it actually runs successfully using `java com.test.ServerMain` and `java com/test/ServerMain` but thanks for the tip. –  Oct 06 '14 at 13:27
  • Are you sure that the libraries you use include the class `org.apache.jasper.servlet.JspServlet`? `jar tf lib/servlet-api.jar` would help – msrd0 Oct 06 '14 at 13:30
  • Having ran the command it seems the `JspServlet` class is not found in the lib/servlet-api.jar or lib/jetty-all.jar. However I found a jar online which does contain that class, re-compiled with the new jar in the class path but still the error persists. –  Oct 06 '14 at 13:52
  • Did you include it to the classpath at runtime? – msrd0 Oct 06 '14 at 13:57
  • No I didn't include the new jar in the cp at runtime, oops! I've re-ran it with the new jar in the cp and the error is no longer there :) Thanks @msrd0! If you create an answer I'll accept it. I'm now getting a new class not found error, it seems this could be a 'find all the dependencies' issue. –  Oct 06 '14 at 14:04

4 Answers4

6

Haven't tried for embedded Jetty but when running Jetty 9.3 as a service you need to add JSP support.

cd $JETTY_BASE
$JAVA_HOME/bin/java -jar $JETTY_HOME/start.jar --add-to-startd=jsp

Where JETTY_BASE is your folder where you deploy application which is separate from JETTY_HOME. So I'm guessing embedded Jetty would need similar configuration.

Nux
  • 9,276
  • 5
  • 59
  • 72
3

All answers here are deprecated, here is what you should do with Jetty 11:

java -jar $JETTY_HOME/start.jar --add-module=jsp
Benjamin Caure
  • 2,090
  • 20
  • 27
2

It seems like you're missing an JAR-file that includes the class org.apache.jasper.servlet.JspServlet. Download a JAR file containing it (look here) and add it to your classpath. Also, on a side note, you should replace com/test/ServerMain with the real class name, com.test.ServerMain. You java statement should look like this:

java -cp ".:lib/servlet-api.jar:lib/jetty-all.jar:lib/apache-jasper.jar" com.test.ServerMain
msrd0
  • 7,816
  • 9
  • 47
  • 82
2

Incidentally, there's a github project, maintained by the Jetty Project, demonstrating JSP support in Jetty Embedded.

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

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Thanks @Joakim, after looking around it seems that there are a number of jar file dependancies required to get the jasper jsp support to work inside jetty. I was hoping there would just be one jar file to download to keep my project lightweight and portable, but I couldn't find one. Finally decided jsp support wasn't worth the extra weight. –  Oct 07 '14 at 21:31
  • @Rob144 heh, JSP is anything but lightweight. Especially if you use the Glassfish/Jasper artifacts, slightly less so when you use the Apache/Jasper artifacts. – Joakim Erdfelt Oct 08 '14 at 00:23
  • @Rob144 also, the use of `jetty-all.jar` is the antithesis of lightweight, it literally contains everything and the kitchen sink that Jetty has available, of which most projects use less than 30%. In fact, the way some of the code is structured, it would be impossible to use 100% of that jar in a single project (as some features are optional and once used exclusively forbid other features from being used) – Joakim Erdfelt Oct 08 '14 at 00:54
  • @Rob144 the `jetty-all.jar` only exists as a teaching aid (referenced in the documentation), its not meant for production use. – Joakim Erdfelt Oct 08 '14 at 00:54