首页 > 代码库 > Maven中使用Jetty容器

Maven中使用Jetty容器

1、在pom.xml中添加Jetty的插件

  1. <plugin>
  2. <groupId>org.mortbay.jetty</groupId>
  3. <artifactId>maven-jetty-plugin</artifactId>
  4. <version>6.1.5</version>
  5. <configuration>
  6. <!-- webdefault.xml中将useFileMappedBuffer设置为false即可在jetty:run的时候不锁定js,css文件 -->
  7. <webDefaultXml>src/main/resources/jetty/webdefault.xml</webDefaultXml>
  8. <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
  9. <scanIntervalSeconds>3</scanIntervalSeconds>
  10. <contextPath>/</contextPath>
  11. <connectors>
  12. <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
  13. <port>8088</port>
  14. </connector>
  15. </connectors>
  16. </configuration>
  17. </plugin>

2、添加Jetty的配置文件

在/webSocket/src/main/resources/目录中新建jetty目录。
然后新建webdefault.xml文件。
技术分享
该文件可以在jetty的jar包中找到
需要将webdefault.xml中的useFileMappedBuffer设置为false,这样jetty在运行的过程中就不会锁定静态资源文件了
webdefault.xml文件的内容如下:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!-- ===================================================================== -->
  3. <!-- This file contains the default descriptor for web applications. -->
  4. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  5. <!-- The intent of this descriptor is to include jetty specific or common -->
  6. <!-- configuration for all webapps. If a context has a webdefault.xml -->
  7. <!-- descriptor, it is applied before the contexts own web.xml file -->
  8. <!-- -->
  9. <!-- A context may be assigned a default descriptor by: -->
  10. <!-- + Calling WebApplicationContext.setDefaultsDescriptor -->
  11. <!-- + Passed an arg to addWebApplications -->
  12. <!-- -->
  13. <!-- This file is used both as the resource within the jetty.jar (which is -->
  14. <!-- used as the default if no explicit defaults descriptor is set) and it -->
  15. <!-- is copied to the etc directory of the Jetty distro and explicitly -->
  16. <!-- by the jetty.xml file. -->
  17. <!-- -->
  18. <!-- ===================================================================== -->
  19. <web-app
  20. xmlns="http://java.sun.com/xml/ns/javaee"
  21. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  23. metadata-complete="true"
  24. version="2.5">
  25. <description>
  26. Default web.xml file.
  27. This file is applied to a Web application before it‘s own WEB_INF/web.xml file
  28. </description>
  29. <!-- ==================================================================== -->
  30. <!-- Context params to control Session Cookies -->
  31. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  32. <!-- UNCOMMENT TO ACTIVATE
  33. <context-param>
  34. <param-name>org.mortbay.jetty.servlet.SessionDomain</param-name>
  35. <param-value>127.0.0.1</param-value>
  36. </context-param>
  37. <context-param>
  38. <param-name>org.mortbay.jetty.servlet.SessionPath</param-name>
  39. <param-value>/</param-value>
  40. </context-param>
  41. <context-param>
  42. <param-name>org.mortbay.jetty.servlet.MaxAge</param-name>
  43. <param-value>-1</param-value>
  44. </context-param>
  45. -->
  46. <context-param>
  47. <param-name>org.mortbay.jetty.webapp.NoTLDJarPattern</param-name>
  48. <param-value>start.jar|ant-.*\.jar|dojo-.*\.jar|jetty-.*\.jar|jsp-api-.*\.jar|junit-.*\.jar|servlet-api-.*\.jar|dnsns\.jar|rt\.jar|jsse\.jar|tools\.jar|sunpkcs11\.jar|sunjce_provider\.jar|xerces.*\.jar</param-value>
  49. </context-param>
  50. <!-- ==================================================================== -->
  51. <!-- The default servlet. -->
  52. <!-- This servlet, normally mapped to /, provides the handling for static -->
  53. <!-- content, OPTIONS and TRACE methods for the context. -->
  54. <!-- The following initParameters are supported: -->
  55. <!-- -->
  56. <!-- acceptRanges If true, range requests and responses are -->
  57. <!-- supported -->
  58. <!-- -->
  59. <!-- dirAllowed If true, directory listings are returned if no -->
  60. <!-- welcome file is found. Else 403 Forbidden. -->
  61. <!-- -->
  62. <!-- redirectWelcome If true, redirect welcome file requests -->
  63. <!-- else use request dispatcher forwards -->
  64. <!-- -->
  65. <!-- gzip If set to true, then static content will be served-->
  66. <!-- as gzip content encoded if a matching resource is -->
  67. <!-- found ending with ".gz" -->
  68. <!-- -->
  69. <!-- resoureBase Can be set to replace the context resource base -->
  70. <!-- -->
  71. <!-- relativeResourceBase -->
  72. <!-- Set with a pathname relative to the base of the -->
  73. <!-- servlet context root. Useful for only serving -->
  74. <!-- static content from only specific subdirectories. -->
  75. <!-- -->
  76. <!-- useFileMappedBuffer -->
  77. <!-- If set to true (the default), a memory mapped -->
  78. <!-- file buffer will be used to serve static content -->
  79. <!-- when using an NIO connector. Setting this value -->
  80. <!-- to false means that a direct buffer will be used -->
  81. <!-- instead. If you are having trouble with Windows -->
  82. <!-- file locking, set this to false. -->
  83. <!-- -->
  84. <!-- cacheControl If set, all static content will have this value -->
  85. <!-- set as the cache-control header. -->
  86. <!-- -->
  87. <!-- maxCacheSize Maximum size of the static resource cache -->
  88. <!-- -->
  89. <!-- maxCachedFileSize Maximum size of any single file in the cache -->
  90. <!-- -->
  91. <!-- maxCachedFiles Maximum number of files in the cache -->
  92. <!-- -->
  93. <!-- cacheType "nio", "bio" or "both" to determine the type(s) -->
  94. <!-- of resource cache. A bio cached buffer may be used-->
  95. <!-- by nio but is not as efficient as a nio buffer. -->
  96. <!-- An nio cached buffer may not be used by bio. -->
  97. <!-- -->
  98. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  99. <servlet>
  100. <servlet-name>default</servlet-name>
  101. <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
  102. <init-param>
  103. <param-name>acceptRanges</param-name>
  104. <param-value>true</param-value>
  105. </init-param>
  106. <init-param>
  107. <param-name>dirAllowed</param-name>
  108. <param-value>true</param-value>
  109. </init-param>
  110. <init-param>
  111. <param-name>redirectWelcome</param-name>
  112. <param-value>false</param-value>
  113. </init-param>
  114. <init-param>
  115. <param-name>maxCacheSize</param-name>
  116. <param-value>4000000</param-value>
  117. </init-param>
  118. <init-param>
  119. <param-name>maxCachedFileSize</param-name>
  120. <param-value>254000</param-value>
  121. </init-param>
  122. <init-param>
  123. <param-name>maxCachedFiles</param-name>
  124. <param-value>1000</param-value>
  125. </init-param>
  126. <init-param>
  127. <param-name>cacheType</param-name>
  128. <param-value>both</param-value>
  129. </init-param>
  130. <init-param>
  131. <param-name>gzip</param-name>
  132. <param-value>true</param-value>
  133. </init-param>
  134. <init-param>
  135. <param-name>useFileMappedBuffer</param-name>
  136. <param-value>false</param-value> <!--?true??false??????????????,?????????????????? update by chenmk 2013.6.24-->
  137. </init-param>
  138. <!--
  139. <init-param>
  140. <param-name>cacheControl</param-name>
  141. <param-value>max-age=3600,public</param-value>
  142. </init-param>
  143. -->
  144. <load-on-startup>0</load-on-startup>
  145. </servlet>
  146. <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  147. <!-- ==================================================================== -->
  148. <!-- JSP Servlet -->
  149. <!-- This is the jasper JSP servlet from the jakarta project -->
  150. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  151. <!-- The JSP page compiler and execution servlet, which is the mechanism -->
  152. <!-- used by Glassfish to support JSP pages. Traditionally, this servlet -->
  153. <!-- is mapped to URL patterh "*.jsp". This servlet supports the -->
  154. <!-- following initialization parameters (default values are in square -->
  155. <!-- brackets): -->
  156. <!-- -->
  157. <!-- checkInterval If development is false and reloading is true, -->
  158. <!-- background compiles are enabled. checkInterval -->
  159. <!-- is the time in seconds between checks to see -->
  160. <!-- if a JSP page needs to be recompiled. [300] -->
  161. <!-- -->
  162. <!-- compiler Which compiler Ant should use to compile JSP -->
  163. <!-- pages. See the Ant documenation for more -->
  164. <!-- information. [javac] -->
  165. <!-- -->
  166. <!-- classdebuginfo Should the class file be compiled with -->
  167. <!-- debugging information? [true] -->
  168. <!-- -->
  169. <!-- classpath What class path should I use while compiling -->
  170. <!-- generated servlets? [Created dynamically -->
  171. <!-- based on the current web application] -->
  172. <!-- Set to ? to make the container explicitly set -->
  173. <!-- this parameter. -->
  174. <!-- -->
  175. <!-- development Is Jasper used in development mode (will check -->
  176. <!-- for JSP modification on every access)? [true] -->
  177. <!-- -->
  178. <!-- enablePooling Determines whether tag handler pooling is -->
  179. <!-- enabled [true] -->
  180. <!-- -->
  181. <!-- fork Tell Ant to fork compiles of JSP pages so that -->
  182. <!-- a separate JVM is used for JSP page compiles -->
  183. <!-- from the one Tomcat is running in. [true] -->
  184. <!-- -->
  185. <!-- ieClassId The class-id value to be sent to Internet -->
  186. <!-- Explorer when using <jsp:plugin> tags. -->
  187. <!-- [clsid:8AD9C840-044E-11D1-B3E9-00805F499D93] -->
  188. <!-- -->
  189. <!-- javaEncoding Java file encoding to use for generating java -->
  190. <!-- source files. [UTF-8] -->
  191. <!-- -->
  192. <!-- keepgenerated Should we keep the generated Java source code -->
  193. <!-- for each page instead of deleting it? [true] -->
  194. <!-- -->
  195. <!-- logVerbosityLevel The level of detailed messages to be produced -->
  196. <!-- by this servlet. Increasing levels cause the -->
  197. <!-- generation of more messages. Valid values are -->
  198. <!-- FATAL, ERROR, WARNING, INFORMATION, and DEBUG. -->
  199. <!-- [WARNING] -->
  200. <!-- -->
  201. <!-- mappedfile Should we generate static content with one -->
  202. <!-- print statement per input line, to ease -->
  203. <!-- debugging? [false] -->
  204. <!-- -->
  205. <!-- -->
  206. <!-- reloading Should Jasper check for modified JSPs? [true] -->
  207. <!-- -->
  208. <!-- suppressSmap Should the generation of SMAP info for JSR45 -->
  209. <!-- debugging be suppressed? [false] -->
  210. <!-- -->
  211. <!-- dumpSmap Should the SMAP info for JSR45 debugging be -->
  212. <!-- dumped to a file? [false] -->
  213. <!-- False if suppressSmap is true -->
  214. <!-- -->
  215. <!-- scratchdir What scratch directory should we use when -->
  216. <!-- compiling JSP pages? [default work directory -->
  217. <!-- for the current web application] -->
  218. <!-- -->
  219. <!-- tagpoolMaxSize The maximum tag handler pool size [5] -->
  220. <!-- -->
  221. <!-- xpoweredBy Determines whether X-Powered-By response -->
  222. <!-- header is added by generated servlet [false] -->
  223. <!-- -->
  224. <!-- If you wish to use Jikes to compile JSP pages: -->
  225. <!-- Set the init parameter "compiler" to "jikes". Define -->
  226. <!-- the property "-Dbuild.compiler.emacs=true" when starting Jetty -->
  227. <!-- to cause Jikes to emit error messages in a format compatible with -->
  228. <!-- Jasper. -->
  229. <!-- If you get an error reporting that jikes can‘t use UTF-8 encoding, -->
  230. <!-- try setting the init parameter "javaEncoding" to "ISO-8859-1". -->
  231. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  232. <servlet id="jsp">
  233. <servlet-name>jsp</servlet-name>
  234. <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
  235. <init-param>
  236. <param-name>logVerbosityLevel</param-name>
  237. <param-value>DEBUG</param-value>
  238. </init-param>
  239. <init-param>
  240. <param-name>fork</param-name>
  241. <param-value>false</param-value>
  242. </init-param>
  243. <init-param>
  244. <param-name>xpoweredBy</param-name>
  245. <param-value>false</param-value>
  246. </init-param>
  247. <!--
  248. <init-param>
  249. <param-name>classpath</param-name>
  250. <param-value>?</param-value>
  251. </init-param>
  252. -->
  253. <load-on-startup>0</load-on-startup>
  254. </servlet>
  255. <servlet-mapping>
  256. <servlet-name>jsp</servlet-name>
  257. <url-pattern>*.jsp</url-pattern>
  258. <url-pattern>*.jspf</url-pattern>
  259. <url-pattern>*.jspx</url-pattern>
  260. <url-pattern>*.xsp</url-pattern>
  261. <url-pattern>*.JSP</url-pattern>
  262. <url-pattern>*.JSPF</url-pattern>
  263. <url-pattern>*.JSPX</url-pattern>
  264. <url-pattern>*.XSP</url-pattern>
  265. </servlet-mapping>
  266. <!-- ==================================================================== -->
  267. <!-- Dynamic Servlet Invoker. -->
  268. <!-- This servlet invokes anonymous servlets that have not been defined -->
  269. <!-- in the web.xml or by other means. The first element of the pathInfo -->
  270. <!-- of a request passed to the envoker is treated as a servlet name for -->
  271. <!-- an existing servlet, or as a class name of a new servlet. -->
  272. <!-- This servlet is normally mapped to /servlet/* -->
  273. <!-- This servlet support the following initParams: -->
  274. <!-- -->
  275. <!-- nonContextServlets If false, the invoker can only load -->
  276. <!-- servlets from the contexts classloader. -->
  277. <!-- This is false by default and setting this -->
  278. <!-- to true may have security implications. -->
  279. <!-- -->
  280. <!-- verbose If true, log dynamic loads -->
  281. <!-- -->
  282. <!-- * All other parameters are copied to the -->
  283. <!-- each dynamic servlet as init parameters -->
  284. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  285. <!-- Uncomment for dynamic invocation
  286. <servlet>
  287. <servlet-name>invoker</servlet-name>
  288. <servlet-class>org.mortbay.jetty.servlet.Invoker</servlet-class>
  289. <init-param>
  290. <param-name>verbose</param-name>
  291. <param-value>false</param-value>
  292. </init-param>
  293. <init-param>
  294. <param-name>nonContextServlets</param-name>
  295. <param-value>false</param-value>
  296. </init-param>
  297. <init-param>
  298. <param-name>dynamicParam</param-name>
  299. <param-value>anyValue</param-value>
  300. </init-param>
  301. <load-on-startup>0</load-on-startup>
  302. </servlet>
  303. <servlet-mapping> <servlet-name>invoker</servlet-name> <url-pattern>/servlet/*</url-pattern> </servlet-mapping>
  304. -->
  305. <!-- ==================================================================== -->
  306. <session-config>
  307. <session-timeout>30</session-timeout>
  308. </session-config>
  309. <!-- ==================================================================== -->
  310. <!-- Default MIME mappings -->
  311. <!-- The default MIME mappings are provided by the mime.properties -->
  312. <!-- resource in the org.mortbay.jetty.jar file. Additional or modified -->
  313. <!-- mappings may be specified here -->
  314. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  315. <!-- UNCOMMENT TO ACTIVATE
  316. <mime-mapping>
  317. <extension>mysuffix</extension>
  318. <mime-type>mymime/type</mime-type>
  319. </mime-mapping>
  320. -->
  321. <!-- ==================================================================== -->
  322. <welcome-file-list>
  323. <welcome-file>index.html</welcome-file>
  324. <welcome-file>index.htm</welcome-file>
  325. <welcome-file>index.jsp</welcome-file>
  326. </welcome-file-list>
  327. <!-- ==================================================================== -->
  328. <locale-encoding-mapping-list>
  329. <locale-encoding-mapping><locale>ar</locale><encoding>ISO-8859-6</encoding></locale-encoding-mapping>
  330. <locale-encoding-mapping><locale>be</locale><encoding>ISO-8859-5</encoding></locale-encoding-mapping>
  331. <locale-encoding-mapping><locale>bg</locale><encoding>ISO-8859-5</encoding></locale-encoding-mapping>
  332. <locale-encoding-mapping><locale>ca</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  333. <locale-encoding-mapping><locale>cs</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  334. <locale-encoding-mapping><locale>da</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  335. <locale-encoding-mapping><locale>de</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  336. <locale-encoding-mapping><locale>el</locale><encoding>ISO-8859-7</encoding></locale-encoding-mapping>
  337. <locale-encoding-mapping><locale>en</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  338. <locale-encoding-mapping><locale>es</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  339. <locale-encoding-mapping><locale>et</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  340. <locale-encoding-mapping><locale>fi</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  341. <locale-encoding-mapping><locale>fr</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  342. <locale-encoding-mapping><locale>hr</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  343. <locale-encoding-mapping><locale>hu</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  344. <locale-encoding-mapping><locale>is</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  345. <locale-encoding-mapping><locale>it</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  346. <locale-encoding-mapping><locale>iw</locale><encoding>ISO-8859-8</encoding></locale-encoding-mapping>
  347. <locale-encoding-mapping><locale>ja</locale><encoding>Shift_JIS</encoding></locale-encoding-mapping>
  348. <locale-encoding-mapping><locale>ko</locale><encoding>EUC-KR</encoding></locale-encoding-mapping>
  349. <locale-encoding-mapping><locale>lt</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  350. <locale-encoding-mapping><locale>lv</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  351. <locale-encoding-mapping><locale>mk</locale><encoding>ISO-8859-5</encoding></locale-encoding-mapping>
  352. <locale-encoding-mapping><locale>nl</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  353. <locale-encoding-mapping><locale>no</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  354. <locale-encoding-mapping><locale>pl</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  355. <locale-encoding-mapping><locale>pt</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  356. <locale-encoding-mapping><locale>ro</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  357. <locale-encoding-mapping><locale>ru</locale><encoding>ISO-8859-5</encoding></locale-encoding-mapping>
  358. <locale-encoding-mapping><locale>sh</locale><encoding>ISO-8859-5</encoding></locale-encoding-mapping>
  359. <locale-encoding-mapping><locale>sk</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  360. <locale-encoding-mapping><locale>sl</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  361. <locale-encoding-mapping><locale>sq</locale><encoding>ISO-8859-2</encoding></locale-encoding-mapping>
  362. <locale-encoding-mapping><locale>sr</locale><encoding>ISO-8859-5</encoding></locale-encoding-mapping>
  363. <locale-encoding-mapping><locale>sv</locale><encoding>ISO-8859-1</encoding></locale-encoding-mapping>
  364. <locale-encoding-mapping><locale>tr</locale><encoding>ISO-8859-9</encoding></locale-encoding-mapping>
  365. <locale-encoding-mapping><locale>uk</locale><encoding>ISO-8859-5</encoding></locale-encoding-mapping>
  366. <locale-encoding-mapping><locale>zh</locale><encoding>GB2312</encoding></locale-encoding-mapping>
  367. <locale-encoding-mapping><locale>zh_TW</locale><encoding>Big5</encoding></locale-encoding-mapping>
  368. </locale-encoding-mapping-list>
  369. <security-constraint>
  370. <web-resource-collection>
  371. <web-resource-name>Disable TRACE</web-resource-name>
  372. <url-pattern>/</url-pattern>
  373. <http-method>TRACE</http-method>
  374. </web-resource-collection>
  375. <auth-constraint/>
  376. </security-constraint>
  377. </web-app>

3、启动Jetty

右键项目Run As–Maven build
技术分享
然后输入jetty:run 启动即可
技术分享

4、访问

在浏览器中输入http://localhost:8088 即可访问

Maven中使用Jetty容器