本篇来学习下Servlet获取配置信息,前面我们学习Servlet的生命周期方法,知道有一个getServletConfig()和一个init(servlet config),ServletConfig是一个对象,通过这个对象,我们可以获取在web.xml中的配置信息,这些信息包括变量和变量的值。获取ServletConfig对象的方法有好几种。
1.在web.xml中的servlet对象添加一个变量
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>servletDemo3</servlet-name>
<servlet-class>com.anthony.servlet.ServletDemo3</servlet-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.通过ServletConfig对象获取这变量的值
我们自定义的ServletDemo3中重写了init()方法,从init()方法拿到了服务器创建的ServletConfig对象,然后我们在doGet()方法中获取web.xml中的变量的值。
package com.anthony.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo3 extends HttpServlet {
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*****ServletDemo3********");
String encodingType = config.getInitParameter("encoding");
System.out.println(encodingType);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
重新部署到tomcat服务器上,然后浏览器打开 http://localhost:8080/Servlet01/demo3
在Eclipse的日志控制台输出
*****ServletDemo3********
UTF-8
第二种方法得到config的对象
这种方法是通过使用继承父类GenericServlet的方法得到ServletConfig对象。
package com.anthony.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*****ServletDemo3********");
String encodingType = super.getInitParameter("encoding");
System.out.println(encodingType);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
重新发布到tomcat,同样可以打印出变量的值。
第三种方法得到ServletConfig对象
第三种方法是通过我们写的子类去调用getServletConfig方法,得到config对象,然后再调用getInitParameter方法。
package com.anthony.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String encodingType = this.getServletConfig().getInitParameter("encoding");
System.out.println(encodingType);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
重新部署,测试依然得到encoding的值。以上就是三种获取ServletConfig对象然后获取web.xml中变量的值的方法。