Página Inicial > Tutoriais > Using embedded Tomcat

Using embedded Tomcat

By using embedded tomcat, your Next application becomes executable. This way, you don’t need to deploy an installation of tomcat to use Next.

What you have to do:

  • Add tomcat libraries to your project
  • Create a launcher class

Note: You can create a Next application the standard way. Download next zip file; extract and run build-install.xml.
Be sure you have the console opened (in eclise: window > show view > console). You will need to create a folder lib/src in your project if using next 3.8.1 or older.


Add tomcat libraries to your project


1) Open the ivy.xml file and add tomcat libraries inside dependencies tag.

  <dependency org="org.apache.tomcat.embed" name="tomcat-embed-core"         rev="8.5.3" conf="lib"/>
  <dependency org="org.apache.tomcat.embed" name="tomcat-embed-logging-juli" rev="8.5.2" conf="lib"/>
  <dependency org="org.apache.tomcat.embed" name="tomcat-embed-jasper"       rev="8.5.3" conf="lib"/>
  <dependency org="org.apache.tomcat"       name="tomcat-jasper"             rev="8.5.3" conf="lib"/>
  <dependency org="org.apache.tomcat"       name="tomcat-jsp-api"            rev="8.5.3" conf="lib"/>
  <dependency org="org.apache.tomcat"       name="tomcat-jasper-el"          rev="8.5.3" conf="lib"/>
  <dependency org="org.apache.tomcat"       name="tomcat-el-api"             rev="8.5.3" conf="lib"/>
	
  <dependency org="org.eclipse.jdt.core.compiler" name="ecj"                 rev="4.5.1" conf="lib"/>

2) Right click on build.xml and select Run As > Ant Build ….
3) Select target Retrieve Ivy Dependencies (check Hide internal targets to filter targets if it is hard to find).
4) Run. This will download the libraries. Refresh (F5) your project and you will see the libraries under lib folder.
5) Select all the libraries and right click > Build Path > Add to build path.
6) The tomcat libraries are now configured.


Create a launcher class


Create a class under src folder

package launch;

import java.io.File;

import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

public class Main {

	private static final String CLASSES_DIR = "build";
	private static final String WEBAPP = "WebContent/";
	private static final String DEFAULT_PORT = "8080";

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

        String webappDirLocation = WEBAPP;
        Tomcat tomcat = new Tomcat();

        //The port that we should run on can be set into an environment variable
        //Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if(webPort == null || webPort.isEmpty()) {
            webPort = DEFAULT_PORT;
        }

        tomcat.setPort(Integer.valueOf(webPort));

        StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

        // Declare an alternative location for your "WEB-INF/classes" dir
        // Servlet 3.0 annotation will work
        File additionWebInfClasses = new File(CLASSES_DIR);
        WebResourceRoot resources = new StandardRoot(ctx);
        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes",
                additionWebInfClasses.getAbsolutePath(), "/"));
        ctx.setResources(resources);

        tomcat.start();
        tomcat.getServer().await();
    }
}

The constants CLASSES_DIR and WEBAPP are configured with eclipse defaults. If you are compiling your classes to a different folder, change CLASSES_DIR. If your web files are in a different folder, change WEBAPP.

Run your application



You can now run your application by executing Main class (right click > Run As > Java Application).

To access your application, go to localhost:8080 in your browser (no application name on path).



Note:

The dependencies on this tutorial are the minimum requirement to run embedded tomcat. You may need other libraries if you use other resources. If you get a class not found exception, you can check what are the dependencies required. For example you can see that tomcat-jasper depends on org.eclipse.jdt.core.compiler . ecj in http://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper/8.5.3

Be sure to have the console view opened. Any logs appear on this view. (In eclise select window > show view > console)

It is always recommended that you use a JDK in your IDE. Download and install a jdk. In eclipse, and you can configure it in window > preferences. The filter by JREs. Add the jdk in Installed JREs and also configure Execution Environments.

Credits to https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat#prerequisites

Categories: Tutoriais Tags: , ,
  1. Nenhum comentário ainda.
  1. Nenhum trackback ainda.