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 ...
随机推荐
- myeclipse中的项目 如何在项目视窗中显示setting,classpath等配置文件
导入了别人的项目,各种jar包都放好后,path也都build好了,项目也能正常启动,但是就是项目名有红叉,这是为什么呢? 网上有人说Java build path中的jar包missing了,这是一 ...
- python -- ajax数组传递和后台接收
phper转pythoner 在当初使用php做网站开发的时候,前端ajax传递数据的时候,就是直接将一个数组传递过去,后台用$_POST['key']接收即可,没有考虑那么细,想来这不都是理所当然的 ...
- oss2罗列所有文件
使用oss python sdk罗列某目录下所有文件. #!/usr/bin/python3 import sys, os import oss2 auth = oss2.Auth('keyID', ...
- docker必须要sudo,但是sudo的话,又获得不了环境变量怎么办?
方法1 sudo usermod -a -G docker $USER 方法2 sudo -E docker-compose ... 在sudo后面加上-E
- Elasticsearch Java API的基本使用
说明 在明确了ES的基本概念和使用方法后,我们来学习如何使用ES的Java API. 本文假设你已经对ES的基本概念已经有了一个比较全面的认识. 客户端 你可以用Java客户端做很多事情: 执行标准的 ...
- login流程
DirServer增加,修改-后台网页操作 维护所有分区的当前信息创建,修改,上报分区信息分区:状态,版本号,注册量等 一.loginserver定时从dir同步所有区服的信息 登陆相关 1.CmdI ...
- 简单工厂模式(Java与Kotlin版)
Kotlin基础知识的学习,请参考之前的文章: Kotlin入门第一课:从对比Java开始 Kotlin入门第二课:集合操作 Kotlin入门第三课:数据类型 初次尝试用Kotlin实现Android ...
- NodeJs使用nodejs-websocket + protobuf
参考: HTML5+NodeJs实现WebSocket即时通讯 (某人的blog) nodejs-websocket使用示例 (www.npmjs.com网站,有示例) Buffer API (nod ...
- ASP.NET Core MVC+EF Core从开发到部署
笔记本电脑装了双系统(Windows 10和Ubuntu16.04)快半年了,平时有时间就喜欢切换到Ubuntu系统下耍耍Linux,熟悉熟悉Linux命令.Shell脚本以及Linux下的各种应用的 ...
- update条件判断更新
UPDATE cw_party tp, cw_shop tsSET tp.state = 3, ts.bonus_average = CASEWHEN ts.bonus_average > 0 ...