一、读取配置文件信息(ServletConfig)
1.在web中配置初始化信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<!-- servlet的内部名称,自定义。尽量有意义 -->
<servlet-name>ServletDmo11</servlet-name>
<!-- servlet的类全名: 包名+简单类名 -->
<servlet-class>com.xinhua.controller.RegisterController2</servlet-class>
<!-- 上面的RegisterController2为你的Java文件的名称 -->
<init-param>
<!-- 初始参数: 这些参数会在加载web应用的时候,封装到ServletConfig对象中 -->
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
<servlet-name>ServletDmo11</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/start</url-pattern>
</servlet-mapping>
</web-app>
2.编写servlet程序
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
public class RegisterController2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// req.setCharacterEncoding("UTF-8");
//]resp.setContentType("text/html;charset=UTF-8");
//读取servlet的初始参数
String charset = this.getServletConfig().getInitParameter("charset");
System.out.print(charset);
//灵活编码位置,不用固定
resp.setContentType("text/html;charset="+charset);
//同时取多个
Enumeration uu = this.getServletConfig().getInitParameterNames();
while (uu.hasMoreElements()){
String key = (String) uu.nextElement();
String value = getInitParameter(key);
//输出servlet的参数和对应的值
resp.getWriter().print(value);
}
}
}
3.运行发布,然后在网页导航栏输入你的Servlet的映射路径(访问路径)
到这里ServletConfig对象就介绍差不多了,下面是 ServletContext对象的使用
二、ServletContext对象(代表一个web应用(域))
1. 获得serveltContext对象
方式一
request.getServletContext();
方式二 (HttpServlet继承)
this.getServletContext()
取得服务的相关信息
System.out.println(this.getServletContext().getServerInfo());
获得web应用的根目录
System.out.println(this.getServletContext().getContextPath());
2. 利用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;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
@WebServlet("/ServletContext")
public class ServletContext extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//读取配置文件
//第一种方式
/* Properties properties = new Properties();
properties.load(this.getServletContext().getResourceAsStream("/File/datainfor.properties"));//里面跟文件的相对路径
String driver = (String) properties.get("url");
System.out.print(driver);*/
//第二种方式
String realPath = this.getServletContext().getRealPath("/File/pro.properties");
System.out.print(realPath);
Properties properties = new Properties();
properties.load(new FileInputStream(realPath));
String driver = (String) properties.get("url");
System.out.print(driver);
}
}
谢谢观看,最后给大家附上运行结果