Maven+Jetty+Servlet 简单示例

avatar 2020年2月29日14:23:31 评论 88 views

一个朋友让我帮他写一个基于 Servlet +Jetty+Derby 实现的项目。本文先介绍 Maven+Jetty+Servlet 的整合例子。

1、添加依赖

pom.xml 里添加依赖

<!--  Servlet  -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
</dependency>

<!--  jetty  -->
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <version>9.4.14.v20181114</version>
  <artifactId>jetty-webapp</artifactId>
</dependency>

 

2、准备两个 Servlet 例子

HelloServlet

package com.example.server.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 言曌
 * @date 2020/2/29 1:03 下午
 */
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String msg = "Hello World!";

    public HelloServlet() {
    }

    public HelloServlet(String msg) {
        this.msg = msg;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>" + msg + "</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}

 

GoodbyeServlet

package com.example.server.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 言曌
 * @date 2020/2/29 1:08 下午
 */

public class GoodbyeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String msg = "Goodbye!";

    public GoodbyeServlet() {
    }

    public GoodbyeServlet(String msg) {
        this.msg = msg;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>" + msg + "</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}

 

3、启动类

将 URL路径映射到对应的 Servlet

启动 jetty 内置服务器

package com.example;

/**
 * @author 言曌
 * @date 2020/2/29 1:09 下午
 */

import com.example.server.servlet.GoodbyeServlet;
import com.example.server.servlet.HelloServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class JettyWebAppApplication {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        // http://localhost:8080/hello
        context.addServlet(new ServletHolder(new HelloServlet()), "/hello");
        // http://localhost:8080/hello/kongxx
        context.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/hello/kongxx");

        // http://localhost:8080/goodbye
        context.addServlet(new ServletHolder(new GoodbyeServlet()), "/goodbye");
        // http://localhost:8080/goodbye/kongxx
        context.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/goodbye/kongxx");

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

 

然后启动该类,访问localhost:8080/hello 即可验证

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: