注解主要代替在web.xml进行配的信息的
主要含有 @WebServlet
@WebListner
@WebFilter
@Resource
其中WebServlet初始化值配置的为
//@WebServlet("/fragment")//就等于在.xml文件中的配置应用内访问的名称和浏览器访问的名称
@WebServlet(
initParams= {@WebInitParam(name="name", value = "小明11111111"),@WebInitParam(name="name2", value = "小黄")},
//配置的初始值
loadOnStartup=0,
urlPatterns= {"/fragment","/text/fragment"}
)
public class FragmentGG extends HttpServlet {
代替了<param-name>test</param-name>
<param-value>123456</param-value>进行初始化值
<servlet>
<servlet-name>MySevlet2</servlet-name>
<servlet-class>com.iotek.jee.servler.MySevler2</servlet-class>
<init-param>
<param-name>test</param-name>
<param-value>123456</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
implements Filter 的监听@WebFilter
package com.ieek.ew;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter("/*")//代表所有的应用都会走这个监听(也可以写单独的页面地址)
public class TestFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("TestFilter destroy");
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("TestFilter doFilter");
arg2.doFilter(arg0, arg1);//运行的内容
System.out.println("TestFilter end");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("TestFilter init");
}
}
implements ServletContextListener
@WebListener的监听
package com.ieek.ew;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("contextDestroyed ContextListener");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("contextInitialized ContextListener");
}
}
@Resource资源注解
首先需要从web.xml中添加
<env-entry>
<env-entry-name>test</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>holle word</env-entry-value>
</env-entry>
再从Servlet中引用
private @Resource(name="test") String str;
System.out.println(str);
结果打印holle word
网友评论