ServletConfig的作用:
1、可以获取Servlet程序的别名servlet-name的值
2、获取初始化参数init-param
3、获取ServletContext对象
完整Hello.java代码:
package demo01; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import java.io.IOException; public class Hello implements Servlet { public Hello() { } @Override public void init(ServletConfig servletConfig) throws ServletException { // ServletConfig的作用: // 1、可以获取Servlet程序的别名servlet-name的值 System.out.println("Hello的别名是"+servletConfig.getServletName()); // 2、获取初始化参数init-param System.out.println("初始化参数username的值:"+servletConfig.getInitParameter("username")); System.out.println("初始化参数url的值:"+servletConfig.getInitParameter("url")); // 3、获取ServletContext对象 System.out.println(servletConfig.getServletContext()); } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
控制台输出: