Jetty非常适合做嵌入式web开发,正如Jetty的口号"Don’t deploy your application in Jetty, deploy Jetty in your application!"。
这个简单的例子是使用Jetty提供文件下载:
首先pom.xml配置文件如下
4.0.0
com.luangeng
jettydownload
1.0-SNAPSHOT
jar
jettydownload
http://maven.apache.org
UTF-8
8.1.9.v20130131
org.eclipse.jetty.aggregate
jetty-webapp
${jetty.version}
org.eclipse.jetty
jetty-jsp
${jetty.version}
junit
junit
3.8.1
test
View Code
---
main方法启动Jetty:
package com.luangeng;
import org.eclipse.jetty.server.Connector;
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.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.webapp.WebAppContext;
public class App
{
public static void main( String[] args ) throws Exception {
Server server = new Server();
server.setStopAtShutdown(true);
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8080);
connector.setReuseAddress(true);
server.setConnectors(new Connector[] { connector });
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/lg");
context.addServlet(DownLoadServlet.class, "/download");
context.addServlet(TestServlet.class, "/test/*");
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
server.setHandler(handlers);
server.start();
server.join();
}
}
---
提供简单的测试 TestServlet.java
packagecom.luangeng;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.PrintWriter;/*** Created by LG.*/
public class TestServlet extendsHttpServlet {public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();out.println("");
out.println("
test");
out.println("
");out.println("GET method");
out.println("");
out.println("");
out.flush();
out.close();
}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();out.println("");
out.println("
test");
out.println("
");out.println("POST method");
out.println("");
out.println("");
out.flush();
out.close();
}
}
---
完成下载功能的Servlet:
DownloadServlet.java
packagecom.luangeng;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;import java.io.*;importjava.net.URLEncoder;/*** Created by LG.*/
public class DownLoadServlet extendsHttpServlet {public voiddoGet(HttpServletRequest request, HttpServletResponse response) {//PrintWriter pw = null;
FileInputStream in = null;
OutputStream outs= null;//String path = request.getParameter("filepath");
String path = "/Users/LG/test.txt";//path = new String(fileName.getBytes("iso8859-1"),"UTF-8");
try{
File file= newFile(path);if(!file.exists())
{
response.setStatus(404);//pw.println("file not found!");//request.getRequestDispatcher("/err.jsp").forward(request, response);
return;
}
response.setContentType("text/html");
String realname= path.substring(path.indexOf("/")+1);
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
in= newFileInputStream(path);
outs=response.getOutputStream();byte buffer[] = new byte[1024];int len = 0;while ((len = in.read(buffer)) > 0) {
outs.write(buffer,0, len);
}
System.out.println(response.getStatus());
}catch(Exception e)
{//pw.println(e.getMessage());
response.setStatus(300);//log
}finally{
response.setStatus(200);try{if(outs != null) {
outs.flush();
outs.close();
}if(in != null) {
in.close();
}
}catch(IOException e) {
}//if(pw!=null) {//pw.flush();//pw.close();//}
}
}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
doGet(request, response);
}
}
---
end