Introduction to Jetty
Jetty is an open source servlet container that provides a runtime environment for Java-based web content, such as JSP and servlet. Jetty is written in Java language, and its API is released in the form of a set of JAR packages. Developers can instantiate the Jetty container into an object, which can quickly provide network and web connections for stand-alone Java applications.
Download Jetty 9.
http://www.eclipse.org/jetty/. Here http://download.eclipse.org/jetty/
(Download JDK1.7, Apache Maven 3.3.3)
JDK1.7 download link: http://download .oracle.com/otn/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz
Apache Maven 3.3.3 download address: http://apache.fayea.com /maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
Install JDK
-
Check if the system comes with java, if there is one, you should Uninstall.
java -version #Check version rpm -qa| grep java #View installed jdk package rpm -e java-1.7 .0-openjdk-Cnodeps #Uninstall jdkrpm -e java-1.6.0-openjdk-Cnodeps
-
Unzip the installation package
#tar zxvf jdk-7u79-linux-x64# chmod +x-R/usr/loacl/jdk-7u79-linux#Grant permissions
Install maven
# tar xf apache-maven-3.3.3-bin.tar.gz# mv apache -maven-3.3.3 /usr/local/maven
Install jetty
#unzip jetty-distribution-9.2.6.v20141205.zip# mv jetty-distribution-9.2.6.v20141205 /usr/local/jetty
Configure environment variables, start Jetty
vim /etc/profile #Add the following lines at the end of the file
ex port JAVA_HOME=/java/jdk1.7.0_79 #JDK environment configuration export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport PATH=$ PATH:$JAVA_HOME/binexport MAVEN_HOME=/usr/local/maven#maven environment configuration export PATH=$PATH:$MAVEN_HOME/binexport JETTY_HOME=/usr/local/jetty#jetty environment configuration export PATH=$PATH:$JETTY_HOME/bin
# source /etc/profile #Make the variable take effect
[root@ljohn opt]#mvn-v#Check maven version Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)Maven home: /usr/local/mavenJava version: 1.7.0_79, vendor: Oracle CorporationJava home: /java/jdk1.7.0_e79/jreDefault local , Platform encoding: UTF-8OS name: "linux", version: "2.6.32-642.el6.x86_64", arch: "amd64", family: "unix"
Jetty directory structure
p>
The download of Jetty should be a compressed package, just unzip it to a certain directory. The decompressed directory structure and its contents
directory file nameDescription
license-eplv10-aslv20.html#Jetty license file
README.txt#Getting started
VERSION.txt#release Information
bin/#Run Jetty’s auxiliary shell script
demo-base/# Demo demo
etc/#Jetty XML configuration file directory, which mainly contains the configuration files required by each module
lib/#All JAR packages needed to run Jetty
logs/#log directory
modules/# Module definition directory, such as server, http, https, etc. module definition files
notice.html#License信息
resources/#Additional resource information, such as log4j configuration
start.d/#Contains a series of global ini files, each ini file stores various command line parameters
start.ini#Global command line parameter file, the content of this file will be added to the command line parameter, that is java -jar start.jar [arg…] The parameters of the command
start.jar#Run Jetty Jar file
webapps/#webapps folder
Test and start jetty
[root@ljohn ~]#cd/usr/local/ jetty/bin[root@ljohnbin]#./jetty.sh start Starting Jetty: 2016-10-27 21:01:19.404:INFO::main: Logging initialized@734ms2016-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.html2016-10-27 21:01:20.125:INFO:oejs.Server:main: jetty-9.2.6. v201412052016-10-27 21:01:20.181:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/usr/local/jetty/webapps/] at interval 12016-10-27 21:01:21.821:INFO:oejsh .ContextHandler:main: Started oejwWebAppContext@43655aac{/,file:/usr/local/jetty/webapps/ROOT/,AVAILABLE}{/ROOT}2016-10-27 21:01:21.868:INFO:oejs.ServerConnector: main: Started ServerConnector@31e55839{HTTP/1.1}{0.0.0.0:8080}2016-10-27 21:01:21.868:INFO:oejs.Server:main:Started@3198ms#Started successfully OK Thu Oct 27 21:01 :22 CST 2016
Jetty has been successfully running, listening to port 8080, and the browser is OK if you directly access http: //localhost:8080
You may have found, “Error 404 �C Not Found“, why is this?
Because Jetty will scan the $JETTY_HOME/webapps directory and deploy applications in this directory by default, but currently there is no application under webapps, so it returns a 404 error.
Deploy the application :
Deploying applications in Jetty is very simple. Just put the war package or the directory containing the content under the webapps directory.
cp -r /usr/local/jetty/demo-base/webapps/ROOT /usr/local/jetty/webapps/ # The files in the ROOT directory are the demo pages for testing jetty.
Note: The URL corresponding to the application deployed in the ROOT (upper and lowercase) directory under webapps is the directory (/), which means that it can be directly passed through http:/ /localhost:8080/ Access
As shown in Figure 4:
[root@ljohn ~]#cd/usr/local/jetty/webapps/[root@ljohnwebapps]#mkdirtest[root@ljohnwebapps]#cdtest[root@ljohntest]# vi test.jsp
simple test
<%--echo hello world--%><%@pagelanguage="java"%><%="HelloWorld"%>[root@ljohn~]#cd/usr/local/jetty/[root@ljohnjetty]#nohupjava -jar start.jarDrag to the background to run. 2016-10-27 21:11:20.290:INFO::main:Logginginitialized@653ms2016-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.html2016-10-27 21:11:20.636:INFO:oejs.Server:main: jetty-9.2.6.v201412052016-10-27 21:11:20.665:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/usr/local/jetty/webapps/] at interval 12016-10-27 21:11 :21.544:INFO:oejsh.ContextHandler:main: Started oejwWebAppContext@166f10ef{/test,file:/usr/local/jetty/webapps/test/,AVAILABLE}{/test}2016-10-27 21:11:21.571 :INFO:oejs.ServerConnector:main:StartedServerConnector@3cb3600d{HTTP/1.1}{0.0.0.0:8080}2016-10-2721:11:21.571:INFO:oejs.Server:main:Started@1935ms
Or go to the bin directory and execute the command./jetty.sh start
As shown below: