Jetty9.2开始之路

来源:互联网 发布:mac有没有软件助手 编辑:程序博客网 时间:2024/05/21 09:38

1.下载/设置JETTY_HOME

下载地址:http://www.eclipse.org/jetty/download.html

ubuntu:

sudo nano ~/.bashrc

图1

JETTY_HOME指向的你的本地文件路径

在CLASSPATH中添加$JETTY_HOME/lib

在PATH中添加$JETTY_HOME/bin

重启或者执行

. ~/.bashrc

让设置生效


2.文件整理

下载的源文件中有一些示例或示例配置在布署时是无用的,以jetty-distribution-9.2.17.v20160517为例主要有:JETTY_HOME/demo-base,以及配置文件目录(JETTY_HOME/etc)中示例xml文件.

root@xiaofanku-GA-880GM-D2H:/opt/jetty9# rm -rf demo-base

整理后的目录
图2

root@xiaofanku-GA-880GM-D2H:/opt/jetty9/etc# rm -rf example-quickstart.xml hawtio.xml jamon.xml jolokia.xml

整理后的配置文件(JETTY_HOME/etc)
图3


3.项目布署(JETTY_HOME/etc/jetty-deploy.xml)

直接把WAR包或项目的文件夹移到JETTY_HOME/webapps目录中即可.

3.1 如果想把某个项目设为ROOT(不需要打项目名直接访问)?
A:把WAR或项目的文件夹重命为ROOT即可

图4

B: 使用jetty-web.xml
在项目的WEB-INF目录下新建一个:jetty-web.xml,设置

<Set name="contextPath">/</Set>

测试的jetty-web.xml示例如下

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"><Configure id="htage" class="org.eclipse.jetty.webapp.WebAppContext">    <Set name="contextPath">/</Set>    <Call name="prependServerClass">        <Arg>-org.eclipse.jetty.util.</Arg>    </Call>    <Call name="prependServerClass">        <Arg>-org.eclipse.jetty.servlets.</Arg>    </Call>    <Get class="org.eclipse.jetty.util.log.Log" name="rootLogger">        <Call name="warn">            <Arg>test webapp is deployed. DO NOT USE IN PRODUCTION!</Arg>        </Call>    </Get></Configure>

这样即使导出的WAR不为ROOT.war也可以把项目设为root.如图:

图7

使用curl 获取地址的head信息, 以测试是否存在/htage项目 和 /项目

图8

注意:
jetty默认是把webapps中的war解压到

System.out.println(System.getProperty("java.io.tmpdir"));

我的ubuntu是:/tmp
图5

在关闭jetty时会从/tmp中把解压的项目删除掉.你可以在JETTY_HOME下新建一个work目录,这样在关闭jetty时解压的文件夹不会被删除.从图4中可以看到JETTY_HOME/work下的解压文件夹都在,包括重命名前的*_htage-any-和重命名后的*_any-

关于解压的详细说明访问:Jetty/Reference/Temporary Directories


4.输出日志(JETTY_HOME/etc/jetty-logging.xml)

jetty9.2默认使用的是:org.eclipse.jetty.util.log.Log,日志的存放文件夹是JETTY_HOME/log,如果你用无日志参数来启动jetty,在log中是看不到日志的.

java -jar start.jar etc/jetty-logging.xml

使用log4j就不需要这样的启动参数.把slf,log4j的三个jar放到JETTY_HOME/lib/ext文件夹中,三个jar包如下:

log4j-1.2.17.jarslf4j-api-1.7.13.jarslf4j-log4j12-1.7.13.jar

slf4j-log4j12-1.7.13.jar在slf下载的目录中就有,另外还需要配一下:log4j.properties,这个文件在JETTY_HOME/resources目录下,jetty9.2中就有这个文件,不过很基础

# This is not needed by Jetty - but it helps with many web apps.log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

本地调试用的log4j.properties

# Basic Log4j Configuration with STDOUT and File logginglog4j.rootLogger=DEBUG, filerlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nlog4j.appender.filer=org.apache.log4j.RollingFileAppenderlog4j.appender.filer.layout=org.apache.log4j.PatternLayoutlog4j.appender.filer.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nlog4j.appender.filer.File=${jetty.home}/logs/jetty.loglog4j.appender.filer.MaxFileSize=1MBlog4j.appender.filer.MaxBackupIndex=20

可以用你的替换掉或者在此基础上作修改.jetty wiki上有其它日志方案的说明:Jetty/Feature/Jetty Logging


5.配置dbcp2连接池

可以在JETTY_HOME/etc/jetty.xml或者在项目的WEB-INF目录下新建一个jetty-env.xml,我使用的是新建一个:jetty-env.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"><Configure id="htage" class="org.eclipse.jetty.webapp.WebAppContext">    <New id="htageDBPool" class="org.eclipse.jetty.plus.jndi.Resource">        <Arg>            <Ref refid="htage" />        </Arg>        <Arg>jdbc/htage</Arg>        <Arg>            <New class="org.apache.commons.dbcp2.BasicDataSource">                <Set name="driverClassName">com.mysql.jdbc.Driver</Set>                <Set name="url">jdbc:mysql://localhost:3306/apo_htage</Set>                <Set name="username">root</Set>                <Set name="password">root</Set>            </New>        </Arg>    </New></Configure>

注意:
1.DOCTYPE. 从JETTY_HOME/etc/jetty.xml中复制.我发现6,7,8的jetty的DOCTYPE都不一样

2.New的使用说明访问:Working with Jetty JNDI

3.连接池的示例访问:Datasource Examples

4.需要把dbcp2的相关jar放到JETTY_HOME/lib/ext目录下.这三个相关jar是

commons-dbcp2-2.1.jarcommons-logging-1.2.jarcommons-pool2-2.3.jar

不确定相关的jar依赖和准确版本,就用maven建一个空项目.对了还需要数据库的驱动.如下图

图6

5.在项目的WEB-INF/web.xml中添加

  <resource-ref>    <res-ref-name>jdbc/htage</res-ref-name>    <res-type>javax.sql.DataSource</res-type>    <res-auth>Container</res-auth>  </resource-ref>

话说在tomcat中配个连接池根本不用这步哈


6.开启GZip压缩

官方文档说可以使用Gzip Handler,折腾了几个小时也没起作用.只能退而求其次用:org.eclipse.jetty.servlets.GzipFilter

A.WEB-INF/web.xml中添加filter映射

    <filter>        <filter-name>GZipFilter</filter-name>        <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>        <init-param>            <param-name>mimeTypes</param-name>            <param-value>text/html,text/css,text/javascript,image/png</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>GZipFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

下面这个filter定义在JETTY_HOME/demo-base/webapps/test.war中的示例

  <filter>    <filter-name>GzipFilter</filter-name>    <filter-class>org.eclipse.jetty.servlets.AsyncGzipFilter</filter-class>    <async-supported>true</async-supported>    <init-param>      <param-name>bufferSize</param-name>      <param-value>8192</param-value>    </init-param>    <init-param>      <param-name>mimeTypes</param-name>      <param-value>text/plain,application/xml,text/html</param-value>    </init-param>    <init-param>      <param-name>minGzipSize</param-name>      <param-value>2048</param-value>    </init-param>    <init-param>      <param-name>userAgent</param-name>      <param-value>(?:Mozilla[^\(]*\(compatible;\s*+([^;]*);.*)|(?:.*?([^\s]+/[^\s]+).*)</param-value>    </init-param>    <init-param>      <param-name>cacheSize</param-name>      <param-value>1024</param-value>    </init-param>    <init-param>      <param-name>excludedAgents</param-name>      <param-value>MSIE 6.0</param-value>    </init-param>    <init-param>      <param-name>uncheckedPrintWriter</param-name>      <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>GzipFilter</filter-name>    <url-pattern>/dump/gzip/*</url-pattern>    <url-pattern>*.txt</url-pattern>  </filter-mapping>

B.确认当前的类路径上有jetty-servlets-9.2.17.v20160517.jar

我用的是jdk7.x所以只能用9.2.x,不要以为JETTY_HOME/lib下的所有jar都在jetty启动的类路径上,人家是模块化管理扩展(JETTY_HOME/modules).怎么查看start.jar的类路径上的jar?

java -jar start.jar --version

在我电脑上运行显示了这41个jar, 其中7个是我放上(JETTY_HOME/lib/ext)去的

Jetty Server Classpath:-----------------------Version Information on 41 entries in the classpath.Note: order presented here is how they would appear on the classpath.      changes to the --module=name command line options will be reflected here. 0:                      2.1 | ${jetty.base}/lib/ext/commons-dbcp2-2.1.jar 1:                      1.2 | ${jetty.base}/lib/ext/commons-logging-1.2.jar 2:                      2.3 | ${jetty.base}/lib/ext/commons-pool2-2.3.jar 3:                   1.2.17 | ${jetty.base}/lib/ext/log4j-1.2.17.jar 4:                   5.1.22 | ${jetty.base}/lib/ext/mysql-connector-java-5.1.22-bin.jar 5:                   1.7.13 | ${jetty.base}/lib/ext/slf4j-api-1.7.13.jar 6:                   1.7.13 | ${jetty.base}/lib/ext/slf4j-log4j12-1.7.13.jar 7:         9.2.17.v20160517 | ${jetty.base}/lib/apache-jsp/org.eclipse.jetty.apache-jsp-9.2.17.v20160517.jar 8:   3.8.2.v20130121-145325 | ${jetty.base}/lib/apache-jsp/org.eclipse.jetty.orbit.org.eclipse.jdt.core-3.8.2.v20130121.jar 9:                   8.0.33 | ${jetty.base}/lib/apache-jsp/org.mortbay.jasper.apache-el-8.0.33.jar10:                      2.3 | ${jetty.base}/lib/apache-jsp/org.mortbay.jasper.apache-jsp-8.0.33.jar11:                    1.2.1 | ${jetty.base}/lib/apache-jstl/org.apache.taglibs.taglibs-standard-impl-1.2.1.jar12:                    1.2.1 | ${jetty.base}/lib/apache-jstl/org.apache.taglibs.taglibs-standard-spec-1.2.1.jar13:                    (dir) | ${jetty.base}/resources14:                    3.1.0 | ${jetty.base}/lib/servlet-api-3.1.jar15:                 3.1.0.M0 | ${jetty.base}/lib/jetty-schemas-3.1.jar16:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-http-9.2.17.v20160517.jar17:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-server-9.2.17.v20160517.jar18:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-xml-9.2.17.v20160517.jar19:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-util-9.2.17.v20160517.jar20:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-io-9.2.17.v20160517.jar21:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-jndi-9.2.17.v20160517.jar22:      1.4.1.v201005082020 | ${jetty.base}/lib/jndi/javax.mail.glassfish-1.4.1.v201005082020.jar23:                      1.2 | ${jetty.base}/lib/jndi/javax.transaction-api-1.2.jar24:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-security-9.2.17.v20160517.jar25:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-servlet-9.2.17.v20160517.jar26:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-webapp-9.2.17.v20160517.jar27:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-deploy-9.2.17.v20160517.jar28:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-plus-9.2.17.v20160517.jar29:         9.2.17.v20160517 | ${jetty.base}/lib/jetty-annotations-9.2.17.v20160517.jar30:                    5.0.1 | ${jetty.base}/lib/annotations/asm-5.0.1.jar31:                    5.0.1 | ${jetty.base}/lib/annotations/asm-commons-5.0.1.jar32:                      1.2 | ${jetty.base}/lib/annotations/javax.annotation-api-1.2.jar33:                      1.0 | ${jetty.base}/lib/websocket/javax.websocket-api-1.0.jar34:         9.2.17.v20160517 | ${jetty.base}/lib/websocket/javax-websocket-client-impl-9.2.17.v20160517.jar35:         9.2.17.v20160517 | ${jetty.base}/lib/websocket/javax-websocket-server-impl-9.2.17.v20160517.jar36:         9.2.17.v20160517 | ${jetty.base}/lib/websocket/websocket-api-9.2.17.v20160517.jar37:         9.2.17.v20160517 | ${jetty.base}/lib/websocket/websocket-client-9.2.17.v20160517.jar38:         9.2.17.v20160517 | ${jetty.base}/lib/websocket/websocket-common-9.2.17.v20160517.jar39:         9.2.17.v20160517 | ${jetty.base}/lib/websocket/websocket-server-9.2.17.v20160517.jar40:         9.2.17.v20160517 | ${jetty.base}/lib/websocket/websocket-servlet-9.2.17.v20160517.jar

这时启动jetty会在日志中提示:

java.lang.ClassNotFoundException: org.eclipse.jetty.servlets.GzipFilter

这说明两个问题:

1.jetty的start.jar的类路径上没有jetty-servlets.jar.JETTY_HOME/start.ini中默认加载的module为

home-base-warning,server,deploy,websocket,ext,resources,jsp,jstl,http

2.JETTY_HOME/lib下不是所有jar都加载.因为jetty-servlets-9.2.17.v20160517.jar是真实存在这个目录的

怎么自定义加载module?

1.通过启动时的参数

java -jar start.jar --module=server,deploy,websocket,ext,resources,jsp,jstl,http,servlets

图9

上图是没有开启GZip的响应, 用上面的命令参数再启动首页的响应如下

图10

2.不想每次都加module参数怎么办?重写start.ini呀

java -jar start.jar --add-to-start=servlets

再看一看Jetty Server Classpath是否可以看到jetty-servlets的jar.如果看到再启动就不用加module参数了.

java -jar start.jar

附:
1.官方文档关于启动jetty时的Managing Startup Modules

2.stackoverflow.com上关于Option的提问

Jetty 9 “–module” instead of “OPTIONS”

Jetty “OPTIONS=All” migration to Jetty 9 modules

对于了解jetty提供了宝贵的指引,分享给各位读者


7.多项目布署

假如当前jetty的JETTY_HOME/webapps目录下有两个或多个项目,例如:
A.war为网站前端, B.war为管理后端, C.war为移动端. 希望使用一个域名的不同二级域名指向不同的war.例

www.domain.com--->A.waradmin.domain.com--->B.warm.domain.com--->C.war

以下为A.war项目的jetty-web.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"><Configure class="org.eclipse.jetty.webapp.WebAppContext">  <!-- 应用路径-->  <Set name="contextPath">/</Set>  <!-- v虚泥主机h-->  <Set name="virtualHosts">    <Array type="java.lang.String">      <Item>www.domain.com</Item>    </Array>  </Set></Configure>

以下为B.war项目的jetty-web.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"><Configure class="org.eclipse.jetty.webapp.WebAppContext">  <!-- 应用路径-->  <Set name="contextPath">/</Set>  <!-- v虚泥主机h-->  <Set name="virtualHosts">    <Array type="java.lang.String">      <Item>admin.domain.com</Item>    </Array>  </Set></Configure>

如果你希望继续使用项目名来提供访问,例如:

admin.domain.com/B

来访问B.war这个项目,只需要在jetty-web.xml中把contextPath设置为/B

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"><Configure class="org.eclipse.jetty.webapp.WebAppContext">  <!-- 应用路径-->  <Set name="contextPath">/B</Set>  <!-- v虚泥主机h-->  <Set name="virtualHosts">    <Array type="java.lang.String">      <Item>admin.domain.com</Item>    </Array>  </Set></Configure>

8.字符集/开发者工具

8.1先看系统的字符集

图11

8.2 开发者工具中测试字符集的代码

        String defaultCharsetName= Charset.defaultCharset().displayName();        System.out.println("defaultCharsetName:"+defaultCharsetName);        System.out.println(System.getProperty("file.encoding"));

NetBeans8.1中上面的代码输出UTF-8

IntelliJ IDEA中上面的代码输出GBK,因为我为了测试设置了一下字符集

图12

默认的也是UTF8,还需要改相关文件的模板在:File and Code Templates中

8.3 测试demo源码

index.jsp

<%@ page contentType="text/html;charset=$Encode" language="java" %><!DOCTYPE html><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset=$Encode">    <title>form page</title>  </head>  <body>  <div>    <form method="get" action="default.jsp">      <p>        <label>关键词</label>        <input type="text" name="word" size="20" />      </p>      <input type="submit" value="搜索" name="btn1"/>    </form>  </div>  <div>    <form method="post" action="default.jsp">      <p>        <label>用户名</label>        <input type="text" name="name" maxlength="12" size="15" />      </p>      <input type="submit" value="提交" name="btn2"/>    </form>  </div>  </body></html>

default.jsp

<%@ page contentType="text/html;charset=$Encode" language="java" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=$Encode">    <title>show page</title></head><body>    <a href="default.jsp?word=${param.word}">${param.word}</a>    <h1>welcome ${param.name}</></body></html>

jsp文件中的$Encode占位符在Netbeans8.1中为UTF-8,在IntelliJ IDEA中为GBK.

NetBeans demo 运行(无web.xml,无jetty-web.xml):
A: tomcat(Netbeans8.1内置的) GET正常,POST乱码
B: jetty9.2(默认就是UTF8,本地) GET正常,POST正常

IntelliJ IDEA demo使用jetty运行(无web.xml,无jetty-web.xml):
A:不加VM参数 GET乱码,POST乱码
B:加上VM参数, GET正常,POST正常

图13

分别说说除了file.encoding之外的三个:

-Dorg.eclipse.jetty.util.URI.charset=GBK

加上后, GET正常,POST乱码

-Dorg.eclipse.jetty.util.UrlEncoded.charset=GBK

单就这个参数无法解决POST乱码的问题,官方apidocs写的很清楚

org.eclipse.jetty.util.UrlEncoded

Handles coding of MIME "x-www-form-urlencoded".This class handles the encoding and decoding for either the query string of a URL or the _content of a POST HTTP request.NotesThe UTF-8 charset is assumed, unless otherwise defined by either passing a parameter or setting the "org.eclipse.jetty.util.UrlEncoding.charset" System property.The hashtable either contains String single values, vectors of String or arrays of Strings.This class is only partially synchronised. In particular, simple get operations are not protected from concurrent updates.

所以最终是需要这三个参数

GET:-Dorg.eclipse.jetty.util.URI.charset=GBKPOST:-Dorg.eclipse.jetty.util.UrlEncoded.charset=GBK-Dorg.eclipse.jetty.util.UrlEncoding.charset=GBK

参考文章地址:
Jetty/Howto/International Characters

jetty 9.2.x的 官方文档

0 0
原创粉丝点击