[Servlet3.0新功能]注释替代配置文件
作者信息
作者名称:金云龙
个人网站:http://www.longestory.com
个人公众帐号:搜索“longestory”或“龙哥有话说”
须要注意的是,假设使用Servlet 3.0版本号的话:首先Tomcatserver必须使用7.0版本号以上的(老版本号不提供3.0版本号)。其次Eclipse创建Webproject时选择3.0版本号。
Servlet 3.0版本号同意使用注解方式来替代web.xml文件里配置Servlet、Filter和Listener的信息,首先回顾一下web.xml文件是怎样配置Servlet、Filter和Listener的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<!-- 配置Servlet -->
<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>app.java.servlet.ServletTest</servlet-class>
<init-param>
<param-name>longestory</param-name>
<param-value>龙哥有话说</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- 配置Filter -->
<filter>
<filter-name>FilterTest</filter-name>
<filter-class>app.java.servlet.FilterTest</filter-class>
<init-param>
<param-name>longestory</param-name>
<param-value>龙哥有话说</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FilterTest</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Listener -->
<listener>
<listener-class>app.java.servlet.ListenerTest</listener-class>
</listener>
</web-app>
使用注解替代web.xml文件配置Servlet。
@WebServlet(urlPatterns="/*",initParams={@WebInitParam(name="longestory",value="龙哥有话说")},loadOnStartup=0)
public class ServletTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
}
使用注解替代web.xml文件配置Filter。
@WebFilter(urlPatterns="/*",initParams={@WebInitParam(name="longestory",value="龙哥有话说")})
public class FilterTest implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
}
@Override
public void destroy() {}
}
使用注解替代web.xml文件配置Listener。
@WebListener
public class ListenerTest implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent arg0) {}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {}
}
转载说明:请注明作者及原文链接,谢谢!
版权声明:本文博主原创文章。博客,未经同意不得转载。
[Servlet3.0新功能]注释替代配置文件的更多相关文章
- Java基础加强-(注解,动态代理,类加载器,servlet3.0新特性)
1. Annotation注解 1.1. Annotation概述 Annotation是JDK 5.0以后提供对元数据的支持,可以在编译.加载和运行时被读取,并执行相应的处理.所谓Annota ...
- Servlet3.0新特性(从注解配置到websocket编程)
Servlet3.0的出现是servlet史上最大的变革,其中的许多新特性大大的简化了web应用的开发,为广大劳苦的程序员减轻了压力,提高了web开发的效率.主要新特性有以下几个: 引入注解配置 支持 ...
- Servlet3.0新特性
1 Servlet3.0新特性概述 使用要求:MyEclipse10.0或以上版本,发布到Tomcat7.0或以上版本,创建JavaEE6.0应用! Servlete3.0的主要新特性如下三部分: 使 ...
- 【servlet3.0新特性】Annotation注解配置
servlet3.0新特性Servlet3.0引入的若干重要新特性,包括异步处理.新增的注解支持.可插性支持等等,为读者顺利向新版本过渡扫清障碍.Servlet3.0新特性概述Servlet3.0作为 ...
- 使用Servlet3.0新特性asyncSupported=true时抛异常java.lang.IllegalStateException: Not supported
最近在运用Servlet3.0新特性:异步处理功能的时候出现以下了2个问题: 运行时会抛出以下两种异常: 一月 19, 2014 3:07:07 下午 org.apache.catalina.core ...
- VS2015预览版中的C#6.0 新功能(二)
VS2015预览版中的C#6.0 新功能(一) VS2015预览版中的C#6.0 新功能(三) 自动属性的增强 只读自动属性 以前自动属性必须同时提供setter和getter方法,因而只读属性只能通 ...
- VS2015预览版中的C#6.0 新功能(三)
VS2015预览版中的C#6.0 新功能(一) VS2015预览版中的C#6.0 新功能(二) Using static 使用using StaticClass,你可以访问StaticClass类里的 ...
- VS2015预览版中的C#6.0 新功能(一)
VS2015预览版中的C#6.0 新功能(二) VS2015预览版中的C#6.0 新功能(三) VS2015的预览版在11月12日发布了,下面让我们来看看C#都提供了哪些新的功能. 字符串添写(Str ...
- REDGATE SQLPROMPT 6.0新功能
原文:REDGATE SQLPROMPT 6.0新功能 REDGATE SQLPROMPT 6.0新功能 下载地址:http://files.cnblogs.com/lyhabc/SQLPrompt6 ...
随机推荐
- hdu1495之经典搜索
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- iOS文件保存策略
Where You Should Put Your App’s Files To prevent the syncing and backup processes on iOS devices fro ...
- Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)
B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Merging a WPF application into a single EXE(WPF应用程序合并成单个Exe文件)
I always dislike handing off little applications to people. Not because I can’t, but because of the ...
- 在Laravel中一步一步创建Packages
首先要看一下Laravel官方文档,这是最新4.2的文档,假设想看中文的话点击此处,基本一样.这个github上的库setup-laravel4-package,也是一步一步介绍怎样创建一个包.并关联 ...
- hdu1542(线段树——矩形面积并)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 分析:离散化+扫描线+线段树 #pragma comment(linker,"/STA ...
- 获得树形json串
public class TreeNode { private long nodeId; private String nodeName; private long fatherNod ...
- POJ 2942 Knights of the Round Table - from lanshui_Yang
Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...
- poj 3172 Scales 搜索
其实这个题目要是注意到了题目的一点关键性的描述就会变得很简单,题意是给出的砝码是至少是前两个的和的,有了这一点,那么砝码的数量应该就在几十左右,这样的话适当剪枝的搜索是应该可以过的. #include ...
- Android获取SIM卡信息--TelephonyManager
1>获得TelephonyManager TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SER ...