JavaWeb-----ServletConfig对象和servletContext对象
1.ServletConfig
ServletConfig:代表当前Servlet在web.xml中的配置信息
- String getServletName() -- 获取当前Servlet在web.xml中配置的名字
- String getInitParameter(String name) -- 获取当前Servlet指定名称的初始化参数的值
- Enumeration getInitParameterNames() -- 获取当前Servlet所有初始化参数的名字组成的枚举
- ServletContext getServletContext() -- 获取代表当前web应用的ServletContext对象
在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中
配置Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Servlet_ServletConfig</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AchieveParam</servlet-name>
<servlet-class>com.servlets.AchieveParamServlet</servlet-class> <init-param>
<param-name>userName</param-name>
<param-value>jtx</param-value>
</init-param>
<init-param>
<param-name>userPwd</param-name>
<param-value>123456</param-value>
</init-param> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AchieveParam</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Servlet处理类
package com.servlets; import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* 可以实现Servlet接口
*
* @author yyx 2019年3月13日
*/
public class AchieveParamServlet extends HttpServlet {
private static final long serialVersionUID = 1L; @Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置输出内容的编码方式
response.setCharacterEncoding("UTF-8"); // 获得Session
HttpSession httpSession = request.getSession(); // 获得ServletConfig对象
ServletConfig servletConfig = this.getServletConfig();
String userName = servletConfig.getInitParameter("userName");
String userPwd = servletConfig.getInitParameter("userPwd");
httpSession.setAttribute("userName", userName);
httpSession.setAttribute("userPwd", userPwd);
/*
* getServletName()获取当前Servlet在web.xml中配置的名字
* getServletContext()获取代表当前web应用的ServletContext对象
* getInitParameterNames()获取当前Servlet所有初始化参数的名字组成的枚举
*/
System.out.println(servletConfig.getServletName());
ServletContext sContext = servletConfig.getServletContext(); Enumeration<String> enumeration = servletConfig.getInitParameterNames();
while (enumeration.hasMoreElements()) {
String name= enumeration.nextElement();
String value = servletConfig.getInitParameter(name);
System.out.println(value);
} // 转发
request.getRequestDispatcher("/success.jsp").forward(request, response);
} @Override
public void destroy() {
super.destroy();
System.out.println("Servlet销毁");
} @Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("Servlet初始化");
} }
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/achieveparamservlet.do">获取参数</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>${ userName}</h2>
<h2>${ userPwd}</h2>
</body>
</html>
2.ServletContext
ServletContext官方叫servlet上下文
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用
- 是一个域对象
- 可以读取全局配置参数
- 可以搜索当前工程目录下面的资源文件
- 可以获取当前工程名字
配置Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Servlet_ServletConfig</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>company</param-name>
<param-value>Alibaba</param-value>
</context-param>
<servlet>
<servlet-name>AchieveParam</servlet-name>
<servlet-class>com.servlets.AchieveParamServlet</servlet-class> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AchieveParam</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Servlet处理类
package com.servlets; import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* 可以实现Servlet接口
*
* @author yyx 2019年3月13日
*/
public class AchieveParamServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private int count; @Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置输出内容的编码方式
response.setCharacterEncoding("UTF-8"); // 获得Session
HttpSession httpSession = request.getSession(); // 获得ServletContext对象,读取全局参数
ServletContext servletContext = this.getServletContext();
String company = servletContext.getInitParameter("company");
httpSession.setAttribute("company", company); Enumeration<String> names = servletContext.getInitParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String value = servletContext.getInitParameter(name);
System.out.println(value);
} // web应用范围内共享数据,每调用一次方法都会加1
servletContext.setAttribute("count", count++); // 读取配置文件
InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty("driverClass"));
System.out.println(properties.getProperty("url"));
is.close(); // 获取工程名称
System.out.println(servletContext.getContextPath());
// 转发
request.getRequestDispatcher("/success.jsp").forward(request, response);
} @Override
public void destroy() {
super.destroy();
System.out.println("Servlet销毁");
} @Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("Servlet初始化");
} }
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/achieveparamservlet.do">获取参数</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>${ company}</h2>
<h2>${ count}</h2>
</body>
</html>
JavaWeb-----ServletConfig对象和servletContext对象的更多相关文章
- Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象
本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...
- 小谈-—ServletConfig对象和servletContext对象
一.servletContext概述 servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者 ...
- ServletConfig对象和ServletContext对象有什么区别?
一个Servlet对应有一个ServletConfig对象,可以用来读取初始化参数. 一个webapp对应一个ServletContext对象. ServletContext对象获取初始化定义的参数. ...
- ServletConfig对象和ServletContext对象
(1)ServletConfig:用来保存一个Servlet的配置信息的(比如 : name, class, url ... ) 这些配置信息没什么大用处,我们还可以在ServletConfig中保存 ...
- Servlet接口的实现类,路径配置映射,ServletConfig对象,ServletContext对象及web工程中文件的读取
一,Servlet接口实现类:sun公司为Servlet接口定义了两个默认的实现类,分别为:GenericServlet和HttpServlet. HttpServlet:指能够处理HTTP请求的se ...
- IT兄弟连 JavaWeb教程 ServletContext对象
ServletContext是Servlet与Servlet容器之间直接通信的接口.Servlet容器在启动一个Web应用时,会为它创建一个ServletContext对象.每个Web应用都有唯一的S ...
- 重温Servlet学习笔记--servletContext对象
一个项目中只有一个ServletContext对象,我们可以在多个servlet中获取这个唯一的对象,使用它可以给多个servlet传递数据,我们通常成servletContext为上下文对象.这个对 ...
- Java第三阶段学习(十一、Servlet基础、servlet中的方法、servlet的配置、ServletContext对象)
一.Servlet简介 1.什么是servlet: sun公司提供的一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的API动态的向 ...
- [Servlet]研究ServletContext对象
作者信息 作者姓名:金云龙 个人站点:http://www.longestory.com 个人公众帐号:搜索"longestory"或"龙哥有话说" Servl ...
随机推荐
- 简单的 FastDFS + Nginx 应用实例
版权声明:本文为GitChat作者的原创文章,未经 GitChat 同意不得转载. https://blog.csdn.net/GitChat/article/details/79479148 wx_ ...
- ROS工作空间和程序包创建
预备工作后面操作中我们将会用到ros-tutorials程序包,请先安装: $ sudo apt-get install ros-<distro>-ros-tutorials 将 < ...
- Swagger UI 与SpringMVC的整合 II
pom.xml <!-- swagger开始 --> <dependency> <groupId>io.springfox</groupId> < ...
- BarTender中如何调整数据输入表单的大小?
BarTender中的表单设计,是一个简单而又复杂的操作.简单的是它提供很多实用的工具,帮助用户实现更多的功能,复杂的是要对其进行排版设计,这就要看小伙伴们的个人要求高低了. 自定义数据输入表单时,你 ...
- [原]unity5 AssetBundle 加载
本文unity版本5.1.3 一.现有的打包教程: 1.http://liweizhaolili.blog.163.com/blog/static/16230744201541410275298/ 阿 ...
- 高并发分布式系统中生成全局唯一(订单号)Id
1.GUID数据因毫无规律可言造成索引效率低下,影响了系统的性能,那么通过组合的方式,保留GUID的10个字节,用另6个字节表示GUID生成的时间(DateTime),这样我们将时间信息与GUID组合 ...
- 使用DDL触发器同步多个数据库结构
使用DDL触发器同步多个数据库结构 背景:当开发组比较大时,势必会分布到不同的地理位置,若无法在同一个快速网络中工作,就会造成多个开发库并存的局面,这样就需要多个开发库结构的同步,甚至是开发测试数据的 ...
- echarts网络拓扑图
option = { title: { text: '' }, tooltip: {}, animationDurationUpdate: 1500, animationEasingUpdate: ' ...
- How not to alienate your reviewers, aka writing a decent rebuttal?
[forwarded from https://nebelwelt.net/blog/20180704-rebuttal.html] Assuming you have given everythin ...
- spark连数据库
DataFrame提供了一条联结所有主流数据源并自动转化为可并行处理格式的渠道,通过它Spark能取悦大数据生态链上的所有玩家,无论是善用R的数据科学家,惯用SQL的商业分析师,还是在意效率和实时性的 ...