Jetty8——實現Servlet 3 註解
有時候工作時,發現專案本地開發竟然使用到了jetty作為本地開發,好奇的我苦苦地終於略懂了一些用法,任何的進步都是有代價,我願在此寫下這幾天的經歷,以供後來著略參觀。
背景:Eclipse、Maven2 下的Web工程,研究物件為Servlet 3的新特性,註解,這是著重的重點(表達語句沒啥問題/han),在Jetty 8執行,為什麼是jetty8?註解@WebServlet 這是Servlet3 新特性,瀏覽了一下jetty6、7、8、9各個版本的主要區別,發現從jetty8才開始支援註解。
新建工程
工程還是Java Project 再加上Maven,在pom.xml配置;
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yd</groupId>
<artifactId >jsp-servlet</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<name>Jsp Servlet</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source >1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
**<scope>provided</scope>**
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.15.v20140411</version><!-- jetty8的上下文配置不一樣 -->
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
**<webApp>
<contextPath>/jsp</contextPath>
</webApp>**
<webAppConfig><!-- 這是8一下 -->
<contextPath>/jsp</contextPath>
</webAppConfig>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8089</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
以上是我這幾天配置的最好的,在jetty中配置啟動埠,和服務的上下文(不同版本的上下文配置不一樣),有兩個**abc**
只需要額外花點心自己琢磨,<scope>provided</scope> 這句話意味著這個jar在執行的時候只使用jetty它自己提供的這個jar,有興趣的可以瞭解下Maven的各種依賴(比如compile、test、provided、runtime、system)
這些也是程式賴以執行的關鍵之一;
接下來看看我的web.xml 配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app **<!-- metadata-complete="true" -->** >
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Create by http://blog.csdn.net/u014229282 -->
</web-app>
不知道眼亮的後讀者發現加黑地方的奧妙不,我最近才知道,
web.xml檔案中使用<metadata-complete>
元素通知Web容器是否要尋找註解,如果你將<metadata-complete>
設為false,或者在檔案中不指定元素,那麼在部署期間,容器必須掃描註解和Web分片,為Web應用程式構建有效的元資料。
如果將<metadata-complete>
設為true,將由部署描述符為Web應用程式提供所有的配置資訊。
這裡的<!-- metadata-complete="true" -->
也是關鍵之一,你設定了這個就意味著,服務在啟動時,就不會掃描這些註解,也不會使這些註解生效。
開始使用註解
**@WebListener**
public class RequestListener implements ServletRequestListener {
// 當用戶請求到達、被初始化時觸發該方法
public void requestInitialized(ServletRequestEvent sre) {
HttpServletRequest request = (HttpServletRequest) sre
.getServletRequest();
HttpSession session = request.getSession();
// 獲取session ID
String sessionId = session.getId();
// 獲取訪問的IP和正在訪問的頁面
String ip = request.getRemoteAddr();
String page = request.getRequestURI();
String user = (String) session.getAttribute("user");
/*Created by http://blog.csdn.net/u014229282*/
// 未登入使用者當遊客處理
user = (user == null) ? "遊客" : user;
System.out.println(user);
}
// 當用戶請求結束、被銷燬時觸發該方法
public void requestDestroyed(ServletRequestEvent sre) {
}
}
像平時的Maven啟動一樣,我是用Eclipse開發的。
這個只需要在Goals填clean install jetty:run
;
一個監聽器類就這樣完成了,是不是覺得太簡單了!!!
這個類就不要在web.xml 配置了,每次收到請求都會執行,雖然演示沒什麼內涵,但控制檯還會在服務端接受請求會列印“遊客”,哈哈哈哈。
總共有好多種型別javax.servlet.annotation——Servlet 3註解
比如下面的一些:
@WebServlet(name="MyServlet",urlPatterns={"/MyServlet"},initParams={})
@WebListener
@WebFilter(urlPatterns="/async")
@WebInitParam(name="1",value="1")
具體怎麼用和對應於web.xml配置都異曲同工。
注:本人程式碼及ppt已上傳,在我csdn資源庫下
May-The-Good-Luck-Be-With-You