------------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>

转载于:https://www.cnblogs.com/daimazhang/p/5364994.html

ServletContext2相关推荐

  1. ServletContext接口

    在Tomcat服务器中的Web的项目,也叫做Web应用程序,应用程序也是对象. 在JavaWeb开发中,使用ServletContext对象表示Web应用程序. 文章目录 ServletContext ...

  2. 请求和响应向更多内容

    1.请求更多方法 /*******[1]获得req对象中的头信息*(了解)*******************/Enumeration<String> headerNames = req ...

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

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

  4. spring-boot整合场景实例分析

    Spring Boot 一.简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...

  5. Javaweb后端阶段

    java web 一.XML ①.概述 概念:可扩展标记语言 可扩展:标签都是自定义的. 功能 存储数据 配置文件 在网络中传输 properties name=zhangsan age=23 gen ...

  6. Servlet-千锋-学习笔记

    视频连接 目录 1.Servlet 1.1编写servlet 1.2配置web.xml 1.3关联第三方jar包 1.4导出war包 2.HTTP协议 2.1请求报文 2.2响应报文 2.3常见状态码 ...

  7. Javaweb-Servlet总结(概述、接口使用、配置、获取请求方式、路径操作、Request对象、Response对象)

    文章目录 Servlet概述 Servlet核心接口和类 三种自定义Servlet接口的方法 1.实现Servlet接口 2.继承GenericServlet抽象类 3.继承HttpServlet抽象 ...

  8. java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象

    java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象 一.问题描述 1.在java定时任务中,使用 @Scheduled 注 ...

  9. JAVA Web学习之servlet开发

    01.servlet开发入门 动态web资源开发有两种技术:Servlet和JSP,只有把Servlet学明白了才能学JSP. Sun公司在其API中提供了一个Servlet接口,用户若想用一个动态w ...

最新文章

  1. sql 日期概念理解中的一些测试
  2. LISP 圆孔标记_lisp使用说明
  3. 中国石油管道科技研究中心2014届应届毕业生招聘(软件工程师岗)
  4. linux java 安装 gi_gi的安装和使用
  5. 如何在Java 8中使用LocalDateTime格式化/解析日期-示例教程
  6. 【Tensorflow】小白入门实战基础篇(下)
  7. ifconfig route 手动设置网卡route路由 示例
  8. c语言malloc和直接声明,问下关于malloc的声明问题
  9. 笔记︱精准营销解决方案以及营销组合评估
  10. 移动大数据时代:无线网络的挑战与机遇
  11. Flutter功能 中ListView和GridView嵌套报错?【教你一步搞定】
  12. Ubuntu 16.04配置VNC进行远程桌面连接
  13. 禁用计算机声卡设备,电脑声音被禁用了怎么办
  14. IOS移动应用跳转微信小程序
  15. python画狗的代码_简单的Python代码能狗实现哪些丧心病狂的功能
  16. plex插件显示无服务器,deepin 15.11 安装plex和插件
  17. 影响红外测温的主要因素分析
  18. 全局快门和卷帘快门(Global shutter and Rolling shutter)
  19. php环境扩展安装流程
  20. 【历史上的今天】6 月 18 日:京东诞生;网店平台 Etsy 成立;Facebook 发布 Libra 白皮书

热门文章

  1. vue商城项目开发:axios发送请求及列表数据展示
  2. jvm的生命周期:启动、执行、退出
  3. Python Django 重写delete方法实现单个对象的删除
  4. python3.8新特性 逻辑表达式_python 3.8 新特性
  5. 通过IDEA查看某个类的继承关系
  6. malloc和free的深层次分析
  7. 最优化——单纯形法,单纯形表的求取
  8. 实验4-1-10 兔子繁衍问题 (15分)
  9. linux如何捕获9的信号,2018-9-17-bash之信号捕捉
  10. linux系统python 2.6 安装pip_详解CentOS升级Python2.6到Python2.7并安装pip