美文网首页文字欲
Jetty实战之 嵌入式Jetty集成Spring运行

Jetty实战之 嵌入式Jetty集成Spring运行

作者: 架构师Javaspring | 来源:发表于2019-06-12 15:33 被阅读0次

1. 首先修改pom.xml文件,添加spring的依赖项

<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

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请求

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位置,内容如下.

<?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/来访问。

如何关闭jetty服务

阅读数 2355

有人分配给我壹个任务:去寻找壹种更好的停止Jetty服务器的方法,而在此之前我们是通过在命令行

写在最后:

码字不易看到最后了,那就点个关注呗,只收藏不点关注的都是在耍流氓!

关注并私信我“架构”,免费送一些Java架构资料,先到先得!

相关文章

网友评论

    本文标题:Jetty实战之 嵌入式Jetty集成Spring运行

    本文链接:https://www.haomeiwen.com/subject/myjlfctx.html