GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
mark.george
/
jetty
Browse code
Added some constants.
master
1 parent
a5bc087
commit
32341c2385bc346f94589f02bace42cc9f32d266
Mark George
authored
on 3 Nov 2021
Patch
Showing
3 changed files
build.gradle
src/main/java/Start.java
src/main/java/dao/DbConnection.java
Ignore Space
Show notes
View
build.gradle
import org.h2.tools.Console; buildscript { repositories { mavenCentral() } dependencies { classpath group: "com.h2database", name: "h2", version: "1.4.200" } } plugins { id "war" id "application" } repositories { mavenCentral() } dependencies { providedCompile group: "javax.servlet", name: "javax.servlet-api", version: "4.0.1" // jetty 10 supports servlet 4 def jettyVer = "10.+" implementation group: "org.eclipse.jetty", name: "jetty-webapp", version: jettyVer implementation group: "org.eclipse.jetty", name: "jetty-annotations", version: jettyVer implementation group: "org.eclipse.jetty", name: "apache-jsp", version: jettyVer implementation group: "org.eclipse.jetty", name: "jetty-slf4j-impl", version: jettyVer implementation group: "com.h2database", name: "h2", version: "1.4.200" implementation group: "ch.qos.logback", name: "logback-classic", version: "1.2.5" } war { // add db to WAR from 'db' } run { dependsOn war } task db { doLast { new org.h2.tools.Console().runTool("-user", "sa", "-url", "jdbc:h2:${projectDir}/db/db/users;AUTO_SERVER=TRUE;IFEXISTS=TRUE"); } } mainClassName = "Start"
import org.h2.tools.Console; buildscript { repositories { mavenCentral() } dependencies { classpath group: "com.h2database", name: "h2", version: "1.4.200" } } plugins { id "war" id "application" } repositories { mavenCentral() } dependencies { providedCompile group: "javax.servlet", name: "javax.servlet-api", version: "4.0.1" // jetty 10 supports servlet 4 def jettyVer = "10.+" implementation group: "org.eclipse.jetty", name: "jetty-webapp", version: jettyVer implementation group: "org.eclipse.jetty", name: "jetty-annotations", version: jettyVer implementation group: "org.eclipse.jetty", name: "apache-jsp", version: jettyVer implementation group: "org.eclipse.jetty", name: "jetty-slf4j-impl", version: jettyVer implementation group: "com.h2database", name: "h2", version: "1.4.200" implementation group: "ch.qos.logback", name: "logback-classic", version: "1.2.5" } war { // add db to WAR from 'db' } run { dependsOn war } task db { doLast { new org.h2.tools.Console().runTool("-user", "sa", "-url", "jdbc:h2:${projectDir}/db/db/users;AUTO_SERVER=TRUE;IFEXISTS=TRUE"); } } mainClassName = "Start"
Ignore Space
Show notes
View
src/main/java/Start.java
import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.sql.SQLException; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.jetty.annotations.AnnotationConfiguration; import org.eclipse.jetty.plus.webapp.EnvConfiguration; import org.eclipse.jetty.plus.webapp.PlusConfiguration; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.FragmentConfiguration; import org.eclipse.jetty.webapp.MetaInfConfiguration; import org.eclipse.jetty.webapp.WebAppConfiguration; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebInfConfiguration; import org.eclipse.jetty.webapp.WebXmlConfiguration; /** * * @author Mark George <mark.george@otago.ac.nz> */ public class Start { private static final Integer PORT = 8080; private static final String CONTEXT_PATH = "/shop"; public static void main(String[] args) throws Exception { Server server = new Server(PORT); WebAppContext context = new WebAppContext(); context.setWar("build/libs/jetty.war"); context.setConfigurations( new Configuration[]{ new AnnotationConfiguration(), new WebAppConfiguration(), new WebXmlConfiguration(), new WebInfConfiguration(), new PlusConfiguration(), new MetaInfConfiguration(), new FragmentConfiguration(), new EnvConfiguration() } ); context.setContextPath(CONTEXT_PATH); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); String path = context.getServletContext().getRealPath(File.separator); String dbUri = "jdbc:h2:file:" + path + "/db/users;AUTO_SERVER=TRUE;IFEXISTS=TRUE"; // open the web console for the deployed database (comment out block if you don't want the console) CompletableFuture.runAsync(() -> { try { new org.h2.tools.Console().runTool("-user", "sa", "-url", dbUri); } catch (Exception ex) { Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex); } }); // h2 has a pretty comprehensive openBrowser method that seems more reliable than the Desktop.browse method org.h2.tools.Server.openBrowser("http://localhost:8080" + CONTEXT_PATH); server.join(); } }
import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.sql.SQLException; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.jetty.annotations.AnnotationConfiguration; import org.eclipse.jetty.plus.webapp.EnvConfiguration; import org.eclipse.jetty.plus.webapp.PlusConfiguration; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.FragmentConfiguration; import org.eclipse.jetty.webapp.MetaInfConfiguration; import org.eclipse.jetty.webapp.WebAppConfiguration; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebInfConfiguration; import org.eclipse.jetty.webapp.WebXmlConfiguration; /** * * @author Mark George <mark.george@otago.ac.nz> */ public class Start { public static void main(String[] args) throws Exception { Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setWar("build/libs/jetty.war"); context.setConfigurations( new Configuration[]{ new AnnotationConfiguration(), new WebAppConfiguration(), new WebXmlConfiguration(), new WebInfConfiguration(), new PlusConfiguration(), new MetaInfConfiguration(), new FragmentConfiguration(), new EnvConfiguration() } ); context.setContextPath("/shop"); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); String path = context.getServletContext().getRealPath(File.separator); String dbUri = "jdbc:h2:file:" + path + "/db/users;AUTO_SERVER=TRUE;IFEXISTS=TRUE"; // // open the web console for the deployed database (comment out block if you don't want the console) // CompletableFuture.runAsync(() -> { // try { // new org.h2.tools.Console().runTool("-user", "sa", "-url", dbUri); // } catch (Exception ex) { // Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex); // } // }); // h2 has a pretty comprehensive openBrowser method that seems more reliable than the Desktop.browse method org.h2.tools.Server.openBrowser("http://localhost:8080/shop/"); server.join(); } }
Ignore Space
Show notes
View
src/main/java/dao/DbConnection.java
package dao; import java.io.File; import java.sql.Connection; import java.sql.SQLException; import org.h2.jdbcx.JdbcConnectionPool; import javax.servlet.ServletContext; public class DbConnection { private static final String USERNAME = "sa"; private static final String PASSWORD = ""; private static JdbcConnectionPool pool; public static Connection getConnection(ServletContext ctx) { String path = ctx.getRealPath(File.separator); String uri = "jdbc:h2:file:" + path + "/db/users;AUTO_SERVER=TRUE;IFEXISTS=TRUE"; System.out.println(uri); if (pool == null) { pool = JdbcConnectionPool.create(uri, USERNAME, PASSWORD); } try { return pool.getConnection(); } catch (SQLException ex) { throw new RuntimeException(ex); } } }
package dao; import java.io.File; import java.sql.Connection; import java.sql.SQLException; import org.h2.jdbcx.JdbcConnectionPool; import javax.servlet.ServletContext; public class DbConnection { private static final String USERNAME = "sa"; private static final String PASSWORD = ""; private static JdbcConnectionPool pool; public static Connection getConnection(ServletContext ctx) { String path = ctx.getRealPath(File.separator); String uri = "jdbc:h2:file:" + path + "/db/users;AUTO_SERVER=TRUE;IFEXISTS=TRUE"; System.out.println(uri); if (pool == null) { pool = JdbcConnectionPool.create(uri, USERNAME, PASSWORD); } try { return pool.getConnection(); } catch (SQLException ex) { throw new RuntimeException(ex); } } }
Show line notes below