Jetty的入门

一、开发环境

Eclipse  4.3.1

Maven  3.1

Jetty  9.0.6.v20130930

Jetty的下载地址:

http://download.eclipse.org/jetty/

二、新建Maven项目

1、新建webapp项目jettytest.

2、在Pom.xml中配置Jetty dependency和plugin

<!-- Jetty begin -->

       <dependency>

           <groupId>org.eclipse.jetty</groupId>

           <artifactId>jetty-server</artifactId>

           <version>9.0.6.v20130930</version>

       </dependency>

       <dependency>

           <groupId>org.eclipse.jetty</groupId>

           <artifactId>jetty-servlet</artifactId>

           <version>9.0.6.v20130930</version>

       </dependency>

       <!-- Jetty end -->

  <plugins>

           <!-- compiler插件, 设定JDK版本 -->

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-compiler-plugin</artifactId>

              <version>3.1</version>

              <configuration>

                  <source>1.7</source>

                  <target>1.7</target>

                  <showWarnings>true</showWarnings>

              </configuration>

           </plugin>

           <plugin>

              <groupId>org.mortbay.jetty</groupId>

              <artifactId>maven-jetty-plugin</artifactId>

           </plugin>

       </plugins>

三、新建SimpleServlet和JettyServer

1、新建一个简单的Servlet:SimlpeServlet

public class ServletServer {

    public static void main(String[] args) throws Exception {
Server server = new Server(8080); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context); context.addServlet(new ServletHolder(new SimpleServlet()), "/firstservlet");
context.addServlet(new ServletHolder(new SimpleServlet("Hello, this is my first Jetty servlet!")), "/secondservlet"); server.start();
server.join();
} }

2、新建JettyServer

public class SimpleServlet extends HttpServlet {

    private static final long serialVersionUID = -8470943914753284049L;

    private String message = "Hello World!";

    public SimpleServlet() {
} public SimpleServlet(String message) {
this.message = message;
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name");
String password = request.getParameter("password"); response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>" + message + "</h1>");
if (name != null) {
response.getWriter().println("名字:" + name + "</br>");
}
if (password != null) {
response.getWriter().println("密码:" + password + "</br>");
} }
}

四、运行结果

1、运行JettyServer,启动Jetty服务器。控制台出现下列信息表示服务器启动成功:

2013-10-12 22:20:36.281:INFO:oejs.Server:main: jetty-9.0.6.v20130930

2013-10-12 22:20:36.312:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@18c2661{/,null,AVAILABLE}

2013-10-12 22:20:36.406:INFO:oejs.ServerConnector:main: Started ServerConnector@838143{HTTP/1.1}{0.0.0.0:8080}

2、打开浏览器输入地址:http://127.0.0.1:8080/firstservlet。出现下列运行结果:

3、还可以输入地址:http://127.0.0.1:8080/secondservlet。出现下列运行结果:

五、大功告成!

Jetty入门的更多相关文章

  1. Jetty入门(1-3)Eclipse集成gradle-Gretty插件或maven-jetty插件运行应用

    英文来源:  http://akhikhl.github.io/gretty-doc/Getting-started.html 一.gradle插件 1.使用gretty来运行jetty: gradl ...

  2. Jetty入门(1-2)eclipse集成jetty插件并发布运行应用

    一.eclipse集成jetty插件 1.从市场安装jetty插件 2.使用jetty插件发布应用和配置运行环境 debug配置默认共用上述run配置 3.使用jetty插件启动运行和停止运行选中的应 ...

  3. Jetty入门(1-1)Jetty入门教程

    一.Jetty是什么? 1.Jetty 是一个Java语言编写的,开源的Servlet容器和应用服务器. Jetty 极度轻量级.高便携性.功能强大.灵活和扩展性好,而且支持各种技术如SPDY.Web ...

  4. jetty 入门

    jetty因其能作为内嵌的应用服务器,随应用一起存在,在小批量应用中很受欢迎. jetty作为应用服务器: jetty下载: 在官网下载jetty:http://www.eclipse.org/jet ...

  5. Jetty使用教程(四:21-22)—Jetty开发指南

    二十一.嵌入式开发 21.1 Jetty嵌入式开发HelloWorld 本章节将提供一些教程,通过Jetty API快速开发嵌入式代码 21.1.1 下载Jetty的jar包 Jetty目前已经把所有 ...

  6. Jetty官方文档翻译

    最近在学习Jetty,没有找到合适的资料,所有只能看官方文档了,但是只有英文的,想着自己翻译着学也是学还不如把学习的过程放到网上,也可以给需要的人看,英文水平毕竟有限,也是用有道翻译着来的,不过也加了 ...

  7. Jetty 服务器的知识

    Jetty 服务器的知识 也许你还没有听说过这个Jetty服务器,不过它确实是一种比较轻量级的Java服务器,和Tomcat一样,专门解释JavaWeb程序的服务器.因为在之前学习Java Web 的 ...

  8. Jetty总览

    Jetty入门 基本功能介绍 配置概览-怎么配置Jetty 配置概览-须要配置什么 Jetty配置 部署到Jetty 配置上下文 配置连接器 配置安全 配置JSP支持 Jetty管理指导 启动Jett ...

  9. Servlet引擎Jetty之入门1

    Jetty与tomcat一样,HttpWeb容器,支持实现Servlet规范. 详细介绍参考:https://www.ibm.com/developerworks/cn/java/j-lo-jetty ...

随机推荐

  1. GIS理论(墨卡托投影、地理坐标系、地面分辨率、地图比例尺、Bing Maps Tile System)

    [注]原文 http://www.cnblogs.com/beniao/archive/2010/04/18/1714544.html 墨卡托投影(Mercator Projection),又名&qu ...

  2. google打不开啦,咋办?

    前言:以前开发的时候一直使用google浏览器,好像是两年前的某一天突然间发现google搜索不能访问了,我喜欢将自己觉得有趣的网页做成标签页,google不能访问只能先换别的了,firefox也挺不 ...

  3. 百度地图api简单使用方法

    百度地图API的使用方法   百度地图API 开始学习百度地图API最简单的方式是看一个简单的示例.以下代码创建了一个520x340大小的地图区域并以天安门作为地图的中心: 1. <html&g ...

  4. NK3C 业务权限控制

    资源中,添加了一个类型:权限(橙色显示),现在有4种数据: 域管理员:domainAdmin 组织管理员:orgAdmin 组管理员:groupAdmin 一线员工:phoneAdmin 权限控制可以 ...

  5. LeetCode344:Reverse String@Python

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  6. Python自动化 【第七篇】:Python基础-面向对象高级语法、异常处理、Scoket开发基础

    本节内容: 1.     面向对象高级语法部分 1.1   静态方法.类方法.属性方法 1.2   类的特殊方法 1.3   反射 2.     异常处理 3.     Socket开发基础 1.   ...

  7. 在where条件中使用CASE WHEN 语句

    CREATE TABLE TB_Test_Report ( id int identity, stateid int, userid int, username ) ) go ,,'a') ,,'b' ...

  8. 用Jenkins配置自动化构建

     公司培训内容 -------------->记一笔 dubbo 微服务soadiamond-server 配置中心kafka rocketmq消息队列cas-server 单点登录spring ...

  9. notepad++ 右键

    在网上搜索建立reg 后运行, 虽然右键菜单出现了建立的右键项目名,但与软件不关联 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\ ...

  10. python学习笔记1-元类__metaclass__

    type 其实就是元类,type 是python 背后创建所有对象的元类   python 中的类的创建规则: 假设创建Foo 这个类 class Foo(Bar): def __init__(): ...