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

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


 
获取同一页面变量
JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域
两页面获取变量
 JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域
 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    
        }    


 
测试第二个页面获取共享变量
 JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域
打开第二个页面测试
JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域
由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    
        }    


 
JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域 
 JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域
超链接跳转测试
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    
        }    


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







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

上一篇:使用有感


下一篇:iOS定义长字符串的实用宏