ServletContext对象核心方法

简介:
得到web应用路径:
context.getContextPath();
request.getContextPath();  //等价于上面的代码

得到web应用参数:
context.getInitParameter("name");
context.getInitParameterNames();

域对象:
context.setAttribute("name",Object): 保存数据
context.getAttribute("name")   得到数据
context.removeAttribue("name")  清除数据

转发:
context.getRequestDispatcher  ("路径").forward(request,  
response);
//等价于上面的代码
request.getRequestDispacher("路径").forward  
(request,response);  

得到web应用中的资源文件
context.getRealPath("路径")
context.getResourceAsStream("路径");

得到web应用路径:

应用:请求重定向。

ServletContext context = this.getServletContext();

System.out.println(context.getContextPath());
//请求重定向
response.sendRedirect(context.getContextPath()  
+"/NewFile.html");

输出:

/saa

并且页面跳转到NewFile.html


得到web应用参数:

//得到SErvletContext对象
ServletContext context = this.getServletContext();
        //效果和上面一样
ServletContext context = this.getServletConfig()  
.getServletContext();
        
System.out.println("参数"+context.getInitParameter("AAA"));
        
Enumeration<String> enums = context.getInitParameterNames();
while(enums.hasMoreElements()){
    String paramName = enums.nextElement();
    String paramValue  =context.getInitParameter(paramName);
    System.out.println(paramName+"="+paramValue);

域对象:

设置数据:

public class ContextDemo3 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 + "]";
    }
    
}

获取数据:

public class ContextDemo4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.得到域对象
        ServletContext context = this.getServletContext();
        
        //2.从域对象中取出数据
        Student student = (Student)context.getAttribute("student");
        
        System.out.println(student);
    }

}

转发

ServletContext context = this.getServletContext();    
context.getRequestDispatcher("/NewFile.html").   
forward(request,response);

转发和重定向区别:

转发

 a)地址栏不会改变
 b)转发只能转发到当前web应用内的资源
 c)可以在转发过程中,可以把数据保存到request域对象中

2)重定向

a)地址栏会改变,变成重定向到地址。
b)重定向可以跳转到当前web应用,或其他web应用,甚至是外部域名网站。
c)不能再重定向的过程,把数据保存到request中。

结论: 如果要使用request域对象进行数据共享,只能用转发技术。


得到web应用中的资源文件:

getRealPath读取,返回资源文件的绝对路径:

String path = this.getServletContext().getRealPath  
("/WEB-INF/classes/db.properties");
        File file = new File(path);
        FileInputStream in = new FileInputStream(file);

getResourceAsStream() 得到资源文件,返回的是输入流:

InputStream in = this.getServletContext().  
getResourceAsStream("/WEB-INF/classes/db.properties");
目录
相关文章
|
6月前
|
存储 对象存储
07JavaWeb基础 - ServletContext对象
07JavaWeb基础 - ServletContext对象
20 0
|
11月前
|
应用服务中间件 容器
ServletConfig、ServletContext概述、配置及ServletContext的三个作用
ServletConfig、ServletContext概述、配置及ServletContext的三个作用
117 0
Servlet学习(六):ServletContext对象的作用
Servlet学习(六):ServletContext对象的作用
Servlet学习(六):ServletContext对象的作用
SpringMVC学习(五):向request域对象共享数据的五种方法
SpringMVC学习(五):向request域对象共享数据的五种方法
101 0
SpringMVC学习(五):向request域对象共享数据的五种方法
Servlet——ServletContext对象功能
Servlet——ServletContext对象功能
73 0
|
存储 应用服务中间件
ServletContext接口实现数据共享
ServletContext接口实现数据共享
136 0
ServletContext接口实现数据共享
|
数据处理 开发者
Servlet 相关类之 ServletRequest 和 Servletresponse 对象的简单介绍 | 学习笔记
快速学习 Servlet 相关类之 ServletRequest 和 Servletresponse 对象的简单介绍。
Servlet 相关类之 ServletRequest 和 Servletresponse 对象的简单介绍 | 学习笔记
ServletContext对象的生命周期监听器
ServletContext对象的生命周期监听器的理解