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. sencha touch 入门系列 (二)sencha touch 开发准备

    这是本人第一次写博客教程,没什么经验,文笔也不是很好,写这教程一方面为了巩固自己这段时间的学习成果,一方面帮助大家解决问题,欢迎大家多提建议,指出问题.接下来我们就开始我们的sencha touch开 ...

  2. Oracle10G 连接11G数据库,出现ORA -1017用户名/口令无效; 登录被拒绝 的问题

    一.出现症状 1.使用sqlplus连接正常 2.C#使用10g的ODP.NET连接时,报上面的错误 二.原因 1.Oracle11G之前密码是不区分大小写的,从11G开始默认密码区分大小写 2.使用 ...

  3. mybatis-generator-core生成代码

    mybatis-generator-core-1.3.3下载地址:http://blog.mybatis.org/p/products.html 下载后名解压,进入lib目录 修改一个Generato ...

  4. SQL笔记-第六章,索引与约束

    一.索引 CREATE INDEX 索引名 ON 表名(字段1, 字段2,……字段n) CREATE INDEX idx_person_nameage ON T_Person(FName,FAge) ...

  5. 利用Resgen.exe 批量生成resources文件

    Resgen.exe(资源文件生成器)  您可以直接如图操作 转换时在 文本中先写好要转换的文件然后 全选 复制到控制台中 Filename.resx 要转换的文件 ResName1.resource ...

  6. jenkins+findbugs

    1)  Jenkins安装findbugs插件 具体安装步骤:在主页面进入系统管理 选择插件管理 在过滤器中找出要安装的插件,并进行安装(Static Analysis Utilities.findb ...

  7. XidianOJ 1120 Gold of Orz Pandas

    题目描述 Orz Panda is addicted to one RPG game. To make his character stronger, he have to fulfil tasks ...

  8. mysql批量执行sql文件

    1.待执行的sql文件为1.sql.2.sql.3.sql.4.sql等 2.写一个batch.sql文件: source .sql; source .sql; source .sql; source ...

  9. 用Jenkins配置自动化构建

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

  10. C++之jsoncpp学习

    最新由于客户端要用到jsoncpp,所以自己也跟着项目的需求学了一下jsoncpp.以前没用过xml,但是感觉接触json后,还蛮好用的. 参考地址 http://jsoncpp.sourceforg ...