赞
踩
目录结构
- │ pom.xml
- │
- └─src
- ├─main
- │ ├─java
- │ │ └─com
- │ │ └─example
- │ │ EmbeddedJettyWebApp.java
- │ │ HelloWorldServlet.java
- │ │
- │ ├─resources
- │ └─webapp
- │ │ index.jsp
- │ │
- │ └─WEB-INF
- └─test
- └─java
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.example</groupId>
- <artifactId>web-app-embedded-jetty-9</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
-
-
-
-
- <!-- Jetty Server -->
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-server</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-servlet</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-webapp</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-annotations</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <!--jsp support for jetty -->
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>apache-jsp</artifactId>
- <version>9.4.45.v20220203</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>apache-jstl</artifactId>
- <version>9.4.45.v20220203</version>
- </dependency>
-
-
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.1</version>
- </plugin>
-
- <plugin>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-maven-plugin</artifactId>
- <version>9.4.45.v20220203</version>
- </plugin>
- </plugins>
- </build>
- </project>
EmbeddedJettyWebApp.java
- package com.example;
-
- 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.NetworkTrafficServerConnector;
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.util.thread.QueuedThreadPool;
- import org.eclipse.jetty.util.thread.ThreadPool;
- import org.eclipse.jetty.webapp.Configuration;
- import org.eclipse.jetty.webapp.FragmentConfiguration;
- import org.eclipse.jetty.webapp.MetaInfConfiguration;
- import org.eclipse.jetty.webapp.WebAppContext;
- import org.eclipse.jetty.webapp.WebInfConfiguration;
- import org.eclipse.jetty.webapp.WebXmlConfiguration;
-
- public class EmbeddedJettyWebApp {
-
- public static void main(String[] args) throws Exception {
- Server server = new Server(createThreadPool());
-
- NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
- connector.setPort(9090);
- connector.setHost("localhost");
-
- server.addConnector(connector);
-
- WebAppContext webAppContext = new WebAppContext();
- webAppContext.setContextPath("/");
- webAppContext.setResourceBase("src/main/webapp");
-
- webAppContext.setConfigurations(new Configuration[]{
- new AnnotationConfiguration(), new WebXmlConfiguration(),
- new WebInfConfiguration(), new PlusConfiguration(),
- new MetaInfConfiguration(), new FragmentConfiguration(),
- new EnvConfiguration()
- });
-
- webAppContext.setAttribute(
- "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
- ".*/target/classes/|.*\\.jar");
-
- webAppContext.setParentLoaderPriority(true);
-
- server.setHandler(webAppContext);
-
- server.setStopAtShutdown(true);
-
- server.start();
- server.join();
- }
-
- private static ThreadPool createThreadPool() {
- QueuedThreadPool threadPool = new QueuedThreadPool(5);
- return threadPool;
- }
- }
HelloWorldServlet.java
- package com.example;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.annotation.WebServlet;
-
- @WebServlet("/HelloWorld")
- public class HelloWorldServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("<h1>Hello World</h1>");
- out.close();
- }
-
- }
index.jsp
- <!DOCTYPE html>
- <html>
- <body>
- <p>
- Today's date: <%= (new java.util.Date()).toLocaleString() %>
- </p>
- </body>
- </html>
如果需要打包成jar运行,需要修改:
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.example</groupId>
- <artifactId>web-app-embedded-jetty-9</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
-
-
-
-
- <!-- Jetty Server -->
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-server</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-servlet</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-webapp</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-annotations</artifactId>
- <version>9.4.35.v20201120</version>
- </dependency>
-
- <!--jsp support for jetty -->
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>apache-jsp</artifactId>
- <version>9.4.45.v20220203</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>apache-jstl</artifactId>
- <version>9.4.45.v20220203</version>
- </dependency>
-
-
- </dependencies>
-
- <build>
- <resources>
-
- <resource>
- <directory>src/main/webapp</directory>
- <targetPath>src/main/webapp</targetPath>
- <includes>
- <include>**/**</include>
- </includes>
- </resource>
-
- </resources>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>3.1.1</version>
-
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass>com.example.EmbeddedJettyWebApp</mainClass>
- </manifest>
- </archive>
- </configuration>
-
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
-
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.1</version>
- </plugin>
-
- <plugin>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-maven-plugin</artifactId>
- <version>9.4.45.v20220203</version>
- </plugin>
- </plugins>
- </build>
- </project>
EmbeddedJettyWebApp.java
- package com.example;
-
- import java.io.InputStream;
- import java.net.URL;
- 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.NetworkTrafficServerConnector;
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.util.thread.QueuedThreadPool;
- import org.eclipse.jetty.util.thread.ThreadPool;
- import org.eclipse.jetty.webapp.Configuration;
- import org.eclipse.jetty.webapp.FragmentConfiguration;
- import org.eclipse.jetty.webapp.MetaInfConfiguration;
- import org.eclipse.jetty.webapp.WebAppContext;
- import org.eclipse.jetty.webapp.WebInfConfiguration;
- import org.eclipse.jetty.webapp.WebXmlConfiguration;
-
- public class EmbeddedJettyWebApp {
-
- public static void main(String[] args) throws Exception {
- Server server = new Server(createThreadPool());
-
- NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
- connector.setPort(9090);
- connector.setHost("localhost");
-
- server.addConnector(connector);
-
- WebAppContext webAppContext = new WebAppContext();
- webAppContext.setContextPath("/");
- String webDir = EmbeddedJettyWebApp.class.getClassLoader().getResource("src/main/webapp").toExternalForm();
-
- webAppContext.setResourceBase(webDir);
- System.out.println("ResourceBase:" + webAppContext.getResourceBase());
-
- webAppContext.setConfigurations(new Configuration[]{
- new AnnotationConfiguration(), new WebXmlConfiguration(),
- new WebInfConfiguration(), new PlusConfiguration(),
- new MetaInfConfiguration(), new FragmentConfiguration(),
- new EnvConfiguration()
- });
-
- webAppContext.setAttribute(
- "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
- ".*/target/classes/|.*\\.jar");
-
- webAppContext.setParentLoaderPriority(true);
-
- server.setHandler(webAppContext);
-
- server.setStopAtShutdown(true);
-
- server.start();
- server.join();
- }
-
- private static ThreadPool createThreadPool() {
- QueuedThreadPool threadPool = new QueuedThreadPool(5);
- return threadPool;
- }
- }
对于WEB-INF\lib下有jar库的情况时,不太容易处理fat jar里的目录,建议不使用java -jar xxx.jar运行,而是打fat jar 包后解压再运行,此时
EmbeddedJettyWebApp.java
- package com.example;
-
- import java.io.InputStream;
- import java.net.URL;
- 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.NetworkTrafficServerConnector;
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.util.thread.QueuedThreadPool;
- import org.eclipse.jetty.util.thread.ThreadPool;
- import org.eclipse.jetty.webapp.Configuration;
- import org.eclipse.jetty.webapp.FragmentConfiguration;
- import org.eclipse.jetty.webapp.MetaInfConfiguration;
- import org.eclipse.jetty.webapp.WebAppContext;
- import org.eclipse.jetty.webapp.WebInfConfiguration;
- import org.eclipse.jetty.webapp.WebXmlConfiguration;
-
- public class EmbeddedJettyWebApp {
-
- public static void main(String[] args) throws Exception {
- Server server = new Server(createThreadPool());
-
- NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
- connector.setPort(9090);
- connector.setHost("localhost");
-
- server.addConnector(connector);
-
- WebAppContext webAppContext = new WebAppContext();
- webAppContext.setContextPath("/");
- webAppContext.setResourceBase("src/main/webapp");
- System.out.println("ResourceBase:" + webAppContext.getResourceBase());
-
- webAppContext.setConfigurations(new Configuration[]{
- new AnnotationConfiguration(), new WebXmlConfiguration(),
- new WebInfConfiguration(), new PlusConfiguration(),
- new MetaInfConfiguration(), new FragmentConfiguration(),
- new EnvConfiguration()
- });
-
- webAppContext.setAttribute(
- "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
- ".*/target/classes/|.*\\.jar");
-
- webAppContext.setParentLoaderPriority(true);
-
- server.setHandler(webAppContext);
-
- server.setStopAtShutdown(true);
-
- server.start();
- server.join();
- }
-
- private static ThreadPool createThreadPool() {
- QueuedThreadPool threadPool = new QueuedThreadPool(5);
- return threadPool;
- }
- }
在解开包后的文件夹根目录,运行命令,如:
F:\web-app-embedded-jetty-jspmon\target\web-app-embedded-jetty-jspmon-0.0.1-SNAPSHOT-jar-with-dependencies>java com.example.EmbeddedJettyWebApp
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。