Jetty简介
Jetty是一个开源的servlet容器,它为基于Java的web内容,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接。
下载Jetty 9。
http://www.eclipse.org/jetty/ 。这里http://download.eclipse.org/jetty/
(下载JDK1.7,Apache Maven 3.3.3)
JDK1.7下载地址:http://download.oracle.com/otn/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz
Apache Maven 3.3.3下载地址:http://apache.fayea.com/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
安装JDK
- 检查系统是否自带java,如果有的话则应先将其卸载。
java -version #检查版本 rpm -qa | grep java #查看安装的jdk包 rpm -e java-1.7.0-openjdk -–nodeps #卸载jdk rpm -e java-1.6.0-openjdk -–nodeps
- 解压安装包
# tar zxvf jdk-7u79-linux-x64 # chmod +x -R /usr/loacl/jdk-7u79-linux #赋权限
安装maven
# tar xf apache-maven-3.3.3-bin.tar.gz # mv apache-maven-3.3.3 /usr/local/maven
安装jetty
# unzip jetty-distribution-9.2.6.v20141205.zip # mv jetty-distribution-9.2.6.v20141205 /usr/local/jetty
配置环境变量,启动Jetty
vim /etc/profile #在文件末尾添加如下几行
export JAVA_HOME=/java/jdk1.7.0_79 #JDK环境配置 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin export MAVEN_HOME=/usr/local/maven#maven环境配置 export PATH=$PATH:$MAVEN_HOME/bin export JETTY_HOME=/usr/local/jetty#jetty环境配置 export PATH=$PATH:$JETTY_HOME/bin
# source /etc/profile #使变量生效
[[email protected] opt]# mvn -v #检查maven版本 Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00) Maven home: /usr/local/maven Java version: 1.7.0_79, vendor: Oracle Corporation Java home: /java/jdk1.7.0_79/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "2.6.32-642.el6.x86_64", arch: "amd64", family: "unix"
Jetty目录结构
下载Jetty应该是一个压缩包,只要把它解压到某个目录下就可以。解压后的目录结构及其包含的内容
目录文件名描述
license-eplv10-aslv20.html#Jetty license文件
README.txt#入门信息
VERSION.txt#release信息
bin/#运行Jetty的辅助shell脚本
demo-base/#演示demo
etc/#Jetty XML配置文件目录,主要包含各个模块需要的配置文件
lib/#运行Jetty需要的所有JAR包
logs/#log目录
modules/#模块定义目录,例如 server, http, https等等模块的定义文件
notice.html#License信息
resources/#额外的资源信息,例如log4j配置
start.d/#包含一系列全局ini文件,其中每个ini文件存储各种命令行参数
start.ini#全局命令行参数文件,该文件包含的内容将添加到命令行参数中,也就是 java -jar start.jar [arg...] 该命令的参数
start.jar#运行Jetty的Jar文件
webapps/#webapps文件夹
测试并启动jetty
[[email protected] ~]#cd /usr/local/jetty/bin [[email protected] bin]# ./jetty.sh start Starting Jetty: 2016-10-27 21:01:19.404:INFO::main: Logging initialized @734ms 2016-10-27 21:01:19.514:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended. See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html 2016-10-27 21:01:20.125:INFO:oejs.Server:main: jetty-9.2.6.v20141205 2016-10-27 21:01:20.181:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/usr/local/jetty/webapps/] at interval 1 2016-10-27 21:01:21.821:INFO:oejsh.ContextHandler:main: Started [email protected]{/,file:/usr/local/jetty/webapps/ROOT/,AVAILABLE}{/ROOT} 2016-10-27 21:01:21.868:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1}{0.0.0.0:8080} 2016-10-27 21:01:21.868:INFO:oejs.Server:main: Started @3198ms #启动成功 OK Thu Oct 27 21:01:22 CST 2016
Jetty已经成功运行了,监听8080端口,浏览器直接访问就Ok了 http://localhost:8080
你可能发现了, "Error 404 – Not Found",这是为啥呢?
因为Jetty默认会扫描$JETTY_HOME/webapps目录和部署该目录下的应用,但是目前webapps下面没有任何应用,所以返回404错误。
部署应用:
Jetty中部署应用非常简单,只要把war包或包含内容的目录放到webapps目录下面就ok了。
cp -r /usr/local/jetty/demo-base/webapps/ROOT /usr/local/jetty/webapps/ #ROOT目录下的文件为测试jetty的demo页面。
注意:webapps下面ROOT(大小写均可)目录部署的应用对应的URL为跟目录(/)也就是说可以直接通过http://localhost:8080/ 访问
如图4所示:
或者自己写一个简单的页面
[[email protected] ~]# cd /usr/local/jetty/webapps/ [[email protected] webapps]# mkdir test [[email protected] webapps]# cd test [[email protected] test]# vi test.jsp <html> <body> <h4>simple test</h4> <%--echo hello world--%> <%@page language="java"%> <%="Hello World"%> </body> </html>
[[email protected] ~]# cd /usr/local/jetty/ [[email protected] jetty]# nohup java -jar start.jar & #拖至后台运行。 2016-10-27 21:11:20.290:INFO::main: Logging initialized @653ms 2016-10-27 21:11:20.356:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended. See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html 2016-10-27 21:11:20.636:INFO:oejs.Server:main: jetty-9.2.6.v20141205 2016-10-27 21:11:20.665:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/usr/local/jetty/webapps/] at interval 1 2016-10-27 21:11:21.544:INFO:oejsh.ContextHandler:main: Started [email protected]{/test,file:/usr/local/jetty/webapps/test/,AVAILABLE}{/test} 2016-10-27 21:11:21.571:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1}{0.0.0.0:8080} 2016-10-27 21:11:21.571:INFO:oejs.Server:main: Started @1935ms
或者进去bin目录执行命令./jetty.sh start
如下图:
至此完成了jetty的部署。期待下期jetty容器的配置