Maven+Jetty+Servlet 简单示例

avatar 2020年02月29日14:23:31 6 4251 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此
一个朋友让我帮他写一个基于 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 即可验证
历史上的今天
二月
29日
  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0