![564ebdbf27244988fd3aa5cb349e0493.gif](https://img-blog.csdnimg.cn/img_convert/564ebdbf27244988fd3aa5cb349e0493.gif)
![3d99d83dba29e65f59fbb263230402a7.png](https://img-blog.csdnimg.cn/img_convert/3d99d83dba29e65f59fbb263230402a7.png)
IT咸鱼
微信号|mu99908888
微博|mu99908888
续前篇...
ServletConfig对象
1. Servlet的配置对象,ServletConfig对象作用域只能在一个Servlet类中使用。每个Servlet类都维护一个ServletConfig对象。它的常用方法如下:
a) public String getInitParameter (String name): 返回包含指定初始化参数的值的 String,如果参数不存在,则返回 null。
b) public java.util.Enumeration getInitParameterNames ():以 String 对象的 Enumeration 的形式返回 servlet 的初始化参数的名称,如果 servlet 没有初始化参数,则返回一个空的 Enumeration。
c) public String getServletName():返回调用该方法的Servlet的实例的名称。该Servlet实例的名称可能是服务器通过web应用web.xml来进行分配,或者没有分配的话,那么该名称就是Servlet的类名称。
d) public ServletContext getServletContext():返回一个ServletContext对象。
2. 参数的初始化的作用在于Servlet启动时需要的参数配置信息。ServletConfig的在web应用中的web.xml配置是,哪个Servlet需要进行参数初始化,就在哪个标签里配置。配置如下:
<servlet>
<servlet-name>ConfigServletDemoservlet-name>
<servlet-class>com.lsh.servletconfig.demo.ConfigServletDemoservlet-class>
<init-param>
<param-name>whoparam-name>
<param-value>Tomparam-value>
init-param>
<init-param>
<param-name>xixiparam-name>
<param-value>abcparam-value>
init-param>
servlet>
在Servlet中调用该初始化参数的代码如下:
@Overridepublic void init(ServletConfig config) throws ServletException {
//对象是Sevlet容器给的。
this.config=config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过指定初始化参数的name获得value值
String value=config.getInitParameter("who");
System.out.println(value);//打印Tom
//获得所有的初始化参数的value值
Enumeration enums=config.getInitParameterNames();while(enums.hasMoreElements()){
String name=enums.nextElement();
System.out.println(name+"::"+config.getInitParameter(name));
}//获得Servlet的名称
String serName=config.getServletName();
System.out.println(serName);//
}
}
ServletContext对象
1. ServletContext对象:
在应用在被服务器加载时就创建了一个ServletContext对象,每一个Web应用都有唯一的一个ServletContext对象。
即随着服务器的加载而出现,随着服务器的关闭而消失。
每次过程都是一个新的ServletContext对象。
2. ServletContext对象的获得:
ServletContext对象是通过ServletConfig的getServletContext方法来获得或者是通过父类的静态方法getServletContext()方法直接获得,建议使用这一种。
其实在ServletContext中是维护了一个Map对象(域对象)。
3. ServletConytext常用的方法如下
a) public Object getAttribute (String name) : 返回具有给定名称的 servlet 容器属性,如果不具有该名称的属性,则返回 null。
b) public void removeAttribute(String name) : 从 servlet 上下文中移除具有给定名称的属性。c) public void setAttribute(String name, Object object):
将对象绑定到此 servlet 上下文中的给定属性名称。
如果已将指定名称用于某个属性,则此方法将使用新属性替换具有该名称的属性。
如果值设置的是null,等同于removeAttribute()方法。
d) public String getRealPath(String path):
为给定虚拟路径返回包含实际路径的 String。
比如例如,可以通过对 "http://host/contextPath/index.html" 的请求使路径 "/index.html" 返回服务器文件系统上(即JavaWeb应用的目录层次)的绝对文件路径,其中 contextPath 是此 ServletContext 的上下文路径。
找不到则此方法返回 null。
4.在Web应用中在web.xml的根元素下配置应用的全局参数。配置内容如下:
<context-param>
<param-name>encodingparam-name>
<param-value>GBKparam-value>
context-param>
在Servlet中获得全局参数的值的代码如下:
public class ServletContextDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
//获得ServletContext对象
ServletContext context=getServletContext();
//获得web.xml中配置的全局参数的值
String value1=context.getInitParameter("encoding");
System.out.println(value1);
}
}
5.多个Servlet之间的数据共享的实现,是通过ServletConext的两个方法来实现的。数据共享即就是一个Servlet设置完数据后,另一个Servlet通过键来获得值。
首先在一个Servlet设置整个web应用的共享数据,代码如下:
public class ServletContextSetData extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
//获得ServletContext对象
ServletContext context=getServletContext();
//设置应用的共享数据
context.setAttribute("name", "lisi");
resp.getOutputStream().write("put success".getBytes());
}
}
设置完之后,我们就可以通过另一个Servlet类来获取我们需要的共享数据的值,代码如下:
public class ServletContextSetData extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
//获得ServletContext对象
ServletContext context=getServletContext();
//设置应用的共享数据
context.setAttribute("name", "lisi");
resp.getOutputStream().write("put success".getBytes());
}
}
Servlet详解(一)之基本概念
2020-10-22
![f54911f9627ce7c238f6e62844ca8392.png](https://img-blog.csdnimg.cn/img_convert/f54911f9627ce7c238f6e62844ca8392.png)
java——零基础10天教程 附完整视频+工具+面试简历模板 强力推荐
2020-10-20
![e63b0daa1203861703facd90ff468709.png](https://img-blog.csdnimg.cn/img_convert/e63b0daa1203861703facd90ff468709.png)
猿们你们开发是用的这些软件吗?必备软件。建议收藏!
2020-10-19
![edfd68c309f54c1083360a213f912f89.png](https://img-blog.csdnimg.cn/img_convert/edfd68c309f54c1083360a213f912f89.png)
脚手架——一套简单,通用的后台管理系统,建议收藏
2020-10-18
![d1eb0536102680b72a15da31dd0cf721.png](https://img-blog.csdnimg.cn/img_convert/d1eb0536102680b72a15da31dd0cf721.png)
END
如果看到这里,说明你喜欢这篇文章,请 转发、点赞。同时 标星(置顶)本公众号可以第一时间接受到博文推送。
我知道你 “在看”