servlet3.0 使用注解

在servlet3.0中可以使用注解方式来简化web.xml中对Servlet的配置,当然不仅仅是对Servlet进行配置,还可以对Filter,Listener。        在servlet3.0中同样也可以使用web.xml,在<web-app>标签下有一属性metadata-complete="true"在设置true时,Servlet中的注解会失效,默认值为false。

访问方式:

localhost:8080/07demo/writeCookie

/package cn.nercel.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/writeCookie")
public class WriteCookie extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String myCookie=request.getParameter("myCookie");
		System.out.println(myCookie);
		response.addCookie(new Cookie("myCookie", myCookie));
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}


你可能感兴趣的:(servlet3.0 使用注解)