Servlet3.x
Servlet/filter/listener 支持注解实现
创建serlvet3.x项目
点击next两次
[2]创建servlet
Tips:多了@WwebServlet注解。 Web.xml中没有servlet相关的配置
[3] @WebServlet
Value: url 数组类型
@WebServlet("/hello3")
@WebServlet(value = {"/hello3","hello"})
Name : servlet的名称
loadOnStartup:配置服务器启动时创建该serlvet
initParams:配置初始化参数
[4] @WebFilter(filterName="ffilter",value={"/*"})
Tips:当使用注解实现filter时,执行顺序为类名的字母顺序。
[5] @WebListener
在实现监听器的类上方添加注解即可。
Servlet3.x对文件上传的支持。
使用注解配置一个servlet:
package com.igeek.servlet;
import java.io.IOException;
import javax.jws.soap.InitParam;
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;
/**
*
* @author www.igeehome.com
*
* TODO 使用注解配置一个servlet
*
* 2018年8月25日下午3:18:36
*/
@WebServlet(name = "helloservlet", value = { "/hello3", "/hello" }, loadOnStartup = 1, initParams = {
@WebInitParam(name = "abc", value = "def") })
public class Hello3Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Hello3Servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("execute Hello3Servlet doGet");
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}