There should be a change in the decision to use MAVEN to manage the project and use jetty as a test container in Java Web development.
1.JAVAWEB Engineering Configuration Jetty-maven-plugin plug-in
<plugin><groupid>org.eclipse.jetty</groupid><artifactid>jetty-maven-plugin</ Artifactid><version>9.2.6.v20141205</version><configuration><scanintervalseconds>10 </scanIntervalSeconds></configuration></plugin>
Jetty's Maven plugin was transferred from Org.mortbay.jetty to Org.eclipse.jetty. The use of the latest jetty-mavevn-plugin requires jre1.7+, so you need to pay extra attention when using the JRE version that the MAVEN command relies on.
With Mvn-version, you can see the JRE version when you specifically execute the MAVEN command.
Apache maven 3.1.0 (893CA28A1DA9D5F51AC03827AF98BB730128F9F2; 2013-06-28 10:15:32+0800) Maven home:d:\__dev\ Apache-maven-3.1.0java version:1.6.0_30, Vendor:sun Microsystems Inc.java Home:d:\__dev\java\jdk1.6.0_30\jredefault LOCALE:ZH_CN, platform Encoding:gbkos name: "Windows 7", Version: "6.1", Arch: "x86", Family: "Windows"
The following error is found when executing the MVN jetty:run command in the above environment:
[ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.2.6.v20141205:run (DEFAULT-CLI) on project tiles: Execution default-cli of goal Org.eclipse.jetty:jetty-maven-plugin:9.2.6.v20141205:run failed:unable to load the Mojo ' r Un ' in the plugin ' org.eclipse.jetty:jetty-maven-plugin:9.2.6.v20141205 ' due to an API incompatibility: org.codehaus.plexus.component.repository.exception.componentlookupexception:org/eclipse/jetty/maven/plugin/ jettyrunmojo:unsupported Major.minor version 51.0
The problem is that the Java version we see when we execute the mvn-version command is not a 1.7+, and this can be a temporary change without changing the java_home settings of the system. In the command window, execute: Set java_home=java_jdk_1.7_ directory.
The specific operation as shown (this machine does not have a ready-made JDK7 used here JDK8):
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/B4/wKioL1SjV9DRQmZcAAHRSHLLFf0243.jpg "title=" jetty_ Run.png "alt=" Wkiol1sjv9drqmzcaahrshllff0243.jpg "/>
After the reset is complete, execute the jetty:run command:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/B7/wKiom1SjV8igIlhwAAHVjMcpZfY916.jpg "title=" jetty_ Start.png "alt=" Wkiom1sjv8igilhwaahvjmcpzfy916.jpg "/>
From the callout, you can see that jetty server started successfully.
This section focuses on the JRE environment that uses Jetty-maven-plugin to be aware of executing MAVEN commands.
Other lower versions of Jetty-maven-plugin are:
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactid>jetty-maven-plugin</ artifactid> <version>7.1.6.v20100715</version> <configuration> <scanintervalseconds>1 </scanIntervalSeconds> <reload>automatic</reload> <webAppConfig> <contextpath>/ tiles</contextpath> </webAppConfig> </configuration> </plugin>
Related documents:
[Org.eclipse.jetty]. [Jetty-maven-plugin]. [9.x]:http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
[Org.mortbay.jetty]. [Jetty-maven-plugin]. [Old_version]:http://docs.codehaus.org/display/jetty/maven+jetty+plugin
It is recommended to use the recently released version and follow Jetty starting from Http://eclipse.org/jetty/documentation.
2. Jetty-based programmable Web application development
2.0. Add dependencies
<dependency><groupid>javax.servlet</groupid><artifactid>javax.servlet-api</ artifactid><version>3.1.0</version><scope>provided</scope></dependency>< dependency><groupid>org.eclipse.jetty.aggregate</groupid><artifactid>jetty-all</ Artifactid><version>7.6.9.v20130131</version></dependency>
If you use a JSP in your app, you need to add jetty-jsp dependencies, in Jetty-all, and for the aggregation jetty-jsp module.
2.1 Creating a Service
package secondriver.embedded.jetty.app;import org.eclipse.jetty.server.handler;import org.eclipse.jetty.server.server;import org.eclipse.jetty.server.handler.contexthandler;import org.eclipse.jetty.server.handler.contexthandlercollection;import Org.eclipse.jetty.server.handler.defaulthandler;import org.eclipse.jetty.server.handler.handlercollection ;import org.eclipse.jetty.server.nio.selectchannelconnector;import org.eclipse.jetty.util.thread.queuedthreadpool;import Secondriver.embedded.jetty.app.servlet.dispatchhandler;public class programmableserver {public static void main (String[] args) throws Exception {// Create Serverserver server = new server ();// add threadpoolqueuedthreadpool queuedthreadpool = new queuedthreadpool (); Queuedthreadpool.setname ("Queuedtreadpool"); Queuedthreadpool.setminthreads ( (queuedthreadpool.setmaxthreads); serveR.setthreadpool (queuedthreadpool);// add connectorselectchannelconnector connector = new selectchannelconnector (); Connector.setport (8888); connector.setacceptors (4); Connector.setmaxbuffers (2048) ; Connector.setmaxidletime (10000); Server.addconnector (connector);// Add handlercontexthandlercollection context = new contexthandlercollection (); Contexthandler contexthandler = context.addcontext ("/", "/"); Contexthandler.sethandler (new dispatchhandler ()); Handler defaults = new defaulthandler (); Handlercollection collection = new handlercollection (); Collection.sethandlers (new handler[] { context, defaults }); Server.sethandler (collection);// start service Server.start (); while (server.isstarted ()) {system.out.println ("server starting ..."); System.out.println ("server stared ..."); System.out.println ("Contexthandlercollectio.getserver () = " + context.getserver (). Hashcode ()); System.out.println ("Server:" + server.hashcode ()); Server.join ();}}
2.2. Write the distribution processing class Dispatchhandler
Package Secondriver.embedded.jetty.app.servlet;import Java.io.ioexception;import javax.servlet.ServletException; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.eclipse.jetty.server.request;import Org.eclipse.jetty.server.handler.abstracthandler;public Class Dispatchhandler extends Abstracthandler {public void handle (String target, Request baserequest,httpservletrequest Request, HttpServletResponse response) throws IOException, servletexception {/** * here will be distributed to different handler according to different requests to handle */if ( Target.equals ("/index")) {new Indexhandler (). Handle (target, baserequest, request, response);} else {new Hellohandler () . Handle (target, baserequest, request, Response);}}
2.3. Write the corresponding handler (Indexhandler and Hellohandler)
public class Indexhandler extends Abstracthandler {@Overridepublic void handle (String target, Request baserequest, HttpServletRequest request, HttpServletResponse response) throws IOException, Servletexception { Response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out = Response.getwriter (), Out.write ("
public class Hellohandler extends Abstracthandler {public void handle (String target, Request baserequest, HttpServletRequest request, HttpServletResponse response) throws IOException, Servletexception { Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out = Response.getwriter (), Out.write ("
2.4. Start the service via browser access: Http://localhost:8888/index and Http://localhost:8888/hello
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/B5/wKioL1SjYRPR5faPAAHFQgzNikk154.jpg "title=" Jetty-programming.png "width=" 423 "height=" 280 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:423px;height:280px; " alt= "Wkiol1sjyrpr5fapaahfqgznikk154.jpg"/>
It is distributed to different handler for processing from a request that can see the result of the access response more requested. Most of the time, instead of using this, jetty is integrated into the application as an embedded container.
3.jetty integrated into the application as an embedded container
There is an open source project on the use of this area to do very well-sparkjava.
Sparkjava:http://sparkjava.com/github:https://github.com/perwendel/spark
Examples can be found in:
Http://git.oschina.net/secondriver/Spark-Java-App
Https://git.oschina.net/secondriver/jetty-app
This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1598025
Javaweb application Development Using jetty notes