Servlet------>ServletConfig和ServletContext
原理图:

之一--------->servletConfig
有些时候,有些参数不适合写死,而且初始化servlet要用,可以通过这个头来调用servletConfig
例如:serlet数据库的配置信息,servlet采用什么码表(utf-8),servlet配置文件是哪个
在web.xml中修改成如下:

在servlet中获取:

实际开发是这样写的:

如果配置了多个init-param:

如何获得呢?

应用的例子:
struts就是一个大大的servlet,所以他的web.xml中是这样的:通过servletconfig来配置要读取的xml

之一--------->servletContext(context域对象)
首先要了解这个servletContext代表当前web应用
这个对象意味着这个web应用下的servlet可以通过他来共享信息
比如说servlet1用setAttibute存东西
servlet2用getAttibute取东西
还有很多方法可以去网上看文档,记住这个对象的含义最重要
如何调用:
ServletConfig和这个servletContext是兄弟,所以可以这样ServletConfig.getServletContext
package cn.itcaste.web.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletDemo6
*/
@WebServlet("/ServletDemo6")
public class ServletDemo6 extends HttpServlet {
//ServletContext事例
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到servletcontext方式一
this.getServletConfig().getServletContext();
//得到servletcontext方式二
this.getServletContext();
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
servletcontext细节一实现数据共享demo
启动tomcat先访问7,把data注入然后再取出来
package cn.itcaste.web.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletDemo7
*/
@WebServlet("/ServletDemo7")
public class ServletDemo7 extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data="aaaa";
this.getServletContext().setAttribute("data1", data);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
package cn.itcaste.web.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletDemo8
*/
@WebServlet("/ServletDemo8")
public class ServletDemo8 extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String value=(String) this.getServletContext().getAttribute("data1");
System.out.println(value); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
结果:控制台输出aaaa
servletcontext细节二:
在web.xml配置好web应用的初始化参数,加载web 应用会自动封装到servletcontext对象里

servletcontext细节三:servlet的转发
首先转发的含义是,你找我借钱,我没有,我去找其他人帮你(相当于发一次请求)
重定向是,你找我借钱,我没有,我告诉你谁有,你自己去找她(相当于发两次请求)
先做一个实例感受一下什么是转发,但是数据是不可以通过servletcontext传递的!!!!,servlet把数据准发给jsp,jsp来显示并且处理
ServletDemo10:
package cn.itcaste.web.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ServletDemo10
*/
@WebServlet("/ServletDemo10")
public class ServletDemo10 extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data="aaaa";
this.getServletContext().setAttribute("data", data); RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/1.jsp");
rd.forward(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>
<%
String data=(String) application.getAttribute("data");//application就是serletcontext!
out.write(data);
%>
</h1>
</body>
</html>
demo结果:

servletcontext细节四:用servletcontext读取properties文件-------1)
重点在于:InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/kms.properties");//获取properties内容。 这个地址是编译后的地址src文件夹就会消失,变成在WEB-INF文件夹中的classes里!

properties位置的几种可能:
/WEB-INF/classes/kms.properties-------- 》
/WEB-INF/classes/cn/itcaste/kms.properties------>
/kms.properties------------------------------------------>
servletcontext细节四:用servletcontext读取properties文件-------2)
传统的io流属于相对路径,是相对于java虚拟机,目前也就是tomcat里的bin文件夹,所以在用之前要取绝对路径(这个方法的好处在于可以取到资源名字)

Servlet------>ServletConfig和ServletContext的更多相关文章
- JavaWeb之Servlet: ServletConfig 与 ServletContext
ServletConfig对象 什么是ServletConfig对象 ServletConfig对象,叫Servlet配置对象.主要用于加载配置文件的初始化参数. 创建时机 ServletConfig ...
- Servlet入门和ServletConfig、ServletContext
Servlet是一门用于开发动态web资源的技术. 若想开发一个动态web资源,需要完成以下2个步骤: 1)编写一个Java类,实现servlet接口: 2)把开发好的Java类部署到web服务器中. ...
- JavaEE:Servlet简介及ServletConfig、ServletContext
Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...
- Servlet之初始化参数和传递数据(ServletConfig,ServletContext )
ServletConfig 容器初始化一个Servlet的时候,会为这个Servlet建一个唯一的Servletconfig的对象(Servlet的配置对象) 容器会从部署的描述文件(web.xml) ...
- day05 Servlet 开发和 ServletConfig 与 ServletContext 对象
day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...
- JaveWeb学习之Servlet(二):ServletConfig和ServletContext
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...
- Servlet】(2)有关Servlet实现的几个类:GenericServlet、HttpServlet、ServletConfig、ServletContext
一.GenericServlet 1.所有的成员方法: 1.在javaWeb项目中: 2.web.xml <?xml version="1.0" encoding=" ...
- Servlet、ServletConfig、ServletContext深入学习
1.Servlet学习 1.Servlet生命周期 Servlet 加载—>实例化—>服务—>销毁. init(servletConfig):(经过自己的测试发现会先调用这个而不是i ...
- JavaWeb之Servlet中ServletConfig和ServletContext
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140877.html [正文] 一.ServletConfig:代表当前 ...
- servlet ServletConfig ServletContext
ServletConfig对象 在Servlet的配置文件中,可以使用一个或者多个<init-param>标签为servlet配置一些初始化参数. 当servlet配置了初始化参数后,we ...
随机推荐
- 用JIRA管理你的项目
https://blog.csdn.net/gaowenhui2008/article/details/70241657 (一) JIRA环境搭建
- tcpdump常用参数说明
(一).学习tcpdump的5个参数 初次使用tcpdump时,使用tcpdump -h命令可以看到它有数十个参数. 根据我们在运维工作中的经验,掌握tcpdump以下5个参数即可满足大部分的工作需要 ...
- 虚拟机下安装centos7方法,修改系统语言为简体中文的方法
说明 自己装系统时一般都可以自定义选择系统语言.可是云端服务器一般都是安装好的镜像,默认系统语言为英文,对于初学者可能还会有搞不懂的计算机词汇.这里简单说一下centos7怎么修改系统语言为中文. 虚 ...
- MyBatis Geneator详解<转>
MyBatis Geneator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中文版的文档的 ...
- [Python基础]Python中remove,del和pop的区别
以a=[1,2,3] 为例,似乎使用del, remove, pop一个元素2 之后 a都是为 [1,3], 如下:http://Novell.Me >>> a=[1,2,3] &g ...
- android system.img 解压和打包
system.img重新编译的时间太长,添加和更改的文件系统内容,往往通过对system.img加压再打包的方式. 参考链接 http://blog.csdn.net/whu_zhangmin/art ...
- 【BZOJ】1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛(lis)
http://www.lydsy.com/JudgeOnline/problem.php?id=1669 水题太严重 #include <cstdio> #include <cstr ...
- .NET Entity Framework(EF)使用SqlQuery直接操作SQL查询语句或者执行过程
Entity Framework是微软出品的高级ORM框架,大多数.NET开发者对这个ORM框架应该不会陌生.本文主要罗列在.NET(ASP.NET/WINFORM)应用程序开发中使用Entity F ...
- jquery ajax 的用法
jquery的ajax请求的主要参数 beforeSend:发送ajax请求之前 success:发送ajax请求成功 error:发送ajax请求错误,通常是网络失去连接.服务器出错.后台方法错误等 ...
- 【Openwrt】刷
设定你的电脑ip 为192.168.1.100 网线一头连接lan口,另外一头连接电脑.WAN口不能插线. 按住路由器的qss 键,开启路由器的电,灯灭掉,等6秒左右灯会再次闪几下就松开,用googl ...