–共享变量 
? setAttribute 
? getAttribute 
–变量的作用域 
? ServletContext 
? HttpSession 
? HttpServletRequest 
–实例 
? 测试变量的作用域
######################################################
? 共享变量 
–无论对象的作用域如何,共享变量和获得变量的 
方法都是一致的 
? 共享变量 
–setAttribute(“varName”,obj); 
? 获得变量 
–getAttribute(“varName”);
? 变量的作用域 
–在Servlet中有三个作用域 
? ServletContext 
–范围最大,应用程序级别的,整个应用程序都能访问 
? HttpSession 
–次之,会话级别的,在当前的浏览器中都能访问 
? HttpServletRequest 
–范围最小,请求级别,请求结束,变量的作用域也结束
? 实例 
–测试变量的作用域
同一页面测试变量
ScopeServlet.java
package com.michael.servlet;

import java.io.IOException;    
import java.io.PrintWriter;

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;

public class ScopeServlet extends HttpServlet {

/**    
         * Constructor of the object.    
         */    
        public ScopeServlet() {    
                super();    
        }

/**    
         * Destruction of the servlet. <br>    
         */    
        public void destroy() {    
                super.destroy(); // Just puts "destroy" string in log    
                // Put your code here    
        }

/**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doGet(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {

doPost(request,response);    
        }

/**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doPost(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {    
                // 1    
                ServletContext sc = this.getServletContext();    
                sc.setAttribute("sc_name", "sc_value");    
                // 2    
                HttpSession session = request.getSession();    
                session.setAttribute("session_name", "session_value");    
                // 3    
                request.setAttribute("request_name", "request_value");    
                String sc_value = (String) sc.getAttribute("sc_name");    
                String session_value = (String) session.getAttribute("session_name");    
                String request_value = (String) request.getAttribute("request_name");

response.setContentType("text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.print(sc_value);    
                out.print(session_value);    
                out.println(request_value);    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }

/**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */    
        public void init() throws ServletException {    
                // Put your code here    
        }

}

获取同一页面变量
两页面获取变量
ScopeServlet2.java
package com.michael.servlet;

import java.io.IOException;    
import java.io.PrintWriter;

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;

public class ScopeServlet2 extends HttpServlet {

/**    
         * Constructor of the object.    
         */    
        public ScopeServlet2() {    
                super();    
        }

/**    
         * Destruction of the servlet. <br>    
         */    
        public void destroy() {    
                super.destroy(); // Just puts "destroy" string in log    
                // Put your code here    
        }

/**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doGet(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {

doPost(request,response);    
        }

/**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doPost(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {    
                ServletContext sc = this.getServletContext();    
                HttpSession session = request.getSession();    
                String sc_value = (String) sc.getAttribute("sc_name");    
                String session_value = (String) session.getAttribute("session_name");    
                String request_value = (String) request.getAttribute("request_name");

response.setContentType("text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.println(sc_value);    
                out.println(session_value);    
                out.println(request_value);    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }

/**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */    
        public void init() throws ServletException {    
                // Put your code here    
        }

}

测试第二个页面获取共享变量

打开第二个页面测试
由ScopeServlet跳转转发到ScopeServlet3页面
ScopeServlet.java
package com.michael.servlet;

import java.io.IOException;    
import java.io.PrintWriter;

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;

public class ScopeServlet extends HttpServlet {

/**    
         * Constructor of the object.    
         */    
        public ScopeServlet() {    
                super();    
        }

/**    
         * Destruction of the servlet. <br>    
         */    
        public void destroy() {    
                super.destroy(); // Just puts "destroy" string in log    
                // Put your code here    
        }

/**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doGet(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {

doPost(request,response);    
        }

/**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doPost(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {    
                // 1    
                ServletContext sc = this.getServletContext();    
                sc.setAttribute("sc_name", "sc_value");    
                // 2    
                HttpSession session = request.getSession();    
                session.setAttribute("session_name", "session_value");    
                // 3    
                request.setAttribute("request_name", "request_value");    
                request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);    
                /*    
                String sc_value = (String) sc.getAttribute("sc_name");    
                String session_value = (String) session.getAttribute("session_name");    
                String request_value = (String) request.getAttribute("request_name");

response.setContentType("text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.print(sc_value);    
                out.print(session_value);    
                out.println(request_value);    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
                **/    
        }

/**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */    
        public void init() throws ServletException {    
                // Put your code here    
        }

}

ScopeServlet3.java 
package com.michael.servlet;

import java.io.IOException;    
import java.io.PrintWriter;

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;

public class ScopeServlet3 extends HttpServlet {

/**    
         * Constructor of the object.    
         */    
        public ScopeServlet3() {    
                super();    
        }

/**    
         * Destruction of the servlet. <br>    
         */    
        public void destroy() {    
                super.destroy(); // Just puts "destroy" string in log    
                // Put your code here    
        }

/**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doGet(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {

doPost(request,response);    
        }

/**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doPost(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {

ServletContext sc = this.getServletContext();    
                HttpSession session = request.getSession();    
                String sc_value = (String) sc.getAttribute("sc_name");    
                String session_value = (String) session.getAttribute("session_name");    
                String request_value = (String) request.getAttribute("request_name");

response.setContentType("text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.println("    <h1>");    
                out.println("ScopeServlet3");    
                out.println("    </h1>");    
                out.println(sc_value);    
                out.println(session_value);    
                out.println(request_value);    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }

/**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */    
        public void init() throws ServletException {    
                // Put your code here    
        }

}

 
超链接跳转测试
ScopeServlet.java
package com.michael.servlet;

import java.io.IOException;    
import java.io.PrintWriter;

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;

public class ScopeServlet extends HttpServlet {

/**    
         * Constructor of the object.    
         */    
        public ScopeServlet() {    
                super();    
        }

/**    
         * Destruction of the servlet. <br>    
         */    
        public void destroy() {    
                super.destroy(); // Just puts "destroy" string in log    
                // Put your code here    
        }

/**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doGet(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {

doPost(request,response);    
        }

/**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */    
        public void doPost(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {    
                // 1    
                ServletContext sc = this.getServletContext();    
                sc.setAttribute("sc_name", "sc_value");    
                // 2    
                HttpSession session = request.getSession();    
                session.setAttribute("session_name", "session_value");    
                // 3    
                request.setAttribute("request_name", "request_value");    
                //request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);    
                /*String sc_value = (String) sc.getAttribute("sc_name");    
                String session_value = (String) session.getAttribute("session_name");    
                String request_value = (String) request.getAttribute("request_name");**/

response.setContentType("text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.println("    <a href=/Servlet_Scope/servlet/ScopeServlet3>");    
                out.println("    ScopeServlet3");    
                out.println("    </a>");    
                /*out.print(sc_value);    
                out.print(session_value);    
                out.println(request_value);**/    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }

/**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */    
        public void init() throws ServletException {    
                // Put your code here    
        }

}

#######################################################
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/273398,如需转载请自行联系原作者

JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域相关推荐

  1. JDBC+Servlet+JSP整合开发之30-JDBC、Servlet、JSP的MVC

    –Servlet 的优势与弊端 –JSP 的优势与弊端 –MVC 设计模式 –实例 ?使用MVC实现学生信息的添加.显示 -----------------------------START----- ...

  2. JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(上)(各种乱码、验证码、重定向和转发)

    HttpServletResponse简介 Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象 request和re ...

  3. JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试

    Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring?  Spring是分层的Java ...

  4. JavaWeb开发之CookieSession

    会话简单理解为:用户打开一个浏览器,点击多个超链接访问服务器的web资源,然后关闭浏览器,整个过程称为是一次会话.每个用户与服务器进行交互过程中,产生一些各自的数据,程序想要把这些数据进行保存,就需要 ...

  5. JAVAWEB开发之JSTL标签库的使用、 自定义EL函数、自定义标签(带属性的、带标签体的)

    JSTL  JSTL简介: JSTL的全称:JSP Standard Tag Library,JSP标准标签库 JSTL的作用:   提供给Java Web开发人员一个标准通用的标签函数库   和EL ...

  6. JavaWeb开发之Ajax省市联动无刷新分页

    我们在上一节课已经和大家讲解过Ajax的使用,大家可以去看下该篇文章,今天也主要是一个拓展延伸,使用Ajax去完成无刷新省市联动和无刷新分页. 案列:使用Ajax完成无刷新分页  数据库数据 crea ...

  7. JAVAWEB开发之JSP、EL、及会话技术(Cookie和Session)的使用详解

    Servlet的缺点 开发人员要十分熟悉JAVA 不利于页面调试和维护(修改,重新编译) 很难利用网页设计工具进行页面设计(HTML内容导入到servlet中,用PrintWriter的对象进行输出) ...

  8. JAVAWEB开发之SpringMVC详解(二)——高级开发、数据回显、参数绑定集合、图片上传、json交互、validation校验、异常处理、RESTful支持、拦截器

    知识回顾 springmvc框架 用户请求url到DispatcherServlet前端控制器,相当于中央调度器,降低系统各组件之间的耦合度. DispatcherServlet前端控制器通过Hand ...

  9. JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(下)(各种乱码、验证码、重定向和转发)

    HttpServletRequest获取请求头信息  (1)获取客户机请求头 String getHeader(String name) Enumeration<String> getHe ...

  10. JAVAWEB开发之Session的追踪创建和销毁、JSP详解(指令,标签,内置对象,动作即转发和包含)、JavaBean及内省技术以及EL表达式获取内容的使用

    Session的追踪技术 已知Session是利用cookie机制的服务器端技术,当客户端第一次访问资源时 如果调用request.getSession() 就会在服务器端创建一个由浏览器独享的ses ...

最新文章

  1. 网络应用 axIos +vue的应用
  2. 数据分析工具Pandas(3):Pandas的对齐运算
  3. WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)
  4. python3 requests模块
  5. LBWE 和SBIW的关系
  6. mysql不使用自增_自增ID有什么坏处?什么样的场景下不使用自增ID?
  7. HighNewTech之QAB:重新温读张首晟教授2018年8月演讲PPT《量子计算, 人工智能与区块链》
  8. Windows驱动开发学习笔记(五)—— SSDT HOOK
  9. MySQL binlog三种模式
  10. 阿里达摩院python_阿里达摩院出品的735集的python教程
  11. java中math方法语句,下列有关Java中标准类Math的random()方法的说法中,正确的是。 - 上学吧学历考试...
  12. Linux常出现的面试试题
  13. JTS(Geometry)工具类
  14. CentOS 6.2安装配置pacemaker
  15. 龙腾P2P流媒体点播系统商业计划书
  16. Mac下安装keras
  17. word中删除多余的空白页
  18. 一文读懂多帧超分辨率来龙去脉2019
  19. 快速排序---(面试碰到过好几次)
  20. 贪吃蛇大作战中的“马太效应”

热门文章

  1. 在设计数据库时,使用代码,对数据项调整形成数字字典(小代码)
  2. 限制文本框只能输入数字
  3. mdadm命令参数详解
  4. 删除字符串前面的0,00,000。。。
  5. 如何利用Camtasia为视频添加注释?
  6. vue-cli3的安装使用
  7. STL模板整理 priority_queue
  8. 利用js解析php的表单数据
  9. 第3/24周 区_SQL Server中管理空间的基本单位
  10. 学习Bloom Filter,处理“海量”数据