JavaWeb API整理学习
来源:互联网 发布:淘宝详情页是怎么做的 编辑:程序博客网 时间:2024/06/05 21:11
1.1. ServletConfig对象说明(了解)
1:
该对象可以获取实例化对象初始化信息
初始化信息需要在web.xml配置文件中配置信息
通过该api方法 String getInitParameter(String name) 根据参数名称 获得 值
<servlet>
<servlet-name>CookieServlet</servlet-name>
<servlet-class>cn.myhome.test.CookieServlet</servlet-class>
<init-param>
<param-name>encode</param-name>
<param-value>utf-8</param-value>
</init-param>
</servlet>
2:
获取ServletConfig对象方法 参看GenericServlet源码 init(ServletConfig config ){
this.config = config;
init();
}
3: 在自定义的Servlet中 直接获取该对象 this.getServletConfig(); 对象
String encode = this.getServletConfig().getInitParameter("encode");------->utf-8
在配置了init-parm标签的servlet 代码中 通过ServletConfig 对象获取配置信息;注意并不是所有的servlet都能获取该配置信息的。它专属于配置了init-parm标签的Servlet
1.2. Servlet上下文对象 ServletContext (部分掌握)
1、ServletContext每个工程对应一个
2、获得ServletContext对象 ----- >servletConfig.getServletContext();
或者this.getServletContext();
全局初始化参数配置 (所有Servlet都可以访问)
* 和ServletConfig配置初始化参数 区分开
1) 配置全局初始化参数
当前web应用所有的组件(servlet & jsp) 都可以访问
<!-- web.xml 文件中去配置信息 全局初始化参数-->
在容器启动的时候 初始化参数就有了...
2) 获得ServletContext对象 ServletContext context =getServletContext();
3) 通过 StringgetInitParameter(String name) 获得全局参数信息
String encode = this.getServletConfig().getInitParameter("encode");
String contextPath = this.getServletContext().getContextPath(); //--------->/writeWebCode01
System.out.println(contextPath);
String url = this.getServletContext().getInitParameter("url");//------>jdbc:mysql//localhost:8080/manager
System.out.println(url);
String realPath = this.getServletContext().getRealPath("web.xml");//WEB-INF下的文件路径通过该方法获得
System.out.println(realPath);//磁盘中实际的路径
String mimeType = this.getServletContext().getMimeType(".html");//注意 点不能忘掉
System.out.println(mimeType);// text/html
ServletContext 对象可以获取服务器下所有资源的路径 getRealPath() 获取服务器绝对路径
应用: ServletContext获取服务器资源的绝对路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/b.txt");//src目录下的b.txt文件 ------>
File f= new File(realPath);System.out.println(f.exists());//true
realPath = this.getServletContext().getRealPath("/a.txt");//WebContent目录下的a.txt文件
f= new File(realPath);
System.out.println(f.exists());//true
realPath = this.getServletContext().getRealPath("/WEB-INF/mm/c.txt");//WebContent目录下mm文件夹下的c.txt文件
f= new File(realPath);
System.out.println(f.exists());//true
//工程下的d.txt文件没有发布到服务器上,所有不能访问
- JavaWeb API整理学习
- JavaWeb--学习笔记(一)--API
- JAVAWEB整理
- iScroll API 整理学习笔记
- 系统学习javaweb-10-Hibernate的配置与api操作
- Drop and Drag API学习与整理
- 4、facebook Marketing API 学习整理
- rollup学习整理-1-api详解
- JavaWeb Session问题整理
- Javaweb重点知识整理
- javaweb面试题目整理
- 【面试】JavaWeb 知识整理
- JavaWeb: 问题整理
- JavaWeb 学习
- javaweb学习
- javaweb学习
- javaWeb学习
- JavaWeb学习
- EditText中imeOptions属性使用及设置无效解决
- 17.c/c++程序员面试宝典-指针概述
- 轻松把玩HttpClient之封装HttpClient工具类(一)(现有网上分享中的最强大的工具类)
- 《Effective C++》读书笔记IV
- ios2时间类的方法
- JavaWeb API整理学习
- 关于形如--error LNK2005: xxx 已经在 msvcrtd.lib ( MSVCR90D.dll ) 中定义--的问题分析解决
- 通信技术
- 用Navicat向数据库导入数据
- /etc/security/limits.conf 控制文件描述符,进程数,栈大小等
- 采用dlopen、dlsym、dlclose加载动态链接库【总结】
- JVM和Dalvik VM的简要区别
- Android TextView中文字 颜色,超链接等效果
- Cocos2D:塔防游戏制作之旅(十三)