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.
Mud Puddles ( bfs )
2.
ReSIProcate环境搭建
3.
SNAT(IP段)和配置网络服务、网络会话
4.
第8章 Linux文件类型及查找命令实践
5.
AIO介绍(八)
6.
中年转行互联网,原动力、计划、行动(中)
7.
详解如何让自己的网站/APP/应用支持IPV6访问,从域名解析配置到服务器配置详细步骤完整。
8.
PHP 5 构建系统
9.
不看后悔系列!Rocket MQ 使用排查指南(附网盘链接)
10.
如何简单创建虚拟机(CentoOS 6.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
>>更多相关文章<<