ServletContext_功能_获取MIME类型与ServletContext_功能_域对象

ServletContext_功能_获取MIME类型

             1.获取MIME类型:    

                MIME类型:在互联网通信过程中定义的一种文件数据类型

                    格式:大类型/小类型     text/html   image/jpeg

                获取:String  getMimeType(String   file)

             2.域对象:共享数据  

             3.获取文件的真实(服务器)路径   

        

 

 

  

package qh.xueqiang.servletcontext;

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;
import java.io.IOException;

@WebServlet("/contextDemo02")
public class ContextDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

                    ServletContext功能
                      1.获取MIME类型:    
                MIME类型:在互联网通信过程中定义的一种文件数据类型
                    格式:大类型/小类型     text/html   image/jpeg
             2.域对象:共享数据  

             3.获取文件的真实(服务器)路径   
         */


            //2.通过HttpSevlert获取
        ServletContext context = this.getServletContext();

            //3.定义文件名称
        String  filename = "a.jpg";
            //4.获取MIME类型
        String mimeType = context.getMimeType(filename);
        System.out.println(mimeType);


    }
}

 

ServletContext_功能_域对象

      

             2.域对象:共享数据 

                1.setAttribute(String name,Object  value)

                2.getAttribute(String name)

                3.removeAttribute(String name)   

 

 

package qh.xueqiang.servletcontext;

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;
import java.io.IOException;

@WebServlet("/contextDemo03")
public class ContextDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

                    ServletContext功能
                       1.获取MIME类型:    
               2.域对象:共享数据  
               3.获取文件的真实(服务器)路径   
         */


            //2.通过HttpSevlert获取
        ServletContext context = this.getServletContext();

        //设置数据
        context.setAttribute("msg","hahahahaha");


    }
}

 

package qh.xueqiang.servletcontext;

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;
import java.io.IOException;

@WebServlet("/contextDemo04")
public class ContextDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

                    ServletContext功能
                      1.获取MIME类型:    
               
             2.域对象:共享数据  

             3.获取文件的真实(服务器)路径   
         */


            //2.通过HttpSevlert获取
        ServletContext context = this.getServletContext();

            //获取数据
        Object msg = context.getAttribute("msg");
        System.out.println(msg);


    }
}                    
                ServletContext对象范围:所有用户请求的数据

             3.获取文件的真实(服务器)路径   

package qh.xueqiang.servletcontext;

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;
import java.io.IOException;

@WebServlet("/contextDemo03")
public class ContextDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*

ServletContext功能
   1.获取MIME类型:    
              2.域对象:共享数据  
              3.获取文件的真实(服务器)路径   
*/


//2.通过HttpSevlert获取
ServletContext context = this.getServletContext();

//设置数据
context.setAttribute("msg","hahahahaha");


}
}
posted @ 2023-02-08 09:07  zj勇敢飞,xx永相随  阅读(18)  评论(0编辑  收藏  举报