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 ...
随机推荐
- JS PHP MySQL 字符长度
摘要: js的string.length 属性取的是字符串的实际长度 php的str_len()函数取的是字符串的字节长度,中文utf-8占3个字节,gb2312占2个字节 mysql中的varcha ...
- 跟着百度学PHP[13]-文件上传
PS:上传的时候一定要用POST方法,GET方法不行. 文件上传的entype要改成“mutilpart/form-data”这个编码 <html> <form action=&qu ...
- MDI多文档窗体--在一个窗体中装载多个窗体
创建MDI窗体之前,首先要明确两个概念:父窗体和子窗体,在MDI窗体中,起到容器作用的窗体被称为“父窗体”, 可放在父窗体中的其他窗体被称为子窗体,也成为MDI子窗体.当应用程序启动时,首先会显示父窗 ...
- 容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
Lengthening Sticks Problem's Link: http://codeforces.com/contest/571/problem/A Mean: 给出a,b,c,l,要求a+x ...
- 使用OpenFace进行人脸识别(1)
http://www.chinacloud.cn/show.aspx?id=25992&cid=12 本文中,笔者将介绍OpenFace中实现人脸识别的pipeline,这个pipeline可 ...
- 第二百四十六节,Bootstrap弹出框和警告框插件
Bootstrap弹出框和警告框插件 学习要点: 1.弹出框 2.警告框 本节课我们主要学习一下 Bootstrap 中的弹出框和警告框插件. 一.弹出框 弹出框即点击一个元素弹出一个包含标题和内容的 ...
- MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用 ...
- 游戏开发之coco2dx ---2d 游戏特效
http://www.cnblogs.com/gamedes/p/4547722.html
- 学习:100个高质量Java开发者博客
谷歌关键字搜索:100个高质量Java开发者博客. Java开发牛人十大必备网站.
- 用 HTML5+ payment方法支付宝支付遇到的坑
用 HTML5+ payment方法碰到的第一个坑就是如果是支付宝的话签约那种支付方式. 因为 Dcloud的文档没有更新的原因你可以看到他们说的都是‘移动支付’,但是你去支付宝平台的时候看到的根本就 ...