当前位置:   article > 正文

配置嵌入式Jetty支持注解Servlet和JSP和打包成jar配置_jetty支持jsp

jetty支持jsp

目录结构

  1. │ pom.xml
  2. └─src
  3. ├─main
  4. │ ├─java
  5. │ │ └─com
  6. │ │ └─example
  7. │ │ EmbeddedJettyWebApp.java
  8. │ │ HelloWorldServlet.java
  9. │ │
  10. │ ├─resources
  11. │ └─webapp
  12. │ │ index.jsp
  13. │ │
  14. │ └─WEB-INF
  15. └─test
  16. └─java

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>web-app-embedded-jetty-9</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <maven.compiler.source>1.8</maven.compiler.source>
  12. <maven.compiler.target>1.8</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>javax.servlet</groupId>
  17. <artifactId>javax.servlet-api</artifactId>
  18. <version>3.1.0</version>
  19. </dependency>
  20. <!-- Jetty Server -->
  21. <dependency>
  22. <groupId>org.eclipse.jetty</groupId>
  23. <artifactId>jetty-server</artifactId>
  24. <version>9.4.35.v20201120</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.eclipse.jetty</groupId>
  28. <artifactId>jetty-servlet</artifactId>
  29. <version>9.4.35.v20201120</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.eclipse.jetty</groupId>
  33. <artifactId>jetty-webapp</artifactId>
  34. <version>9.4.35.v20201120</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.eclipse.jetty</groupId>
  38. <artifactId>jetty-annotations</artifactId>
  39. <version>9.4.35.v20201120</version>
  40. </dependency>
  41. <!--jsp support for jetty -->
  42. <dependency>
  43. <groupId>org.eclipse.jetty</groupId>
  44. <artifactId>apache-jsp</artifactId>
  45. <version>9.4.45.v20220203</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.eclipse.jetty</groupId>
  49. <artifactId>apache-jstl</artifactId>
  50. <version>9.4.45.v20220203</version>
  51. </dependency>
  52. </dependencies>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.apache.maven.plugins</groupId>
  57. <artifactId>maven-compiler-plugin</artifactId>
  58. <version>3.8.1</version>
  59. </plugin>
  60. <plugin>
  61. <groupId>org.eclipse.jetty</groupId>
  62. <artifactId>jetty-maven-plugin</artifactId>
  63. <version>9.4.45.v20220203</version>
  64. </plugin>
  65. </plugins>
  66. </build>
  67. </project>

EmbeddedJettyWebApp.java

  1. package com.example;
  2. import org.eclipse.jetty.annotations.AnnotationConfiguration;
  3. import org.eclipse.jetty.plus.webapp.EnvConfiguration;
  4. import org.eclipse.jetty.plus.webapp.PlusConfiguration;
  5. import org.eclipse.jetty.server.NetworkTrafficServerConnector;
  6. import org.eclipse.jetty.server.Server;
  7. import org.eclipse.jetty.util.thread.QueuedThreadPool;
  8. import org.eclipse.jetty.util.thread.ThreadPool;
  9. import org.eclipse.jetty.webapp.Configuration;
  10. import org.eclipse.jetty.webapp.FragmentConfiguration;
  11. import org.eclipse.jetty.webapp.MetaInfConfiguration;
  12. import org.eclipse.jetty.webapp.WebAppContext;
  13. import org.eclipse.jetty.webapp.WebInfConfiguration;
  14. import org.eclipse.jetty.webapp.WebXmlConfiguration;
  15. public class EmbeddedJettyWebApp {
  16. public static void main(String[] args) throws Exception {
  17. Server server = new Server(createThreadPool());
  18. NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
  19. connector.setPort(9090);
  20. connector.setHost("localhost");
  21. server.addConnector(connector);
  22. WebAppContext webAppContext = new WebAppContext();
  23. webAppContext.setContextPath("/");
  24. webAppContext.setResourceBase("src/main/webapp");
  25. webAppContext.setConfigurations(new Configuration[]{
  26. new AnnotationConfiguration(), new WebXmlConfiguration(),
  27. new WebInfConfiguration(), new PlusConfiguration(),
  28. new MetaInfConfiguration(), new FragmentConfiguration(),
  29. new EnvConfiguration()
  30. });
  31. webAppContext.setAttribute(
  32. "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
  33. ".*/target/classes/|.*\\.jar");
  34. webAppContext.setParentLoaderPriority(true);
  35. server.setHandler(webAppContext);
  36. server.setStopAtShutdown(true);
  37. server.start();
  38. server.join();
  39. }
  40. private static ThreadPool createThreadPool() {
  41. QueuedThreadPool threadPool = new QueuedThreadPool(5);
  42. return threadPool;
  43. }
  44. }

HelloWorldServlet.java

  1. package com.example;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import javax.servlet.annotation.WebServlet;
  9. @WebServlet("/HelloWorld")
  10. public class HelloWorldServlet extends HttpServlet {
  11. public void doGet(HttpServletRequest request,
  12. HttpServletResponse response) throws ServletException, IOException {
  13. response.setContentType("text/html");
  14. PrintWriter out = response.getWriter();
  15. out.println("<h1>Hello World</h1>");
  16. out.close();
  17. }
  18. }

index.jsp

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>
  5. Today's date: <%= (new java.util.Date()).toLocaleString() %>
  6. </p>
  7. </body>
  8. </html>

如果需要打包成jar运行,需要修改:

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>web-app-embedded-jetty-9</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <maven.compiler.source>1.8</maven.compiler.source>
  12. <maven.compiler.target>1.8</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>javax.servlet</groupId>
  17. <artifactId>javax.servlet-api</artifactId>
  18. <version>3.1.0</version>
  19. </dependency>
  20. <!-- Jetty Server -->
  21. <dependency>
  22. <groupId>org.eclipse.jetty</groupId>
  23. <artifactId>jetty-server</artifactId>
  24. <version>9.4.35.v20201120</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.eclipse.jetty</groupId>
  28. <artifactId>jetty-servlet</artifactId>
  29. <version>9.4.35.v20201120</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.eclipse.jetty</groupId>
  33. <artifactId>jetty-webapp</artifactId>
  34. <version>9.4.35.v20201120</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.eclipse.jetty</groupId>
  38. <artifactId>jetty-annotations</artifactId>
  39. <version>9.4.35.v20201120</version>
  40. </dependency>
  41. <!--jsp support for jetty -->
  42. <dependency>
  43. <groupId>org.eclipse.jetty</groupId>
  44. <artifactId>apache-jsp</artifactId>
  45. <version>9.4.45.v20220203</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.eclipse.jetty</groupId>
  49. <artifactId>apache-jstl</artifactId>
  50. <version>9.4.45.v20220203</version>
  51. </dependency>
  52. </dependencies>
  53. <build>
  54. <resources>
  55. <resource>
  56. <directory>src/main/webapp</directory>
  57. <targetPath>src/main/webapp</targetPath>
  58. <includes>
  59. <include>**/**</include>
  60. </includes>
  61. </resource>
  62. </resources>
  63. <plugins>
  64. <plugin>
  65. <groupId>org.apache.maven.plugins</groupId>
  66. <artifactId>maven-assembly-plugin</artifactId>
  67. <version>3.1.1</version>
  68. <configuration>
  69. <descriptorRefs>
  70. <descriptorRef>jar-with-dependencies</descriptorRef>
  71. </descriptorRefs>
  72. <archive>
  73. <manifest>
  74. <mainClass>com.example.EmbeddedJettyWebApp</mainClass>
  75. </manifest>
  76. </archive>
  77. </configuration>
  78. <executions>
  79. <execution>
  80. <id>make-assembly</id>
  81. <phase>package</phase>
  82. <goals>
  83. <goal>single</goal>
  84. </goals>
  85. </execution>
  86. </executions>
  87. </plugin>
  88. <plugin>
  89. <groupId>org.apache.maven.plugins</groupId>
  90. <artifactId>maven-compiler-plugin</artifactId>
  91. <version>3.8.1</version>
  92. </plugin>
  93. <plugin>
  94. <groupId>org.eclipse.jetty</groupId>
  95. <artifactId>jetty-maven-plugin</artifactId>
  96. <version>9.4.45.v20220203</version>
  97. </plugin>
  98. </plugins>
  99. </build>
  100. </project>

EmbeddedJettyWebApp.java

  1. package com.example;
  2. import java.io.InputStream;
  3. import java.net.URL;
  4. import org.eclipse.jetty.annotations.AnnotationConfiguration;
  5. import org.eclipse.jetty.plus.webapp.EnvConfiguration;
  6. import org.eclipse.jetty.plus.webapp.PlusConfiguration;
  7. import org.eclipse.jetty.server.NetworkTrafficServerConnector;
  8. import org.eclipse.jetty.server.Server;
  9. import org.eclipse.jetty.util.thread.QueuedThreadPool;
  10. import org.eclipse.jetty.util.thread.ThreadPool;
  11. import org.eclipse.jetty.webapp.Configuration;
  12. import org.eclipse.jetty.webapp.FragmentConfiguration;
  13. import org.eclipse.jetty.webapp.MetaInfConfiguration;
  14. import org.eclipse.jetty.webapp.WebAppContext;
  15. import org.eclipse.jetty.webapp.WebInfConfiguration;
  16. import org.eclipse.jetty.webapp.WebXmlConfiguration;
  17. public class EmbeddedJettyWebApp {
  18. public static void main(String[] args) throws Exception {
  19. Server server = new Server(createThreadPool());
  20. NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
  21. connector.setPort(9090);
  22. connector.setHost("localhost");
  23. server.addConnector(connector);
  24. WebAppContext webAppContext = new WebAppContext();
  25. webAppContext.setContextPath("/");
  26. String webDir = EmbeddedJettyWebApp.class.getClassLoader().getResource("src/main/webapp").toExternalForm();
  27. webAppContext.setResourceBase(webDir);
  28. System.out.println("ResourceBase:" + webAppContext.getResourceBase());
  29. webAppContext.setConfigurations(new Configuration[]{
  30. new AnnotationConfiguration(), new WebXmlConfiguration(),
  31. new WebInfConfiguration(), new PlusConfiguration(),
  32. new MetaInfConfiguration(), new FragmentConfiguration(),
  33. new EnvConfiguration()
  34. });
  35. webAppContext.setAttribute(
  36. "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
  37. ".*/target/classes/|.*\\.jar");
  38. webAppContext.setParentLoaderPriority(true);
  39. server.setHandler(webAppContext);
  40. server.setStopAtShutdown(true);
  41. server.start();
  42. server.join();
  43. }
  44. private static ThreadPool createThreadPool() {
  45. QueuedThreadPool threadPool = new QueuedThreadPool(5);
  46. return threadPool;
  47. }
  48. }

对于WEB-INF\lib下有jar库的情况时,不太容易处理fat jar里的目录,建议不使用java -jar xxx.jar运行,而是打fat jar 包后解压再运行,此时

EmbeddedJettyWebApp.java

  1. package com.example;
  2. import java.io.InputStream;
  3. import java.net.URL;
  4. import org.eclipse.jetty.annotations.AnnotationConfiguration;
  5. import org.eclipse.jetty.plus.webapp.EnvConfiguration;
  6. import org.eclipse.jetty.plus.webapp.PlusConfiguration;
  7. import org.eclipse.jetty.server.NetworkTrafficServerConnector;
  8. import org.eclipse.jetty.server.Server;
  9. import org.eclipse.jetty.util.thread.QueuedThreadPool;
  10. import org.eclipse.jetty.util.thread.ThreadPool;
  11. import org.eclipse.jetty.webapp.Configuration;
  12. import org.eclipse.jetty.webapp.FragmentConfiguration;
  13. import org.eclipse.jetty.webapp.MetaInfConfiguration;
  14. import org.eclipse.jetty.webapp.WebAppContext;
  15. import org.eclipse.jetty.webapp.WebInfConfiguration;
  16. import org.eclipse.jetty.webapp.WebXmlConfiguration;
  17. public class EmbeddedJettyWebApp {
  18. public static void main(String[] args) throws Exception {
  19. Server server = new Server(createThreadPool());
  20. NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
  21. connector.setPort(9090);
  22. connector.setHost("localhost");
  23. server.addConnector(connector);
  24. WebAppContext webAppContext = new WebAppContext();
  25. webAppContext.setContextPath("/");
  26. webAppContext.setResourceBase("src/main/webapp");
  27. System.out.println("ResourceBase:" + webAppContext.getResourceBase());
  28. webAppContext.setConfigurations(new Configuration[]{
  29. new AnnotationConfiguration(), new WebXmlConfiguration(),
  30. new WebInfConfiguration(), new PlusConfiguration(),
  31. new MetaInfConfiguration(), new FragmentConfiguration(),
  32. new EnvConfiguration()
  33. });
  34. webAppContext.setAttribute(
  35. "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
  36. ".*/target/classes/|.*\\.jar");
  37. webAppContext.setParentLoaderPriority(true);
  38. server.setHandler(webAppContext);
  39. server.setStopAtShutdown(true);
  40. server.start();
  41. server.join();
  42. }
  43. private static ThreadPool createThreadPool() {
  44. QueuedThreadPool threadPool = new QueuedThreadPool(5);
  45. return threadPool;
  46. }
  47. }

在解开包后的文件夹根目录,运行命令,如:

F:\web-app-embedded-jetty-jspmon\target\web-app-embedded-jetty-jspmon-0.0.1-SNAPSHOT-jar-with-dependencies>java com.example.EmbeddedJettyWebApp

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/124393
推荐阅读
相关标签
  

闽ICP备14008679号