web服务器把配置信息封装到config对象中,提供一些信息
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>Servlet7</servlet-name>
<servlet-class>hyl.Servlet7</servlet-class>
<init-param>
<param-name>data</param-name>
<param-value>xx</param-value>
</init-param>
<init-param>
<param-name>dat</param-name>
<param-value>xx0</param-value>
</init-param>
<init-param>
<param-name>da</param-name>
<param-value>xx1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Servlet7</servlet-name>
<url-pattern>/Servlet7</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Servlet
package hyl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
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 Servlet7 extends HttpServlet {
//在实际的开发中,有一些东西不适合在servlet中写死,这类数据就可以通过配置文件来配给servlet
//servlet采用哪个码表,servlet链接哪个库,使用哪个配置文件
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//这是得到单个的信息
String value = this.getServletConfig().getInitParameter("data");//直接调用这个Servlet对象的getconfig方法得到封装数据因为父类已经写好了这个方法无需重写
System.out.println(value);
//得到所有的信息
Enumeration e = this.getServletConfig().getInitParameterNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
String value1 = this.getServletConfig().getInitParameter(name);
System.out.println(name+"="+value1);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>Servlet7</servlet-name>
<servlet-class>hyl.Servlet7</servlet-class>
<init-param>
<param-name>data</param-name>
<param-value>xx</param-value>
</init-param>
<init-param>
<param-name>dat</param-name>
<param-value>xx0</param-value>
</init-param>
<init-param>
<param-name>da</param-name>
<param-value>xx1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Servlet7</servlet-name>
<url-pattern>/Servlet7</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Servlet
package hyl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
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 Servlet7 extends HttpServlet {
//在实际的开发中,有一些东西不适合在servlet中写死,这类数据就可以通过配置文件来配给servlet
//servlet采用哪个码表,servlet链接哪个库,使用哪个配置文件
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//这是得到单个的信息
String value = this.getServletConfig().getInitParameter("data");//直接调用这个Servlet对象的getconfig方法得到封装数据因为父类已经写好了这个方法无需重写
System.out.println(value);
//得到所有的信息
Enumeration e = this.getServletConfig().getInitParameterNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
String value1 = this.getServletConfig().getInitParameter(name);
System.out.println(name+"="+value1);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}