5.10作业 ConfigServlet

1 分别单独给某个Servlet 以及 ServletContext 设置配置信息并获取,截图展示结果

@WebServlet(name = "configServlet", urlPatterns = "/config",
        initParams = {@WebInitParam(name = "username", value = "admin"),
                @WebInitParam(name = "password", value = "000000")
        }
)
public class ConfigServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获取所有配置信息集合
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String s = initParameterNames.nextElement();
            System.out.println(s + "对应的值:" + servletConfig.getInitParameter(s));
        }
        //获取servletContext对象
        ServletContext servletContext = servletConfig.getServletContext();
        //获取相对路径
        String contextPath = servletContext.getContextPath();
        System.out.println("获取相对路径"+contextPath);
        //获取绝对路径
        String realPath = servletContext.getRealPath("/");
        System.out.println("获取绝对路径:"+realPath);
        //设置属性信息
        servletContext.setAttribute("password","1234");
        //根据属性名  获取属性信息
        Object password = servletContext.getAttribute("password");
        System.out.println(password);//1234
        //根据属性名  删除属性
        servletContext.removeAttribute("password");
        password = servletContext.getAttribute("password");
        System.out.println(password);//null
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

 ServletContext

 xml配置文件

<?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_4_0.xsd"
         version="4.0">
<servlet>
    <servlet-name>contextServlet</servlet-name>
    <servlet-class>com.ak.web.ContextServlet</servlet-class>
</servlet>
    <servlet-mapping>
        <servlet-name>contextServlet</servlet-name>
        <url-pattern>/context</url-pattern>
    </servlet-mapping>
<!--    配置ServletContext  参数-->
    <context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>000000</param-value>
    </context-param>
public class ContextServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取配置信息
        ServletContext context = getServletConfig().getServletContext();
        Enumeration<String> initParameterNames = context.getInitParameterNames();
        while (initParameterNames.hasMoreElements()){
            String s = initParameterNames.nextElement();
            System.out.println(s+"对应的值:"+context.getInitParameter(s));
        }
        //获取 项目路径
        String contextPath = context.getContextPath();
        System.out.println("通过上下文 获取路径:"+contextPath);
        //获取 绝对路径
        String realPath = context.getRealPath("/");
        System.out.println("获取绝对路径:"+realPath);
        //使用ServletContext  设置属性信息
        context.setAttribute("param1","value1");
        //获取配置信息
        Object param1 = context.getAttribute("param1");
        System.out.println("根据属性名获取属性值:"+param1);//value1
        //根据属性名  删除属性
        context.removeAttribute("param1");
        param1 = context.getAttribute("param1");
        System.out.println("根据属性名获取属性值:"+param1);//null
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值