zoukankan      html  css  js  c++  java
  • JavaWeb之ServletContext域对象

    ServletContext(贯穿整个WEB项目)

    代表是一个web应用的环境(上下文)对象,ServletContext对象 内部封装是该web应用的信息。

    ServletContext就是一个域对象,下边是域对象操作的函数:

      保存数据:setAttribute()

      获取数据: getAttribute()  

      删除数据: removeAttribute()  

    实例:

     servlet1

    public class servlet1 extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //1.得到域对象
            ServletContext context = this.getServletContext();
            
            //2.把数据保存到域对象中
            //context.setAttribute("name", "eric");
            context.setAttribute("student", new Student("jacky",20));
            System.out.println("保存成功");
        }
    
    }
    
    
    class Student{
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public Student(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString() {
            return "Student [age=" + age + ", name=" + name + "]";
        }
      

    servlet2

    public class servlet2 extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //1.得到域对象
            ServletContext context = this.getServletContext();
            
            //2.从域对象中取出数据
            //String name = (String)context.getAttribute("name");
            Student student = (Student)context.getAttribute("student");
            //System.out.println("name="+name);
            
            System.out.println(student);
        }
    
    }
  • 相关阅读:
    ASP.NET:关于.net中的runat
    javascript/dom:原生的JS写选项卡方法
    深入学习javascript:cookie
    javascript练习:8综合练习
    javascript/dom:对样式进行操作
    C#:form的窗体属性formborderstyle设置为none后就不能移动了
    javascript/dom:获取CSS值/getComputedStyle方法
    【玩转.Net MF – 04】远程屏幕截图
    .Net Micro Framework V4.1 beta 发布
    RFID技术在.Net Micro Framework中的应用
  • 原文地址:https://www.cnblogs.com/cgj1994/p/9851538.html
Copyright © 2011-2022 走看看