Jetty入门

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。出现下列运行结果:

  

五、大功告成!

原文地址:https://www.cnblogs.com/williamchang/p/3366155.html