web.html影射配置文件
<!-- web.html -->
<!-- servletDemo5 -->
<servlet>
<servlet-name>ServletDemo5</servlet-name>
<servlet-class>com.demo.ServletDemo5</servlet-class>
<!-- 配置信息 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo5</servlet-name>
<url-pattern>/demo5</url-pattern>
</servlet-mapping>
public class ServletDemo5 extends HttpServlet {
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 方式:1
String encoding = this.config.getInitParameter("encoding");
System.out.println(encoding);
// 方式:2
String encoding2 = super.getInitParameter("encoding");
System.out.println(encoding2);
// 方式:3
String encoding3 = this.getServletConfig().getInitParameter("encoding");
System.out.println(encoding3);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}