ServletConfig对象和ServletContext对象的应用

一、读取配置文件信息(ServletConfig

        1.在web中配置初始化信息

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <!-- servlet的内部名称,自定义。尽量有意义 -->
        <servlet-name>ServletDmo11</servlet-name>
        <!-- servlet的类全名: 包名+简单类名 -->
        <servlet-class>com.xinhua.controller.RegisterController2</servlet-class>
        <!-- 上面的RegisterController2为你的Java文件的名称  -->
        <init-param>
            <!-- 初始参数: 这些参数会在加载web应用的时候,封装到ServletConfig对象中 -->
            <param-name>charset</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <!-- servlet的内部名称,一定要和上面的内部名称保持一致!! -->
        <servlet-name>ServletDmo11</servlet-name>
        <!-- servlet的映射路径(访问servlet的名称) -->
        <url-pattern>/start</url-pattern>
    </servlet-mapping>

</web-app>

         2.编写servlet程序

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;


public class RegisterController2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // req.setCharacterEncoding("UTF-8");
        //]resp.setContentType("text/html;charset=UTF-8");
        //读取servlet的初始参数
        String charset = this.getServletConfig().getInitParameter("charset");
        System.out.print(charset);
        //灵活编码位置,不用固定
        resp.setContentType("text/html;charset="+charset);
        //同时取多个
        Enumeration uu = this.getServletConfig().getInitParameterNames();
        while (uu.hasMoreElements()){
            String key = (String) uu.nextElement();
            String value = getInitParameter(key);
            //输出servlet的参数和对应的值
            resp.getWriter().print(value);
        }
    }
}

        3.运行发布,然后在网页导航栏输入你的Servlet的映射路径(访问路径)

  

到这里ServletConfig对象就介绍差不多了,下面是 ServletContext对象的使用

 二、ServletContext对象(代表一个web应用(域)

       1. 获得serveltContext对象

                方式一

                        request.getServletContext();

                方式二   (HttpServlet继承)

                        this.getServletContext()

                取得服务的相关信息

                        System.out.println(this.getServletContext().getServerInfo());

                获得web应用的根目录

                        System.out.println(this.getServletContext().getContextPath());

2. 利用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;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
@WebServlet("/ServletContext")
public class ServletContext extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取配置文件
        //第一种方式
           /* Properties properties = new Properties();
            properties.load(this.getServletContext().getResourceAsStream("/File/datainfor.properties"));//里面跟文件的相对路径
            String driver = (String) properties.get("url");
            System.out.print(driver);*/
        //第二种方式
        String realPath = this.getServletContext().getRealPath("/File/pro.properties");
        System.out.print(realPath);
        Properties properties = new Properties();
        properties.load(new FileInputStream(realPath));
        String driver = (String) properties.get("url");
        System.out.print(driver);
    }
}

谢谢观看,最后给大家附上运行结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值