------------ContextServlet.java--------------节选--

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  request.setCharacterEncoding("GB2312");
  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  ServletContext context = getServletConfig().getServletContext();
  //获得工程相关信息
  out.println("获得了工程的相对路径:"+context.getRealPath("/")+"<br>");
  out.println("获取了Tomcat的信息: "+context.getServerInfo()+"<br>");
  out.println("获取了JNDI:"+context.getResource("/")+"<br>");
  out.println("获取了源程序的相对路径:"+context.getResourcePaths("/")+"<br>");
  //getInitParameter方法
  String date = context.getInitParameter("date");
  String myclass = context.getInitParameter("class");
  out.println("date is -- "+date+"<br>");
  out.println("class is -- "+myclass+"<br>");
  //集合方法
  Enumeration e = context.getInitParameterNames();
  while(e.hasMoreElements()){
   String pName=(String) e.nextElement();
   String pValue=context.getInitParameter(pName);
   out.println("<br><b>"+pName+"</b>&nbsp;");
   out.println("----- "+pValue+"<br>");
  }
 }

-------------------web.xml----------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test4-03</display-name>
  <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>
  <context-param>
   <param-name>date</param-name>
   <param-value>200</param-value>
  </context-param>
  <context-param>
   <param-name>class</param-name>
   <param-value>102</param-value>
  </context-param>
  <servlet>
   <description>This is the description of my J2EE component</description>
   <display-name>This is the display name of my J2EE component</display-name>
   <servlet-name>ContextServlet</servlet-name>
   <servlet-class>com.ContextServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>ContextServlet</servlet-name>
   <url-pattern>/servlet/ContextServlet</url-pattern>
  </servlet-mapping>
</web-app>

ServletContext2的更多相关文章

  1. 重温Servlet学习笔记--servletContext对象

    一个项目中只有一个ServletContext对象,我们可以在多个servlet中获取这个唯一的对象,使用它可以给多个servlet传递数据,我们通常成servletContext为上下文对象.这个对 ...

  2. ServletContext

    1.为什么需要servletContext    需求1 需求2 --------------->解决之道servletContext     servletContext 1.ServletC ...

  3. day18(javaEE三大组件之一servlet(简介(一)))

    Servlet servlet是小型服务器语言,使用它可以处理前台传递来的信息,servlet进行处理后在响应给前台,其中servlet起到了关键性的作用.前端输入的信息可以持久化的存储在数据库中,并 ...

  4. Servlet、ServletConfig、ServletContext深入学习

    1.Servlet学习 1.Servlet生命周期 Servlet 加载—>实例化—>服务—>销毁. init(servletConfig):(经过自己的测试发现会先调用这个而不是i ...

  5. servlet(一):从Sevlet到HttpServlet

    Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层. servlet ...

  6. Web开发之Servlet

    当一个请求到达服务端,服务器怎么处理? 当一个请求到达服务端时,由服务端的引擎来进行分析.它根据工程名找到工程, 然后拿到URL的资源地址和web.XML文件的所有的进行对比,和哪一个对比上就找到了具 ...

随机推荐

  1. asp.net读excle的数据类型不统一取出空值问题

    如果表格里某列全是数字或是字符没问题,但如果混合了全是数字和部分字符就会有部分读取为空连接EXCEL方式如下 string strConn = "Provider=Microsoft.Jet ...

  2. jQuery的datatable怎么才能给某一列添加超链接?

    aocolumeDef.这个里面去定义.return返回的字符串会代替原来cell里面的内容 e.g: datatable=$('#dt_basic').dataTable({ "bAuto ...

  3. Mysqlbackup 备份详解(mysql官方备份工具)

    A.1全库备份. 命令: mysqlbackup --defaults-file=/home/mysql-server/mysql3/my.cnf  --user=root --password=ro ...

  4. java之URL类

    Java的网络类可以让你通过网络或者远程连接来实现应用.而且,这个平台现在已经可 以对国际互联网以及URL资源进行访问了.Java的URL类可以让访问网络资源就像是访问你本地的文件夹一样方便快捷.我们 ...

  5. 头痛的ASCII和preg_replace()

    说这个之前,大家先看下这条语句: preg_replace("/\<\?\=(\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\[\]\"\'\$\x7f- ...

  6. 【转】让apache支持中文路径或者中文文件

    本帖最后由 狂人阿川 于 2013-4-12 19:13 编辑 今天在给一美国VPS客户调试他的程序的时候.发现他的网站有中文名称.貌似apache无法认识中文路径,火狐下面能下载他的文件,IE下面不 ...

  7. git push冲突解决

    1. 首先,可以试图用git push origin branch-name推送自己的修改:2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并:如果git pull提示 ...

  8. POJ 1042 Gone Fishing

    题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的 ...

  9. ArcMap自定义脚本工具制作

    原文 ArcMap自定义脚本工具制作 在制图的前期,一般需要做一些数据的整理,如图层合并.裁剪等工作.虽然在ArcMap中也有提供对应的工具,但使用起来需要点技巧.如批量裁剪,虽然可以实现,但出来的结 ...

  10. ubuntu下安装selenium2.0 环境

    参考:http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html ubuntu 安装过程: 1.安装:setuptools $ apt-ge ...