ServletConfig
代表当前Servlet在web.xml中的配置信息
在Servlet的配置文件中,可以使用一个或多个标签为servlet配置一些初始化参数。
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
这样做的好处是:如果将数据库信息、编码方式等配置信息放在web.xml中,如果以后数据库的用户名、密码改变了,则直接很方便地修改web.xml就行了,避免了直接修改源代码的麻烦。
String getServletName() – 获取当前Servlet在web.xml中配置的名字
String getInitParameter(String name) – 获取当前Servlet指定名称的初始化参数的值
Enumeration getInitParameterNames() – 获取当前Servlet所有初始化参数的名字组成的枚举
获取servlet的配置对象
方式一:通过ServletConfig对象
public class Demo12 extends HttpServlet {
// 声明成员变量
private ServletConfig config;
// 初始化方法
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
// 给成员变量赋值
this.config = config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 通过ServletConfig对象 获取servlet的配置信息
String value = this.config.getInitParameter("wanglong");
// 获取当前Servlet中配置的初始化参数(全部获取)经常用到
Enumeration<String> names = this.config.getInitParameterNames();
while(names.hasMoreElements()) {
System.out.println(names.nextElement());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
方式二:直接调用父类的方法
public class Demo01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取方式2(直接调用父类的方法)
ServletConfig config2 = this.getServletConfig();
String value = config2.getInitParameter("wanglong");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
ServletContext(域对象)
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig().getServletContext()方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
ServletContext(作用范围最大的域对象) 作用于整个工程(项目) 都能使用该对象
生命周期:当服务器启动web应用加载后创建出ServletContext对象后,域产生。当web应用被移除出容器或服务器关闭,随着web应用的销毁域销毁。
注意:所有的域对象 内部都是维护了一个map集合
所有域对象都有 setAttribute getAttribute 这两个方法
作用:
1.存值取值
2.获取全局配置信息
3.可以获取服务器上所有资源的真实路径(在服务器上的路径) getRealPath()
4.可以请求转发
ServletContext的作用
获取ServletContext对象
1.可以通过ServletConfig对象来获取
ServletContext context = this.getServletConfig().getServletContext();
// 向context对象中保存一个值
context.setAttribute("username","zmw");
2.通过父类来获取
ServletContext context = this.getServletContext();
// 获取context的配置信息
String value = context.getInitParameter("username")
// 取出context域中的值
Object object = context.getAttribute("username")
3.获取服务器上的资源路径
public class Demo04 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 读取a文件
// 参数的路径是相对于工程名来填的
String path = this.getServletContext().getRealPath("/WEB-INF/classes/a.properties");
System.out.println(path);
// 读文件
FileInputStream fis = new FileInputStream(path);
Properties properties = new Properties();
properties.load(fis);
System.out.println(properties.getProperty("key"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.请求转发
特点: 1.用户只发起了一次请求
2.用户请求时 并不知道 网站内部做了什么操作
public class Demo05 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 通过Context对象获取请求转发器
// 注意:请求转发 只能站内(本工程)转发 转发的路径相对于你的工程
// 请求转发器
RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/demo06");
// 请求转发
dispatcher.forward(request, response);
System.out.println("Demo05");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}