WEB3.0版本之后出现了注解,为了替代web.xml配置
@WebServlet
1.使用注解的方式访问Servlet
自己写一个类Servlet,类继承HttpServlet (extends HttpServlet)<br /> 类重写方法<br /> 方法参数 抛出异常<br /> 配置 在类上 添加一个注解<br /> @WebServlet()<br /> urlPatterns={"/test","/test2"}<br /> 如果请求url只有一个的话 大括号是可以省略的<br /> value信息 等价于urlPatterns <br /> 建议用value如果只有value是可以生路的 <br /> loadOnStartUp信息 控制servlet对象的加载的(立即)<br /> initParams 为了携带信息<br /> 里面需要{@WebInitParam(name="",value="")}
package controller;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
@WebServlet(
// name = "test",
// description = "类似注释",
// displayName = "显示请求名",
// asyncSupported = true,
// value = "",
urlPatterns = {"/test",""},
loadOnStartup = 0,
initParams = {
@WebInitParam(name = "zhangsan",value = "333"),
@WebInitParam(name = "lisi",value = "444"),
@WebInitParam(name = "wangwu",value = "555"),
}
)
public class TestController extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
System.out.println("testController对象加载ing...");
Enumeration en = config.getInitParameterNames();
while (en.hasMoreElements()){
String key = (String) en.nextElement();
String value = config.getInitParameter(key);
System.out.println(key+"--"+value);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("This is a TestController,执行ing...");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
System.out.println(name+"--"+pass);
}
}
@WebFilter
2.使用注解的方式访问Filter
自己写一个类,类继承HttpFilter( extends HttpFilter )<br /> 重写方法doFilter<br /> 方法参数 request respons chain 抛异常<br /> 配置 类上添加注解<br /> @WebFilter<br /> urlPatterns<br /> value<br /> initParams 为了携带信息<br /> 里面需要{@WebInitParam(name="",value="")}
package filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
@WebFilter(
// description = "",
// displayName = "",
// value = {"","",""};
urlPatterns = {"/*",""},
initParams = {
@WebInitParam(name = "key1",value = "value1"),
@WebInitParam(name = "key2",value = "value2")
}
)
//@WebFilter("/*")
public class TestFilter extends HttpFilter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("This is a Filter,真正init方法加载ing```");
Enumeration en = filterConfig.getInitParameterNames();
while (en.hasMoreElements()){
String key = (String) en.nextElement();
String value = filterConfig.getInitParameter(key);
System.out.println(key+"--"+value);
}
}
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("This is a Filter,真正doFilter方法执行ing...");
chain.doFilter(request,response);
}
}
@WebListener
3.使用注解的方式让Listener执行
自己写一个类,实现接口 (impliments ServletRequestListener)<br /> 方法必须重写<br /> 配置 类上添加注解<br /> @WebListener
package listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
@WebListener()
public class TestRequestListener implements ServletRequestListener {
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("request对象销毁了");
}
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("request对象创建了");
}
}