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

本文介绍JavaWeb开发中Servlet的变量共享方法及不同作用域的使用技巧,包括ServletContext、HttpSession和HttpServletRequest的区别与应用场景。

今天一直被这个问题困扰着,就是一个JSP页面中有两个表单form1、form2,分别对应着Servlet1、Servlet2,其中form1是上传文件的,可以在Servlet1中获得上传文件的名称等信息,由于要在form2的Servlet2中用此文件名称,要实现Servlet1和Servlet2的变量共享,一直找方法都找不到,最后采用了session来实现变量共享,其实挺简单的,参照如下文章:


原文:http://bbs.51cto.com/thread-654846-1.html


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

–共享变量 
? 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    
        }    




 
 


最近看javascript权威指南,感觉自己以前对javascript认识的太过于肤浅。有好多方面,很欠缺。比如对作用域来说。 大家都知道一个变量作用域(scope)是程序中定义这个变量的区域。全局(global)变量作用域是全局性的,在javascript中,它的存在都有定义。而在函数之内声明的变量,就只在函数体内部有定义。它们是局部(local)变量作用域是全局性的。函数的参数也是局部变量,它们只在函数体内部有定义。 在函数体内部,局部变量的优先级比同名的全局变量高。比如给一个局部变量或者函数的参数声明的名字某个全局变量名字一样的话,那么引用的就是那个局部变量或者函数的参数声明啦,间接隐藏了那个全局变量 var scope=”jquery”; function checkscope(){ var scope=”javascript”; alert(scope); } checkscope(); 上面的代码就是显示alert出javascript,之前定义的全局变量jquery有效的被隐藏。 但是如果一个函数定义嵌套在另外一个函数中,那么嵌套的函数中有声明的变量就具有嵌套的局部作用域。当然我们知道全局变量是全局对象的属性,而局部变量是一个特殊的调用对象的属性,那么我们就可以再次关注一下变量作用域的表示法,对它进行再定义。有关作用域的新描述给理解多环境下的变量提供了一种有用的方法,它为javascript的工作过程提供了一个强大的新理解。 每个javascript执行环境都有一个和它关联在一起的作用域链(scope chain).这个作用域链是一个对象列表或对象链。当javascript需要查询变量x的值时,它就开始查看该链的第一个对象。如果那个对象有一个叫x的属性,那么就采用这个属性的值。要是第一个对象没有叫x的属性,那么继续查询链中的第二个对象。如果第二个没有继续查,依次类推。 转自http://www.jqueryba.com/68.html
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值