ServletContext对象介绍
来源:互联网 发布:两点最短路线 算法 编辑:程序博客网 时间:2024/06/06 05:37
ServletContext对象介绍
/** * ServletContext对象介绍 * * */@WebServlet("/servlet/context")public class TestServletContextextends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {/** * 拿到ServletContext对象有四种方法 * * *///1.父类支持拿到ServletContext对象ServletContext app1 = super.getServletContext();//2.通过ServletConfig来拿到ServletContext对象ServletContext app2 = super.getServletConfig().getServletContext();//3.从请求对象request中拿到ServletContext对象 新版本(Tomcat)之后才支持javax.servlet.ServletContext app3 = req.getServletContext();//4.从session对象中拿到ServletContext对象 支持以前的版本ServletContext app4 = req.getSession().getServletContext();/** * 测试ServletContext中重要方法 * *///拿到上下文路径 System.out.println(app1.getContextPath());//拿到这个项目的真实路径(作用:以后做上传将文件保存在当前的项目中)System.out.println(app1.getRealPath("/"));//application中科院拿到全局的初始化参数(需要在web.xml中配置全局参数)System.out.println(app1.getInitParameter("encoding"));}}
application中科院拿到全局的初始化参数(需要在web.xml中配置全局参数)web.xml文件如下:
<!-- 全局的参数配置 --> <context-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </context-param>
重点:
req.getParameter(" ")和req.getAttribute(" ")的区别?
req.getParameter :(浏览器)用户传过来的
req.getAttribute :从作用域里拿的(必需是放到作用域里的)
ServletConfig.getInitParameter 和 application.getInitParameter 有什么区别?
ServletConfig.getInitParameter :只作用于某一个Servlet,查看一个servlet中配置的初始化信息
在web.xml中配置如下:
<servlet> <servlet-name></servlet-name> <servlet-class></servlet-class> <init-param> <param-name>email</param-name> <param-value>xxxx@qq.com</param-value> </init-param> </servlet>
application.getInitParameter :表示整个应用都可以拿到 只是配置的方式不同
在web.xml中配置如下:
<!-- 全局的参数配置 --> <context-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </context-param>
阅读全文
0 0
- ServletContext对象介绍
- ServletContext对象介绍
- ServletContext介绍
- ServletContext对象:
- servletContext对象
- servletContext对象
- servletContext对象
- serVletContext对象
- ServletContext对象
- servletContext对象
- ServletContext对象
- ServletContext对象
- ServletContext对象
- ServletContext对象
- ServletContext对象
- servletContext对象
- ServletContext对象
- ServletContext对象
- SpringBoot开发详解(十一) -- Redis在SpringBoot中的整合
- ubuntu16.04 kubeadm快速搭建kubernetes环境
- ZooKeeper系列(一)
- DB2教程
- 计算字符串的长度及sizeof与strlen、gets与scanf、字符数组与字符串
- ServletContext对象介绍
- java刷题之查漏补缺(三)
- LINQ系列:Linq to Object元素操作符
- JAVA 攻城狮 第三天
- 数组中重复的数字
- 《机器学习》学习笔记四 神经网络
- 鸟哥Linux学习之——VIM基本操作(1)
- 华大鑫盛ee-boss全线升级 助力中小企业转型迈向中国智造
- python中Socket的使用