Java Server Pages (JSP) are a technology for building dynamic websites with Java. They are basically a mixture of HTML and Java code. They run in a special server, a servlet container, which compiles them to Java Servlets, Java objects with handler routines which are called whenever a client (user) accesses the page via HTTP(S). Thus, in order to get your JSP to run, you deploy it into a servlet container, like Tomcat, Jetty, or JBoss. This means you have to install the servlet container.
This small project is an example for how you can build a "fat" jar
which includes both the JSPs and the servlet container (Jetty). This jar
is stand-alone, i.e., you can directly run the jar
and access the server page without any installation or deployment.
You can compile it with Maven with goals compile
and package
. As result, you will get the standAloneJSPs-full.jar
, which you can run via java -jar standAloneJSPs-full.jar
without any additional requirements. It will then start the internal servlet container and then a browser to visit the entry page. In other words, different from our deployable JSP examples, you do not need a servlet container. This is similar to what we did in the HTTP Proxy Servlet example.
If you import this project in Eclipse, it may first show you a lot of errors. (I recommend using Eclipse Mars or later.) This is a Maven project, so you should "update" it first in Eclipse by
- Make sure that you can see the
package view
on the left-hand side of the Eclipse window. - Right-click on the project (
standAloneJSPsWithJetty
) in thepackage view
. - In the opening pop-up menu, left-click on
Maven
. - In the opening sub-menu, left-click on
Update Project...
. - In the opening window...
- Make sure the project (
standAloneJSPsWithJetty
) is selected. - Make sure that
Update project configuration from pom.xml
is selected. - You can also select
Clean projects
. - Click
OK
. - Now the structure of the project in the
package view
should slightly change, the project will be re-compiled, and the errors should disappear.
Now you can actually build the project, i.e., generate a jar
file that you can directly execute on the command line and which contains both your JSPs as well as the (Jetty) servlet container:
- Make sure that you can see the
package view
on the left-hand side of the Eclipse window. - Right-click on the project (
standAloneJSPsWithJetty
) in thepackage view
. - In the opening pop-up menu, choose
Run As
. - In the opening sub-menu choose
Run Configurations...
. - In the opening window, choose
Maven Build
- In the new window
Run Configurations
/Create, manage, and run configurations
, chooseMaven Build
in the small white pane on the left side. - Click
New launch configuration
(the first symbol from the left on top of the small white pane). - Write a useful name for this configuration in the
Name
field. You can use this configuration again later. - In the tab
Main
enter theBase directory
of the project, this is the folder calledjavaServerPages/standAloneJSPsWithJetty
containing the Eclipse/Maven project. - Under
Goals
, enterclean compile package
. This will build ajar
archive. - Click
Apply
- Click
Run
- The build will start, you will see its status output in the console window.
- The folder
target
will contain a filestandAloneJSPs-full.jar
after the build. This is the executable archive with our application.
Under Linux, you can also simply run make_linux.sh
in this project's folder to build the servlet without Eclipse, given that you have Maven installed.
This project is derived from embedded-jetty-jsp with copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. Compared to that project, the main differences are
- the "fat"
jar
, making the application stand-alone, - the usage of different (newer) versions of the dependencies in the Maven pom
- a new example servlet
- with the goal to make the example smaller,
- the Main class has been redesigned
- packages have been renamed
- while other classes and elements from that project have been deleted.
- a browser is automatically opened and pointed to the entry page of the example
The license of this project is Apache License v2.0.