Jetty in the actual combat of embedded operation Jetty

Source: Internet
Author: User

This article address: http://blog.csdn.net/kongxx/article/details/7218776

One of the most common uses of jetty is to embed jetty into its own Java application, where Jetty is run as a background servlet container, accepting the user's HTTP request, and the following is one of the simplest uses for embedding jetty.

1. First use MAVEN to create a Java project

MVN Archetype:generate-dgroupid=com.google.code.garbagecan.jettystudy-dartifactid=jettystudy- Darchetypeartifactid=maven-archetype-quickstart-dinteractivemode=false
2. Modify the Pom file, add or modify the compilation and Dependencies section

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.google.code.garbagecan.jettystudy</groupId> < Artifactid>jettystudy</artifactid> <packaging>jar</packaging> <version>1.0-snapshot
		</version> <name>jettystudy</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <inherited>true
					</inherited> <configuration> <source>1.6</source> <target>1.6</target> <debug>true</debug> </configuration> </plugin> </plugins> </build> < Dependencies> <dependency> <groupid>org.eclipse.jetTy.aggregate</groupid> <artifactId>jetty-all</artifactId> <version>8.0.4.v20111024</ version> <type>jar</type> <scope>provided</scope> </dependency> <dependency&
			Gt <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version > <scope>test</scope> </dependency> </dependencies> </project>
3. Create a server class, use the start Jetty server, and through a hellohandler to process the browser sent over the request;

Package com.google.code.garbagecan.jettystudy.sample1;

Import Org.eclipse.jetty.server.Server;

public class MyServer {public
	static void Main (string[] args) throws Exception {
		Server server = new Server (8080) ;
		Server.sethandler (New Hellohandler ());
		
		Server.start ();
		Server.join ();
	}
4. Create a handler class with requests to process all clients

Package com.google.code.garbagecan.jettystudy.sample1;

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 Hellohandler extends Abstracthandler {public
	void handle (String target, Request baserequest, Httpservle Trequest request, HttpServletResponse response)
			throws IOException, servletexception {
		Response.setcontenttype ("Text/html;charset=utf-8");
		Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);
		Baserequest.sethandled (true);
		Response.getwriter (). println ("
5. Run the MyServer class and then access http://localhost:8080/through the browser, and you can see "Hello world." "and the requested URL.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.