有的时候需要将一个简单的功能封装为服务,相比python使用flask、web.py的简洁,使用java-web显得太重量级,幸好,我们可以直接在java项目中使用jetty来搭建简易服务

1、pom.xml加入jetty依赖

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.0.v20161208</version>
</dependency> <dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.0.v20161208</version>
</dependency> <dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<version>9.4.0.v20161208</version>
</dependency> <dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.1.4.v20140401</version>
</dependency>

  

2、增加Server

Serverserver=newServer(12580);

  

3、设置ServletContextHandler

ServletContextHandlercontext=newServletContextHandler(server,"/");
server.setHandler(context);

  

4、Context增加Servlet 
4.1 创建Servlet 继承HttpServlet,重载doGet,doPost即可

public class XXXHandler extends HttpServlet {

    @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
JSONObject ret = new JSONObject();
try {
String ttsTxt = req.getParameter("text"); String outFile = System.nanoTime() + ".mp4";
String url = xx.xxx(ttsTxt,...);
ret.put("ret","0");
ret.put("url",url);
}catch (Exception ex){
ret.put("ret","-1");
ret.put("error",ex.getMessage());
}
if(req.getParameter("callback")!=null) {
resp.getWriter().write(req.getParameter("callback")+"("+ret.toString()+")");
}else {
resp.getWriter().write(ret.toString());
}
} }

  

4.2 将Servlet 加入Context

context.addServlet(xxxHandler.class,"/xxx");
context.addServlet(Image2VideoHandler.class,"/*");

  

5、启动server

server.start();
server.join();

6、在浏览器访问http://localhost:12580/XXX 即可 

java内嵌jetty服务器的更多相关文章

  1. Message高级特性 & 内嵌Jetty实现文件服务器

    1. Messaage Properties  常见属性 更多的属性以及介绍参考:http://activemq.apache.org/activemq-message-properties.html ...

  2. Spring Boot 揭秘与实战(五) 服务器篇 - 内嵌的服务器 Tomcat剖析

    文章目录 1. 内嵌的 Tomcat,一个Jar包运行 2. 如何定制内嵌 Tomcat3. War 包部署的使用细节 2.1. 设置内嵌Tomcat的端口 2.2. 设置内嵌Tomcat的最大线程数 ...

  3. spring内嵌jetty容器,实现main方法启动web项目

    Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...

  4. IDEA内嵌Jetty启动SpringMvc项目

    这段时间本意是想要研究一下Netty的多线程异步NIO通讯框架,看完原理想要做下源码分析.查找资料发现Jetty框架底层支持用Netty做web请求的多线程分发处理,于是就筹备着将Jetty框架内嵌到 ...

  5. swt java 内嵌ActiveX控件

    这里用的是SWT/JFace开发application中SWT自带的org.eclipse.swt.ole.win32 包可以支持内嵌OLE和ActiveX. 具体用法如下: //创建一个OleFra ...

  6. eclipse内嵌jetty(run-jetty-run插件) 配置jndi数据源

    运行环境 java 6,eclipse juno,ssh(spring,hibernate,springmvc ) 1.离线安装 下载地址:http://pan.baidu.com/s/1qX67wO ...

  7. JAVA内嵌数据库H2的使用入门

    H2数据库是开源的,非常适合做嵌入式数据库使用,尤其用java编码的时候. H2的优势: 1.h2采用纯Java编写,因此不受平台的限制. 2.h2只有一个jar文件,十分适合作为嵌入式数据库试用. ...

  8. 内嵌Jetty输出debug日志

    Slf4jLog logger = new Slf4jLog(); logger.setDebugEnabled(true); Log.setLog(logger); log4j2.xml中配置如下章 ...

  9. Embed Tomcat Java(内嵌tomcat启动简述)

    简单记录一下内部tomcat启动 maven pom.xml <dependencies> <!-- embed tomcat dependency --> <depen ...

随机推荐

  1. hibernate映射关系(多对多)

    Student与Teacher关系多对多(只建了2个实体类) public class Student { private int id; private String name; private S ...

  2. Day8--------------yum软件包管理

    1.url三段式:协议.域名.路径 例如:http://wan.360.cn/game 2.本地yum配置: vim /etc/yum.repos.d/local.repo [local] #固定格式 ...

  3. layui 各种弹出框

    第一步:引用文件 第二步:写脚本 <script> //iframe窗 //layer.open({ // type: 2, // title: false, // closeBtn: 0 ...

  4. ueditor使用

    <html>    <head>        <meta charset="UTF-8">        <title></ ...

  5. 关于 IOS 时间的一下用法

    1. 加减月份 -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month {     NSDateComp ...

  6. Walle,一个开源的web代码发布管理系统

    前言 Walle 一个web部署系统工具,可能也是个持续发布工具,配置简单.功能完善.界面流畅.开箱即用!支持git.svn版本管理,支持各种web代码发布,静态的HTML,动态PHP,需要编译的JA ...

  7. flutter No material widget found textfield widgets require a material widget ancestor

    Error states that TextField widgets require a Material widget ancestor. Simply wrapping your whole l ...

  8. Git使用五:回到过去

    reset:将仓库里面的内容恢复回暂存区,类似于从仓库里检出文件到暂存区checkout:将暂存区的文件恢复回工作区,即,把暂存区的文件检出到工作区 下面是之前三次提交的内容 三个区域的文件状态: 执 ...

  9. 数据库解析IP,时间戳

    #解析IP SELECT INET_NTOA('168494269'); #解析时间戳 SELECT FROM_UNIXTIME('1505458308');

  10. 用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...