Jetty8——实现Servlet 3 注解

来源:互联网 发布:企业协同办公软件 编辑:程序博客网 时间:2024/06/05 03:15

有时候工作时,发现项目本地开发竟然使用到了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

原创粉丝点击