Javaweb-ServletContext的应用

一、ServletContext的概念

  • WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,代表当前的web应用。
  • ServletConfig对象中维护了ServletContext对象的应用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。

二、获取ServletContext的方法

package demo;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Demo1")
public class Demo1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Demo1() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到servletContext的方式1
        ServletContext context = this.getServletConfig().getServletContext();

        //得到servletContext的方式2
        context = this.getServletContext();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

三、方法应用

  • 一个WEB应用中所有Servlet共享同一个ServletContext对象,所以多个Servlet通过ServletContext对象实现数据共享
  • ServletContext对象通常被称为context域对象。
  • 获取WEB应用的初始化参数。
  • 实现Servlet的转发。
  • 利用ServletContext对象读取资源文件。

备注:
域对象:context域、request域、session域、page域


四、实例说明

1.数据共享

  • 在其中一个servlet文件中设置如下内容
String data = "abc";//abc表示要共享的数据内容,可以更改为其他
this.getServletContext().setAttribute("data", data);
  • 在其他servlet文件中实现共享获取该数据
String value = (String) this.getServletContext().getAttribute("data");//CTRL+1强转
System.out.println(value);//在myeclipse窗口验证是否取得数据,此处在实际开发中省略

2.获取web应用的初始化参数

  • 为整个web应用配置初始化参数,不同于<init-param>为某个servlet配置初始化参数:
<context-param>
    <param-name>data</param-name>
    <param-value>abc</param-value>
</context-param>
  • 获取方式:
String value = (String) this.getServletContext().getInitParameter("data");
  • 多个初始化参数获取使用迭代器,同servletconfig对象的参数获取

3.Servlet转发

  • 转发与重定向的区别,重定向要求客户机发出两次请求,转发只需要一次请求。
  • 开发中大量用到转发技术,servlet的数据通常不能直接输出,因为无法实现美观输出的需求,此时需要将数据转发到jsp上再输出。
    备注:html标签排版将数据进行美化
    ①servlet文件内容
 String data = "abc";
 //把数据带给1.jsp,不能通过context域而是request域
 ((ServletRequest) this.getServletContext).setAttribute("data",data);//把data数据存入名称为data里面
 RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/1.jsp");
 //转发到的jsp文件名
rd.forward(request, response);

②1.jsp文件内容

<body>
    <h1>
        <font color="red"> <%
    String data = (String) application.getAttribute("data");
    out.write(data);
 %>
        </font>
    </h1>
</body>

4.获取资源文件
servlet文件内容
方法1:

//读取资源文件的模板代码
        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/demo/db.properties");
        //path随文件所在路径变化
        Properties props = new Properties();
        props.load(in);

        String url = props.getProperty("url");
        String username = props.getProperty("username");
        String password = props.getProperty("password");

方法2:

//通过该方式可以直接获取资源名称
        String path = this.getServletContext().getRealPath("/WEB-INF/classes/demo/db.properties");
        String filename= path.substring(path.lastIndexOf("\\")+1);//双斜杠转义,获取最后一个\的位置

        FileInputStream in = new FileInputStream();//得到资源的绝对路径后,再通过传统方式获取资源
        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/demo/db.properties");
        Properties props = new Properties();
        props.load(in);

        String url = props.getProperty("url");
        String username = props.getProperty("username");
        String password = props.getProperty("password");

db.properties文件内容

url = jdbc:mysql://localhost:3306/test
usernaem = root
password = root

备注:用作配置资源的文件有两种:
①数据之间有关系用XML文件,例如嵌套关系;
②数据之间无关使用properties文件,例如数据库的连接关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值