Servlet3.0 Application, doGet,doPost

package com.lee;

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

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="get-application",urlPatterns={"/get-application"})
public class GetApplication extends HttpServlet{
	
//doGet 和  doPost 都交给 serivce 方法来执行
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.println("<html><head><title>");
		out.println("测试application");
		out.println("</title></head><body>");
		//Servlet 中没有 application  只有通过 servletContext 来获取 application在中的
		ServletContext sc=getServletConfig().getServletContext();
		out.println("application中当前的counter1值为");
		out.println(sc.getAttribute("counter1"));
		out.println("</body></html>");
	}

}



你可能感兴趣的:(application)