动手学servlet(五) 共享变量
1、 无论对象的作用域如何,设置和读取共享变量的方法是一致的
-setAttribute("varName",obj);
-getAttribute("varName");
2、变量的作用域
ServletContext:范围最大,应用程序级别的,整个应用程序都能访问
HttpSession:次之,会话级别的,在当前的浏览器中都能访问
HttpServletRequest:范围最小,请求级别。请求结束,变量的作用域也结束
设置共享变量
package servletdemo; import java.io.IOException; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* Servlet implementation class SetServletVar
*/
@WebServlet("/SetServletVar")
public class SetServletVar extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public SetServletVar() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext ctx=this.getServletContext();
ctx.setAttribute("ctx_name", "ctx_value");
HttpSession session=request.getSession();
session.setAttribute("session_name", "session_value");
request.setAttribute("request_name", "request_value");
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
代码
获取共享变量(注意这是新建的一个Servlet)
package servletdemo; import java.io.IOException; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* Servlet implementation class GetServetVar
*/
@WebServlet("/GetServetVar")
public class GetServetVar extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public GetServetVar() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext ctx=this.getServletContext();
HttpSession session=request.getSession();
String ctx_value=(String)ctx.getAttribute("ctx_name");
String session_value=(String)session.getAttribute("session_name");
String request_value=(String)request.getAttribute("request_name");
System.out.println("ctx_value:"+ctx_value);
System.out.println("session_value:"+session_value);
System.out.println("request_value:"+request_value);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
代码

我们看到HttpServletRequest变量为空,验证了这个变量作用域是在请求级别的,但是如果我们在设置了变量后跳转到GetServletVar
request.getRequestDispatcher("GetServetVar").forward(request, response);那么输出的值就是"request_value";
动手学servlet(五) 共享变量的更多相关文章
- 动手学servlet(六) 过滤器和监听器
过滤器(Filter) 过滤器是在客户端和请求资源之间,起一个过滤的作用,举个例子,比如我们要请求admin文件夹下的index.jsp这个页面,那么我们可以用一个过滤器,判断登录用户是不是管理员 ...
- 动手学servlet(四) cookie和session
Cookie cookie是保存在客户端的一个“键值对”,用来存储用户的一些信息 cookie的应用: -在电子商务会话中标识用户 -对网站进行定制,比如你经常浏览哪些内容,就展示哪些页面给你 - ...
- 动手学servlet(三) 请求头和响应头信息
获取请求头信息 package servletdemo; import java.io.IOException; import java.util.Enumeration; import javax. ...
- 动手学servlet(二) servlet基础
1.我们来试着向一个servlet提交一个表单,现在webcontent下新建一个login.html页面,其中action对应servelt类名,代码如下: <!DOCTYPE HTML PU ...
- 动手学servlet(一) 第一个servlet程序
1.文件>新建>动态WEB项目 "javaeedemo">在Java Resource的src下新建包“servletdemo”,包下新建一个类“MyServet ...
- Mina、Netty、Twisted一起学(五):整合protobuf
protobuf是谷歌的Protocol Buffers的简称,用于结构化数据和字节码之间互相转换(序列化.反序列化),一般应用于网络传输,可支持多种编程语言. protobuf如何使用这里不再介绍, ...
- 从头开始学JavaScript (五)——操作符(二)
原文:从头开始学JavaScript (五)--操作符(二) 一.乘性操作符 1.乘法:* 乘法操作符的一些特殊规则: 如果操作数都是数值,按照常规的乘法计算,如果乘积超过了ECMAscri ...
- 对比《动手学深度学习》 PDF代码+《神经网络与深度学习 》PDF
随着AlphaGo与李世石大战的落幕,人工智能成为话题焦点.AlphaGo背后的工作原理"深度学习"也跳入大众的视野.什么是深度学习,什么是神经网络,为何一段程序在精密的围棋大赛中 ...
- 【动手学深度学习】Jupyter notebook中 import mxnet出错
问题描述 打开d2l-zh目录,使用jupyter notebook打开文件运行,import mxnet 出现无法导入mxnet模块的问题, 但是命令行运行是可以导入mxnet模块的. 原因: 激活 ...
随机推荐
- HttpURlconntiuon获取网络数据
package Network; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream ...
- Django 创建APP简单步骤
yum install epel-releaseyum install python34yum install python-pippip install django django-admin st ...
- Apache2 部署 Django
环境: debian8 apache2.4.10 #请注意自己的apache版本,不同版本配置文件结构差异很大 django1.10 python3.4 默认ap ...
- Spring使用ThreadLocal技术来处理这些问题
过去我习惯于从左到右的思考,因为这符合书写的习惯,对于“好”得前端工程师,我们首先可能会去思考什么是好,好的定义和范围,标准和要求?但现在我习惯于从右到左的思考,因为我觉得越是抽象越难以定义,从粒度更 ...
- Delphi常用关键字用法详解
本文详细介绍了Delphi中常用的各个关键字名称及用法,供大家在编程过程中借鉴参考之用.详情如下: absolute: ? 1 2 3 4 5 6 7 8 9 10 //它使得你能够创建一个新变量, ...
- 面试题五 数组中出现次数超过一半的数字 时间为O(n)
也就是说 该数字出现的次数比其他所有数字出现次数的和还要多. 因此可以保存两个值,一个数字,一个次数. 遍历时 1.如果数字相同,count++ 2.如果count == 0 count = 1 nu ...
- 渗透编码转码转换工具:CodeFuns
功能: 1 支持10 进制 16进制 数字的互相转换 2 对字符串进行倒叙 3 ASCII转换,反转 4 生成MSSQL的char函数ASCII字符串,反转 5 生成PHP的CHR函数ASCII字符串 ...
- 鼠标光标聚焦到可编辑div的最末尾
<p> <div id='text' contenteditable=true style='width:100px;height:100px;border:1px #ccc;'&g ...
- js中cookie的使用详细分析
JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...
- 拜拜了,浮动布局-基于display:inline-block的列表布局
原创文章,转载请注明来自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com]本文地址:http://www.zhangxinxu.com/wordpress/?p=1194