Giter Site home page Giter Site logo

embedded-jetty-jsp's Introduction

Example: Embedded Jetty w/ JSP Support

This is a maven project, to build it:

$ mvn clean package

To run the example org.eclipse.jetty.demo.Main:

$ mvn exec:java

Open your web browser to:

http://localhost:8080/  

To stop Jetty:

use CTRL+C

Code Of Interest

See org.eclipse.jetty.demo.Main

Set a Servlet Temp Directory

It is important for JSP to define a temp directory suitable for managing itself. Such a directory will be used for converting the JSP source into a java file and then compiling it into a class. A sub directory in this temp directory will be automatically added by the JSP implementation for loading the compiled JSP classes.

context.setAttribute("javax.servlet.context.tempdir",scratchDir);

Set a non-System Classloader

The JSP implementation will refuse to the System Classloader (per JSTL + JSP spec), this will wrap the system classloader in a simple URLClassLoader suitable for use by the JSP implementation.

// Set Classloader of Context to be sane (needed for JSTL)
// JSP requires a non-System classloader, this simply wraps the
// embedded System classloader in a way that makes it suitable
// for JSP to use
ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
context.setClassLoader(jspClassLoader);

Jsp Servlet must be named "jsp"

The JspServlet must be named "jsp" (per JSP spec).

// Add JSP Servlet (must be named "jsp")
ServletHolder holderJsp = new ServletHolder("jsp",JspServlet.class);
holderJsp.setInitOrder(0);

Default Servlet must exist

The JSP implementation relies on various Servlet Spec requirements, but mainly the fact that a "default" named servlet must exist.

// Add Default Servlet (must be named "default")
ServletHolder holderDefault = new ServletHolder("default",DefaultServlet.class);
holderDefault.setInitParameter("resourceBase",baseUri.toASCIIString());
holderDefault.setInitParameter("dirAllowed","true");
context.addServlet(holderDefault,"/");

embedded-jetty-jsp's People

Contributors

benhardy avatar dependabot[bot] avatar ericminio avatar janbartel avatar joakime avatar olamy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.