嵌入式jetty --- 轉載

嵌入式jetty

發表於  七月 3, 2012  由 forest

本文主要講述如何使用嵌入式jetty,建立http/https服務器. html

一.   相關jar包

Jetty-server
Jetty-servlet java

二.建立方式

2.1)最簡單的

1
2
3
Server server =newServer(8080);
server.start();
server.join();

建立一個jetty server,並啓動,這樣咱們就建立了一個最簡單的http服務器,檢查8080端口爲監聽狀態, 可是很明顯,該服務器不會對客戶端請求有任何響應. apache

2.2)HANDLER

1)實現

從實現上來看, jetty server自己就是一個HandlerWrapper, 能夠經過爲server設置handler,處理客戶端的請求. 編程

1
2
3
4
Server server =newServer(8080);
server.setHandler(newHelloHandler());
server.start();
server.join();

經過擴展AbstractHandler類, 實現handle方法, 定義用戶本身的handler, 例如: 瀏覽器

1
2
3
4
5
6
7
8
9
10
11
12
13
publicclassHelloHandlerextendsAbstractHandler {
publicvoidhandle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throwsIOException, ServletException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("</pre>
<h1>Hello World</h1>
<pre>
");
}
}

2)測試

瀏覽器上輸入: 安全

http://localhost:8080

輸出: 服務器

<h1>Hello World</h1>

2.3) SERVLET

直接爲Server設置Handler,咱們沒法針對不一樣的URL Path,完成不一樣的處理邏輯, 例如,
當瀏覽器輸入以下URL時,服務器區別處理: app

http://localhost:8080/*

http://localhost:8080/TYPE1/*


http://localhost:8080/TYPE2/*

1)實現

Server添加ServletContextHandler,而後,針對不一樣的path, 設置對應的Servlet來處理. eclipse

1
2
3
4
5
6
7
8
9
10
11
12
Server server =newServer(8080);
ServletContextHandler context =newServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(newServletHolder(newHelloServlet()),"/*");
context.addServlet(newServletHolder(newHelloServlet(
"TYPE1 Request")),"/TYPE1/*");
context.addServlet(newServletHolder(newHelloServlet(
"TYPE2 Request")),"/TYPE2/*");
server.start();
server.join();

經過擴展HttpServlet,覆蓋doPost或者doGet方法,定義用戶本身的Servlet,例如: socket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
publicclassHelloServletextendsHttpServlet {
String greeting ="Hello";
publicHelloServlet() {
}
 
publicHelloServlet(String hi) {
greeting = hi;
}
 
@Override
protectedvoiddoGet(HttpServletRequest request,
HttpServletResponse response)throwsServletException, IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("</pre>
<h1>」 + greeting + "</h1>
<pre>
");
}
 
@Override
protectedvoiddoPost(HttpServletRequest request,
HttpServletResponse response)throwsServletException, IOException {
doGet(request, response);
}
}

2)測試

瀏覽器上輸入:

http://localhost:8080/*

http://localhost:8080/TYPE1/*


http://localhost:8080/TYPE2/*

分別輸出:

<h1>Hello</h1>
 <h1> TYPE1 Request</h1>
 <h1> TYPE2 Request<h1>

2.3)Connector

http/https服務器也能夠同時在多個端口上提供服務, 這就須要引入Connector

1)實現

以下代碼就設置了兩個Connector, 一個使用8080端口,另外一個使用8888端口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Server server =newServer();
SelectChannelConnector connector0 =newSelectChannelConnector();
connector0.setPort(8080);
connector0.setMaxIdleTime(30000);
connector0.setRequestHeaderSize(8192);
 
SelectChannelConnector connector1 =newSelectChannelConnector();
connector1.setPort(8888);
connector1.setMaxIdleTime(30000);
connector1.setRequestHeaderSize(8192);
 
server.setConnectors(newConnector[] { connector0,
connector1 });
server.setHandler(newHelloHandler());
server.start();
server.join();

2)測試

瀏覽器上輸入:

http://localhost:8080/*

或者

http://localhost:8888/*

三.https服務器

本節着重講述如何建立https服務器.

3.1)KEYSTORE

使用keytool命令,建立keystore. 例如:

keytool -keystore zenithKS -alias zenith -genkey -keyalg RSA
Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  zenith

What is the name of your organizational unit?
  [Unknown]:  zenith

What is the name of your organization?
  [Unknown]:  zenith

What is the name of your City or Locality?
  [Unknown]:  bj

What is the name of your State or Province?
  [Unknown]:  bj

What is the two-letter country code for this unit?
  [Unknown]:  cn

Is CN=zenith, OU=zenith, O=zenith, L=bj, ST=bj, C=cn correct? (type "yes" or "no")
  [no]:  yes

Enter key password for <zenith>:
        (RETURN if same as keystore password):

須要用戶輸入keystore密碼以及key密碼,最後,會在當前目錄下生成一個zenithKS文件,這就是keystore文件

3.2)SSL CONNECTOR

爲jetty Server設置ssl Connector,須要指定keystore路徑, keystore密碼以及key密碼.

1
2
3
4
5
6
7
8
9
10
Server server =newServer();
SslSelectChannelConnector ssl_connector =newSslSelectChannelConnector();
ssl_connector.setPort(8443);
SslContextFactory cf = ssl_connector.getSslContextFactory();
cf.setKeyStore("D:\\keystores\\zenithKS");
cf.setKeyStorePassword("password");
cf.setKeyManagerPassword("password");
server.addConnector(ssl_connector);
server.start();
server.join();

瀏覽器上輸入:

https://localhost:8443/*

3.3)HTTPS客戶端

HttpClient提供了對SSL的支持, 如下經過擴展HttpClient類實現自動接受證書.

由於這種方法自動接收全部證書,所以存在必定的安全問題,因此在使用這種方法前請仔細考慮您的系統的安全需求。具體的步驟以下:

1) 提供一個自定義的socket factory(test.MySecureProtocolSocketFactory)。這個自定義的類必須實現接口

org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory

在實現接口 的類中調用自定義的

X509TrustManager(test.MyX509TrustManager)

這兩個類能夠在隨本文帶的附件中獲得

2) 建立一個org.apache.commons.httpclient.protocol.Protocol的實例,指定協議名稱和默認的端口號

Protocol myhttps = new Protocol("https", new MySecureProtocolSocketFactory (), 443);

3) 註冊剛纔建立的https協議對象

Protocol.registerProtocol("https ", myhttps);

4) 而後按照普通編程方式打開https的目標地址

代碼下載:

source

具體請參考:

http://www.blogjava.net/happytian/archive/2007/01/18/94553.html

四.參考資料

http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/embedded/package-summary.html
http://docs.codehaus.org/display/JETTY/How+to+configure+SSL

此條目是由  forest 發表在  J2EEJetty編碼技巧 分類目錄的。將 固定連接加入收藏夾。
相關文章
相關標籤/搜索