新建项目jetty-dcs
选择打包方式为war
在pom文件里面导入servlet包 和 jetty插件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.landau.testjetty</groupId> <artifactId>jetty-dcs</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.16.v20140903</version> <configuration> <jvmArgs>-Xms512m -Xmx512m -XX:PermSize=512m</jvmArgs> <scanIntervalSecond>10</scanIntervalSecond> <webApp> <contextPath>/jetty-dcs</contextPath> </webApp> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8080</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
新建servlet类 重写doGet()
public class JettyServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("================================="); req.setAttribute("user", new User(1,"landauNi",23)); req.getRequestDispatcher("/WEB-INF/hello.jsp").forward(req, resp); } }
在web.xml里面配置请求拦截
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>com.jetty.test.JettyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
书写简单的jsp测试页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>hello</title> </head> <body> 你好me${user.uName} </body> </html>
项目目录结构
启动方式 jetty:run
参考:
http://jingyan.baidu.com/article/d3b74d64f07e101f77e60906.html
http://blog.csdn.net/guanfl/article/details/65628494#