javaWeb:ServletContext学习

1.ServletContext对象之获取web项目信息

在这里插入图片描述

在:服务器目录conf下的:web.xml文件里面:有

<mime-mapping>
        <extension>text</extension>  //索引
        <mime-type>text/plain</mime-type>//文本类型
    </mime-mapping>

在这里插入图片描述

创建一个类:

 ServletContext servletContext=this.getServletContext();//创建ServletContext对象
        String mineType=servletContext.getMimeType("aa.txt");//aa.text,文本类型是txt ,servletContext.getMimeType();对象的:API,返回类型
                System.out.println(mineType);
package com.example.demo16;


import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
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 HelloSerrvlet3 extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        resp.getWriter().println("helloWolrd!");
        ServletConfig config=this.getServletConfig();//创建对象ServletConfig
      //  String username=config.getInitParameter("username");//得到配置里面的值

       // String password=config.getInitParameter("password");//也得到了
    //    System.out.println(username+"  "+password);
        Enumeration<String> names=config.getInitParameterNames();//获得所有参数值
        while (names.hasMoreElements()){

            String name=names.nextElement();
            String value=config.getInitParameter(name);
            System.out.println(name+" "+value);

        }

        String servletName=config.getServletName();//获类取名称
        System.out.println(servletName);


        ServletContext servletContext=this.getServletContext();
        String mineType=servletContext.getMimeType("aa.txt");//aa.text,文本类型是txt
                System.out.println(mineType);             //就会打印出类型


    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

在这里插入图片描述

在这里插入图片描述

2.利用:ServletContext:获取公共属性

配置:公共属性web.xml中

<context-param>
    <param-name>username</param-name>
    <param-value>root</param-value>

</context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>123</param-value>

    </context-param>

在 doGet(){},增加下面代码:

String username=servletContext.getInitParameter("username");
        String password=servletContext.getInitParameter("password");

        System.out.println(username+" "+password); //获得值


        Enumeration<String> names=servletContext.getInitParameterNames();  



        while (names.hasMoreElements()) {  //遍历


            String name=names.nextElement();
             String value=config.getInitParameter(name);
             System.out.println(name+" "+value);

        }

效果:

在这里插入图片描述

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">



<context-param>
    <param-name>username</param-name>
    <param-value>root</param-value>

</context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>123</param-value>

    </context-param>




    <servlet>
        <servlet-name>demo2</servlet-name>
        <servlet-class>com.example.demo16.HelloServlet2</servlet-class>





        <load-on-startup>1</load-on-startup>


    </servlet>


<servlet-mapping>
    <servlet-name>demo2</servlet-name>
    <url-pattern>/hello</url-pattern>

</servlet-mapping>


    <servlet>
        <servlet-name>demo3</servlet-name>
        <servlet-class>com.example.demo16.HelloSerrvlet3</servlet-class>

        <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>

        <init-param>
            <param-name>password</param-name>
            <param-value>abc</param-value>
        </init-param>

    </servlet>


    <servlet-mapping>
        <servlet-name>demo3</servlet-name>
        <url-pattern>/helloo</url-pattern>
    </servlet-mapping>



</web-app>

3.ServletContext:获取读取web项目下的文件:

创建类:HelloServlet4

WEB-INF/dp.properties:(IDEA中)

要放在:WEB-INF下面:建立dp.properties文件

那么:就会在:服务器目录:有WEB-INF/dp.properties.
就可以服务器被访问到
在这里插入图片描述

 InputStream is=this.getServletContext().getResourceAsStream("WEB-INF/dp.properties");
package com.example.demo16;

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.io.InputStream;
import java.util.Properties;

public class HelloServlet4 extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);

resp.getWriter().println("helloworld");

text();

    }



    protected void text() throws IOException {

        Properties properties=new Properties();//创建文件对象
        InputStream is=this.getServletContext().getResourceAsStream("WEB-INF/dp.properties");//这里的路径,

          properties.load(is);
String driverClassName=properties.getProperty("driverClassName");
        String url=properties.getProperty("url");
      String  password=properties.getProperty("password");
        String usernane=properties.getProperty("username");

        System.out.println(driverClassName);
System.out.println(url);
System.out.println(password);
System.out.println(usernane);

    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

xml配置:

<servlet>
        <servlet-name>demo4</servlet-name>
        <servlet-class>com.example.demo16.HelloServlet4</servlet-class>

        <load-on-startup>1</load-on-startup>


    </servlet>


    <servlet-mapping>
        <servlet-name>demo4</servlet-name>
        <url-pattern>/htrt</url-pattern>

    </servlet-mapping>

效果:
在这里插入图片描述

在这里插入图片描述

4.ServletContext:ServletContext对象之作为域对象存取数据:

在:init(){}放:
this.getServletContext().setAttribute(“name”,“张三”);
在doget(){}放:

String name= (String) this.getServletContext().getAttribute(“name”);
System.out.println(name);
结果:打印出张三

另外:在其他的类也可以使用:
this.getServletContext().getAttribute(“name”);
System.out.println(name);//打印出数据

package com.example.demo16;

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.io.InputStream;
import java.util.Properties;

public class HelloServlet4 extends HttpServlet {

    @Override
    public void init() throws ServletException {
        super.init();

        this.getServletContext().setAttribute("name","张三");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);

resp.getWriter().println("helloworld");



        String name= (String) this.getServletContext().getAttribute("name");
        System.out.println(name);
//text();

    }



    protected void text() throws IOException {

        //Properties properties=new Properties();//创建文件对象
        //InputStream is=this.getServletContext().getResourceAsStream("WEB-INF/dp.properties");//这里的路径,

        //  properties.load(is);
//String driverClassName=properties.getProperty("driverClassName");
       // String url=properties.getProperty("url");
      //String  password=properties.getProperty("password");
      //  String usernane=properties.getProperty("username");

      //  System.out.println(driverClassName);
//System.out.println(url);
//System.out.println(password);
//System.out.println(usernane);



    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半浮名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值