如何从context-param获取参数?
从context-param获取:
马克-to-win:用context-param存放的参数,本个web应用中的任何servlet,jsp都可以获得。
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
例:1.3.1
ServletHello1.java:
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHello1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext servletContext = getServletContext();
String jd = servletContext.getInitParameter("zhangsan");
System.out.println(jd);
}
}
web.xml中,在任何“servlet标签对儿”的外面加入(因为context参数不属于任何特定的servlet,属于整个web应用):
<context-param>
<param-name>zhangsan</param-name>
<param-value>1000</param-value>
</context-param>
运行Servlet后,console中输出结果:
1000