最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java的web.xml中context-param与init-param的区别介绍
时间:2016-04-08 编辑:简简单单 来源:一聚教程网
web.xml中
package test;
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 TestServlet extends HttpServlet {
private static final long serialVersionUID = 5222793251610509039L;
@Override
public void init() throws ServletException {
System.out.println(this.getInitParameter("name"));
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse resposne)
throws ServletException, IOException {
System.out.println(this.getInitParameter("name"));
System.out.println(this.getServletContext().getInitParameter("appversion"));
System.out.println(request.getServletContext().getInitParameter("appversion"));
}
}
上面代码的输出结果是:
yedward
yedward
1.0
1.0
启动一个WEB项目的时候,容器(如:Tomcat)会去配置文件web.xml读两个节点:
ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的键");
得到这个context-param的值之后,你就可以做一些操作了。注意,这个时候你的WEB项目还没有完全启动完成,这个动作会比所有的Servlet都要早。换句话说,这个时候,对
相关文章
- XML中的处理指令解析 09-28
- XML中的字符实体与字符数据解析 09-28
- XML中的属性学习代码展示 09-28
- XML中的文档与声明用法介绍 09-28
- 编写SVG布置画布和坐标系统以及视窗的教程 09-28
- XML中的文档与声明使用方法 09-27