A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 大山哥哥 于 2018-2-1 19:34 编辑

Servlet中的四大域对象
    1.ServletContext域对象
    2.Seesion域对象
    3.Request域对象
    4.PageContext域对象

ServletContext是一个域对象.
    * 作用范围:整个web工程.
    * 创建:服务器启动的时候,tomcat服务器为每个web项目创建一个单独ServletContext对象.
    * 销毁:服务器关闭的时候,或者项目从服务器中移除的时候.


ServletContext的作用:
    1.作为域对象来存取数据
    2.用来读取web项目下文件
    3.初始化全局参数
    4.用来获得文件的MIME类型


1. ServletContext作为域对象存取数据

方法:
    1.设置全局属性的值
        void setAttribute(String name, Object object);
   
    2.获取相应属性的值
        Object getAttribute(String name);

[Java] 纯文本查看 复制代码
public class HttpServletContextDemo extends HttpServlet{
        private static final long serialVersionUID = -7168725846977975610L;
        
        
        @Override
        public void init() throws ServletException {
                int count = 0;
                //----------------------------------------------------
                //设置全局参数count
                this.getServletContext().setAttribute("count", count);
                //----------------------------------------------------
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                doPost(req,resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                //中文乱码
                resp.setContentType("text/html;charset=UTF-8");
                //----------------------------------------------------
                //获取全局参数count
                int count = (int) this.getServletContext().getAttribute("count");
                //----------------------------------------------------
                count++;
                this.getServletContext().setAttribute("count", count);
                resp.getWriter().println("您是第"+count+"个访问者");
        }
}


2. ServletContext读取项目下文件

方法:
    1.根据提供路径读取文件返回一个文件的输入流.
        InputStream is = getResourceAsStream(String path);
    2.返回一个文件的磁盘绝对路径
        String path = getRealPath(String path);

[Java] 纯文本查看 复制代码
public class HttpServletContextDemo extends HttpServlet{
        private static final long serialVersionUID = -7168725846977975610L;        
        
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                doPost(req,resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                //中文乱码
                resp.setContentType("text/html;charset=UTF-8");
                ServletContext context = this.getServletContext();
                //----------------------------------------------------
                //读取配置文件
                InputStream is = context.getResourceAsStream("WEB-INF/classes/db.properties");
                //----------------------------------------------------
                Properties p = new Properties();
                p.load(is);
                System.out.println(p);
                //----------------------------------------------------
                //获取配置文件所在得绝对路径
                String path = context.getRealPath("WEB-INF/classes/db.properties");
                //----------------------------------------------------
                System.out.println(path);
        
        }
}


3. ServletContext初始化全局参数

方法:
    1.获取全局初始化参数的值
        String getInitParameter(String name);
   
    2.获取全局所有初始化参数的名字组成的枚举
        Enumeration getInitParameterNames();

[Java] 纯文本查看 复制代码
public class HttpServletContextDemo extends HttpServlet{
        private static final long serialVersionUID = -7168725846977975610L;        

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                doPost(req,resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                //中文乱码
                resp.setContentType("text/html;charset=UTF-8");
                ServletContext context = getServletContext();
                //----------------------------------------------------
                //获取全局指定初始化参数
                String user = context.getInitParameter("user");
                System.out.println(user);
                //----------------------------------------------------
                //----------------------------------------------------
                //获取全局所有初始化参数
                Enumeration enumration = context.getInitParameterNames();
                while(enumration.hasMoreElements()){
                        String name = (String) enumration.nextElement();
                        String value = context.getInitParameter(name);
                        System.out.println(name+":"+value);
                }
                //----------------------------------------------------
        }
}


在web.xml中配置全局参数

[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee [url=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
        id="WebApp_ID" version="2.5">
        <display-name>ServletDemo</display-name>
        <welcome-file-list>
                <welcome-file>index.html</welcome-file>
        </welcome-file-list>
        ---------------------------------------------------
        <context-param>
                <param-name>user</param-name>
                <param-value>b3a4a</param-value>
        </context-param>
        <context-param>
                <param-name>pass</param-name>
                <param-value>123456</param-value>
        </context-param>
        ---------------------------------------------------
        <!-- ServletContext演示 -->
                <servlet>
                <servlet-name>HttpServletContextDemo</servlet-name>
                <servlet-class>com.b3a4a.servlet.HttpServletContextDemo</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>HttpServletContextDemo</servlet-name>
                <url-pattern>/servletContext</url-pattern>
        </servlet-mapping>
</web-app>


4. ServletContext用来获得文件的MIME类型

方法:
    1.获取文件的MimeType类型
        String getMimeType(String path);

[Java] 纯文本查看 复制代码
public class HttpServletContextDemo extends HttpServlet{
        private static final long serialVersionUID = -7168725846977975610L;
        
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                doPost(req,resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                //中文乱码
                resp.setContentType("text/html;charset=UTF-8");
                //----------------------------------------------------
                //获取文件的MimeType类型
                String mimeType = this.getServletContext().getMimeType("html/success.html");
                System.out.println(mimeType);
                //----------------------------------------------------
        }

}

2 个回复

倒序浏览

回帖奖励 +1

我来占层楼啊   
回复 使用道具 举报
还没写完…加油么么哒
来自宇宙超级黑马专属安卓客户端来自宇宙超级黑马专属安卓客户端
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马