Servlet学习(五):ServletConfig类的作用
作者:互联网
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() {
}
}
控制台输出:
标签:ServletConfig,servlet,System,servletConfig,学习,Override,Servlet,public 来源: https://blog.csdn.net/m0_49499183/article/details/119058336