首页 > 代码库 > 嵌入jetty到Java代码

嵌入jetty到Java代码

在做Demo实例时,使用的jetty版本为8.x。为了避免麻烦,将所有的包都导入到MyEclipse的lib目录下。

实例1:自定义handler的服务器

package com.jetty.test01;

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,HttpServletRequest request,HttpServletResponse response) 
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }
}


package com.jetty.test01;

import org.eclipse.jetty.server.Server;

public class SimplestServer
{
    public static void main(String[] args) throws Exception
    {
    	Server server = new Server(8081);
        server.setHandler(new HelloHandler());
        server.start();
        server.join();
    }
}

访问路径:http://localhost:8080

实例2:通过xml文件配置服务器

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="FileServer" class="org.eclipse.jetty.server.Server">

	<Call name="addConnector">
		<Arg>
			<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
				<Set name="port">8080</Set>
			</New>
		</Arg>
	</Call>

	<Set name="handler">
		<New class="org.eclipse.jetty.server.handler.HandlerList">
			<Set name="handlers">
				<Array type="org.eclipse.jetty.server.Handler">
					<Item>
						<New class="org.eclipse.jetty.server.handler.ResourceHandler">
							<Set name="directoriesListed">true</Set>
							<Set name="welcomeFiles">
								<Array type="String">
									<Item>index.html</Item>
								</Array>
							</Set>
							<Set name="resourceBase">.</Set>
						</New>
					</Item>
					<Item>
						<New class="org.eclipse.jetty.server.handler.DefaultHandler">
						</New>
					</Item>
				</Array>
			</Set>
		</New>
	</Set>
</Configure>
package com.jetty.test01;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlConfiguration;

public class FileServerXml {
	public static void main(String[] args) throws Exception{
		Resource fileserver_xml = Resource.newSystemResource("fileServer.xml");
		XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
		Server server = (Server)configuration.configure();
		server.start();
		server.join();
	}
}

访问路径:http://localhost:8080

实例3:文件服务器:

package com.chinahrt.zyn.jetty;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;

public class FileServer {
	public static void main(String args[]) throws Exception{
		Server server = new Server();
		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setPort(8080);
		server.addConnector(connector);
		ResourceHandler resource_handler = new ResourceHandler();
		resource_handler.setDirectoriesListed(true);
		resource_handler.setWelcomeFiles(new String[]{"index.html"});
		resource_handler.setResourceBase(".");
		HandlerList handlers = new HandlerList();
		handlers.setHandlers(new Handler[]{resource_handler,new DefaultHandler()});
		server.setHandler(handlers);
		server.start();
		server.join();
	}
}

访问路径:http://localhost:8080

实例4:创建Servlet

package com.jetty.test01;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class OneServletContext {

	public static void main(String[] args) throws Exception{
		Server server = new Server(8085);
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
		context.setContextPath("/");
		server.setHandler(context);
		context.addServlet(new ServletHolder(new HelloServlet()), "/*");
		context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")), "/it/*");
		context.addServlet(new ServletHolder(new HelloServlet("Bonjour le monde")), "/fr/*");
		server.start();
		server.join();
	}
}
package com.jetty.test01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 246698127519451952L;
	
	private String greeting="Hello World";
	
	public HelloServlet(){}
	
	public HelloServlet(String greeting){
		this.greeting = greeting;
	}
	
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		response.setStatus(HttpServletResponse.SC_OK);
		response.getWriter().println("<h1>"+greeting+"</h1>");
		response.getWriter().println("session="+request.getSession(true).getId());
	}	
}

访问路径:http://localhost:8080/http://localhost:8080/ithttp://localhost:8080/fr

实例5: