zoukankan      html  css  js  c++  java
  • servlet中servletContext的五大作用(三)

    1.    获取web的上下文路径

    2.    获取全局的参数

    3.    作为域对象使用

    4.    请求转发

    5.    读取web项目的资源文件

    package day10.about_servletcontext.scope;
    
    import java.io.IOException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * 【context对象的作用-3】:域对象
     * @author mzy
     *		域对象是什么?域对象的作用?
     *		在web中我们经常需要在两个servlet之间传递信息
     * 但是通过重定向的话:
     *		通过response.sendReirect("/servlet2?name=eric");
     *		这种方式弊端很大:
     *		这种方式的本质就是通过get方式进行数据传输的
     *
     * 现在ServletContext是我们学习的第一个域对象:
     * 
     * 域对象负责到servlet中获取数据
     * 和将数据传输到指定的地方
     * 其中提供了两个方法进行这种操作: 
     * setAttribute("name", Object);
     * getAttribute("name");
     */	
    public class ContextDemo03_ScopeDemo01 extends HttpServlet {
    
    	private static final long serialVersionUID = -4001884291361193466L;
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("utf-8");
    		response.setContentType("text/html;charset=utf-8");
    		
    		ServletContext context = this.getServletContext();
    		context.setAttribute("name", "mzy");
    		System.out.println("name属性已经保存到context中...");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("utf-8");
    		response.setContentType("text/html;charset=utf-8");
    		
    	}
    }
    

    查看域对象中Attribute的servlet:

    package day10.about_servletcontext.scope;
    
    import java.io.IOException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ContextDemo03_ScopeDemo02 extends HttpServlet {
    
    	private static final long serialVersionUID = 880491464311792192L;
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("utf-8");
    		response.setContentType("text/html;charset=utf-8");
    		
    		ServletContext context = this.getServletContext();
    		String name = (String)context.getAttribute("name");
    		System.out.println(name);
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("utf-8");
    		response.setContentType("text/html;charset=utf-8");
    		
    	}
    
    }
    


  • 相关阅读:
    MongoDB CRUD操作
    mongodb的help类
    MongoDB(六):使用C#代码连接并读取MongoDB数据库
    MongoDB下载、安装、配置、使用,如何下载MongoDB数据库,MongoDB入门
    MongoDB(一):关系型数据库和非关系型数据库
    浅谈.NET中闭包
    JavaScript 对象
    python连接数据库
    个人总结
    课堂建议
  • 原文地址:https://www.cnblogs.com/mzywucai/p/11053519.html
Copyright © 2011-2022 走看看