ServletConfig源码

 

//一个被Servlet容器使用的Servlet配置对象,在初始化时可以传递给Servlet
public interface ServletConfig {

	public String getServletName();
	
	//返回一个ServletContext的引用
	public ServletContext getServletContext();

	//返回初始化参数,如无,返回null
	public String getInitParameter(String name);

	public Enumeration getInitParameterNames();

}
public class Test extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		resp.setContentType("text/html;charset=GB2312");

		PrintWriter out = resp.getWriter();
		ServletConfig config = this.getServletConfig();
		out.println("<html><head><title>my title</title></head>");
		out.println("<body>");
		out.println(config.getServletName());
		out.println("</body></html>");

		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
    运行这个Servlet发现,返回的是Test, 即Servlet的类名。

你可能感兴趣的:(servlet)