在独立的java程序中使用jetty
来源:互联网 发布:izeeyu是什么软件 编辑:程序博客网 时间:2024/06/15 20:58
在独立的java程序中,不使用tomcat,如何产生HTTP请求呢?可以使用jetty
jetty官网: http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html
1.使用jetty,需要引入jar包
maven项目中,在pom.xml添加如下内容:
<!-- jetty --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.8.v20171121</version> </dependency> <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-servlet --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.4.8.v20171121</version> </dependency>
2.参考示例
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.servlet.ServletHandler;public class MinimalServlets{ public static void main( String[] args ) throws Exception { // Create a basic jetty server object that will listen on port 8080. // Note that if you set this to port 0 then a randomly available port // will be assigned that you can either look in the logs for the port, // or programmatically obtain it for use in test cases. Server server = new Server(8080); // The ServletHandler is a dead simple way to create a context handler // that is backed by an instance of a Servlet. // This handler then needs to be registered with the Server object. ServletHandler handler = new ServletHandler(); server.setHandler(handler); // Passing in the class for the Servlet allows jetty to instantiate an // instance of that Servlet and mount it on a given context path. // IMPORTANT: // This is a raw Servlet, not a Servlet that has been configured // through a web.xml @WebServlet annotation, or anything similar. handler.addServletWithMapping(HelloServlet.class, "/hello"); // Start things up! server.start(); // The use of server.join() the will make the current thread join and // wait until the server is done executing. // See // http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join() server.join(); } @SuppressWarnings("serial") public static class HelloServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Hello from HelloServlet</h1>"); } }}
3.访问
本地运行程序,可以访问:http://localhost:8080/hello
阅读全文
0 0
- 在独立的java程序中使用jetty
- Jetty在win10上的配置,IDEA中配置Jetty,Maven中配置Jetty插件,Eclipse中配置Jetty插件及其使用,通过java代码内嵌Jetty Server
- 在独立的环境中通过使用基于 Java 的配置创建和使用Spring 容器
- Jetty配置JAAS(独立的Jetty服务器)
- 使用独立的*.config文件在Unity中
- 如何在独立的类似onClickListener中使用sendBroadcast
- 怎样在一个C 程序中调用另一个程序(独立可执行的程序, 或系统命令)?
- 使用嵌入式jetty在Eclipse中部署struts2示例程序struts2-blank
- 怎么让vb编写好的程序生成独立的文件啊,在其他电脑上也能独立使用?
- 在Java程序中使用逻辑块的技巧
- 在idea使用jetty
- 在idea使用jetty
- 在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(一)
- 在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(二)
- 在Eclipse中使用jetty启动maven项目突然停止的问题
- 在ACM中使用Java解程序
- 在java程序中使用memcached
- 在Java程序中使用系统环境变量
- tomcat7性能调优
- XFTP设置utf8编码的方法
- 在WIN7 搭建python 环境
- intellj idea 如何设置类头注释和方法注释
- web SSH项目报Table ‘performance_schema.session_variables’ doesn’t exist
- 在独立的java程序中使用jetty
- Goolgle knowledge graph API使用
- 2017.12.12 java多线程
- 开发项目遇到的大大小小问题总结
- 【深度学习】实时的物体识别-YOLO v2环境的搭建(4)
- MySQL 主从复制详解(详细)
- maven入门
- mysql中事件失效如何解决
- VOT2015 Benchmark简易教程