Java topic notes ~ ServletContext

Configuration object for a single Servlet

 web.xml

<servlet>
    <servlet-name>FirstServlet</servlet-name>
    <servlet-class>com.ambow.test.FirstServlet</servlet-class>
    <init-param>
        <param-name>charset</param-name>
        <param-value>utf-8</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>FirstServlet</servlet-name>
    <url-pattern>/first01</url-pattern>
</servlet-mapping>

Get the initialization parameters of a single Servlet

ServletConfig config;

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    this.config = config;
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String charset = config.getInitParameter("charset");//获取初始化参数
    ...
}

Guess you like

Origin blog.csdn.net/qq_53142796/article/details/132213391