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 ...
随机推荐
- KVM部署LVS集群故障案例一则
一.故障现象 KVM部署LVS(Linux Virtual Server)集群后,能够单独以HTTP方式访问RS(Real Server)的实际IP,但无法通过VIP(Virtual IP)访问. 二 ...
- 问题-Ctrl+F7跟踪值时提示“Function to be called, TGGLPolyhedron3d.AsString, was eliminated by linker”
问题现象:F9运行程序后,选中一个对象,Ctrl+F7跟踪值时,调用对象的某一个方法提示“Function to be called, TGGLPolyhedron3d.AsString, was e ...
- iOS中的动画(转载)
iOS中的动画 最近两天没事在慢慢学习一些动画,好多东西长时间不用都给忘了,找到一篇介绍很详细的文章就粘贴了过来以备复习,原文地址:https://my.oschina.net/aofe/blog/ ...
- ms17010漏洞复现-2003
先使用Smbtouch模块检测一下是否有漏洞. 然后使用Doublepulsar写一个shellcode到本地. 生成成功后的截图: 再使用EternalRomance植入Doublepulsar后门 ...
- ewebeditor编辑器配合IIS6.0解析漏洞拿shell
很明显这是一个ewebeditor编辑器,这个编辑器存在可遍历目录可创建文件夹等一系列漏洞.直接在url处加../即可.若要创建文件夹直接在url后面写文件夹名称即可. 上传一张shell图片,抓包改 ...
- Android开发艺术探索读书笔记——进程间通信
1. 多进程使用场景 1) 应用某些模块由于特殊需求须要执行在单独进程中. 如消息推送,使消息推送进程与应用进程能单独存活,消息推送进程不会由于应用程序进程crash而受影响. 2) 为加大一个应用可 ...
- python 反编译模块uncompyle2的使用--附破解wingide5 方法
原来一直用pycharm,无奈它常常无法使用.来訪问一些模块的属性,朋友推荐了wingide,于是去官网下载了wingide5的最新版本号,仅仅有10天的试用期,就想能否用python的uncompy ...
- 140725暑期培训.txt
1.若须要使用64位int 定义 __64int 类型 %I64d 2.Fibbonacci 数列 採用递归的方法 int F(int n) { if(n= ...
- php扩展安装
[root@129-2-10-2 src]# cat kuozhan.sh #!/bin/bash###install redis extend #########cd /usr/local/srct ...
- 除去Scala的糖衣(13) -- Default Parameter Value
欢迎关注我的新博客地址:http://cuipengfei.me/ 好久没有写博客了,上一次更新竟然是一月份. 说工作忙都是借口,咋有空看美剧呢. 这半年荒废掉博客说到底就是懒,惯性的懒惰.写博客这事 ...