JavaShuo
欄目
標籤
Jetty實戰之 嵌入式Jetty集成Spring運行
時間 2019-11-21
標籤
jetty
實戰
嵌入
集成
spring
運行
欄目
Jetty
简体版
原文
原文鏈接
本文連接:http://blog.csdn.net/kongxx/article/details/7227107
html
1. 首先修改pom.xml文件,添加spring的依賴項
java
[html]
view plain
copy
print
?
<
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
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring
</
artifactId
>
<
version
>
2.5.6
</
version
>
<
type
>
jar
</
type
>
<
scope
>
provided
</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>
junit
</
groupId
>
<
artifactId
>
junit
</
artifactId
>
<
version
>
3.8.1
</
version
>
<
scope
>
test
</
scope
>
</
dependency
>
</
dependencies
>
</
project
>
2. 建立一個Server類,用來經過spring來啓動Jetty server
[java]
view plain
copy
print
?
package
com.google.code.garbagecan.jettystudy.sample4;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
MyServer {
public
static
void
main(String[] args)
throws
Exception {
new
ClassPathXmlApplicationContext(
"/com/google/code/garbagecan/jettystudy/sample4/spring.xml"
);
}
}
3. 建立一個Handler類,用了處理http請求
[java]
view plain
copy
print
?
package
com.google.code.garbagecan.jettystudy.sample4;
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
MyHandler
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>"
);
response.getWriter().println(
"<li>Request url: "
+ target +
"</li>"
);
response.getWriter().println(
"<li>Server port: "
+ request.getServerPort() +
"</li>"
);
}
}
4. 建立一個spring配置文件,並放在com/google/code/garbagecan/jettystudy/sample4/spring.xml位置,內容以下.
[java]
view plain
copy
print
?
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
<bean id=
"Server"
class
=
"org.eclipse.jetty.server.Server"
init-method=
"start"
destroy-method=
"stop"
>
<property name=
"connectors"
>
<list>
<bean id=
"Connector"
class
=
"org.eclipse.jetty.server.nio.SelectChannelConnector"
>
<property name=
"port"
value=
"8080"
/>
</bean>
</list>
</property>
<property name=
"handler"
>
<bean id=
"handlers"
class
=
"org.eclipse.jetty.server.handler.HandlerList"
>
<property name=
"handlers"
>
<list>
<bean
class
=
"com.google.code.garbagecan.jettystudy.sample4.MyHandler"
/>
<bean
class
=
"org.eclipse.jetty.server.handler.DefaultHandler"
/>
</list>
</property>
</bean>
</property>
</bean>
</beans>
其中定義了Jetty Server的配置,包括Connector和Handler等等。
5. 運行MyServer類,而後經過http://localhost:8080/來訪問。
相關文章
1.
Jetty實戰(4)之嵌入式Jetty集成Spring運行
2.
Jetty實戰之 嵌入式Jetty集成Spring運行
3.
Jetty實戰之 嵌入式運行Jetty
4.
Jetty實戰(1)之嵌入式運行Jetty
5.
Jetty實戰之 嵌入式Jetty運行Servlet
6.
Jetty實戰之 嵌入式Jetty運行web app
7.
Jetty - 嵌入式運行Servlet
8.
Spring MVC實現Spring Security,Spring Stomp websocket Jetty嵌入式運行
9.
運行嵌入JETTY報錯
10.
Spring Boot 集成 Jetty
更多相關文章...
•
Spring DI(依賴注入)的實現方式:屬性注入和構造注入
-
Spring教程
•
Eclipse 運行程序
-
Eclipse 教程
•
Spring Cloud 微服務實戰(三) - 服務註冊與發現
•
Java Agent入門實戰(一)-Instrumentation介紹與使用
相關標籤/搜索
jetty
Jetty 入門實戰
spring+jetty+jersey+mybatis
maven+jetty
apache+jetty
jetty+maven
jetty+mongodb
springmvc+jetty
mvn+jetty
jetty+struts2
Jetty
Spring
紅包項目實戰
Spring教程
PHP教程
spring cloud
設計模式
委託模式
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
1.2 Illustrator多文檔的幾種排列方式
2.
5.16--java數據類型轉換及雜記
3.
性能指標
4.
(1.2)工廠模式之工廠方法模式
5.
Java記錄 -42- Java Collection
6.
Java記錄 -42- Java Collection
7.
github使用
8.
Android學習筆記(五十):聲明、請求和檢查許可
9.
20180626
10.
服務擴容可能引入的負面問題及解決方法
本站公眾號
歡迎關注本站公眾號,獲取更多信息
相關文章
1.
Jetty實戰(4)之嵌入式Jetty集成Spring運行
2.
Jetty實戰之 嵌入式Jetty集成Spring運行
3.
Jetty實戰之 嵌入式運行Jetty
4.
Jetty實戰(1)之嵌入式運行Jetty
5.
Jetty實戰之 嵌入式Jetty運行Servlet
6.
Jetty實戰之 嵌入式Jetty運行web app
7.
Jetty - 嵌入式運行Servlet
8.
Spring MVC實現Spring Security,Spring Stomp websocket Jetty嵌入式運行
9.
運行嵌入JETTY報錯
10.
Spring Boot 集成 Jetty
>>更多相關文章<<