概念:代表整个web应用,可以与程序的容器进行通信
获取:
- 通过request对象获取
req.getServletContext(); - 通过HttpServlet获取
this.getServletContext();
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("demo0831")
public class ServletContextDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
ServletContext sc1 = this.getServletContext();
System.out.println(sc+"===="+sc1);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
功能:
- 获取MIME类型
@WebServlet("demo0831")
public class ServletContextDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
String filename = "b.jpg";
String mimeType = sc.getMimeType(filename);
System.out.println(mimeType);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
- 域对象:共享数据
@WebServlet("demo0831")
public class ServletContextDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
Object msg = sc.getAttribute("msg");
System.out.println(msg);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
@WebServlet("/demo083101")
public class ServletContext02 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = this.getServletContext();
sc.setAttribute("msg","lalala");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
- 获取文件的路径
@WebServlet("/demo083101")
public class ServletContext02 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = this.getServletContext();
String realPath = sc.getRealPath("/abc.txt");
System.out.println(realPath);
File file = new File(realPath);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
案例—文件下载
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
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;
@WebServlet("/demo083102")
public class DownloadDemo01 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String filename = req.getParameter("filename");
ServletContext sc = this.getServletContext();
String path = sc.getRealPath("/img/" + filename);
FileInputStream fileInputStream = new FileInputStream(path);
String mime = sc.getMimeType(filename);
resp.setHeader("content-type",mime);
String agent = req.getHeader("user=agent");
filename = DownLoadUtils.getFileName(agent,filename);
resp.setHeader("content-disposition","attachment;filename"+filename);
ServletOutputStream outputStream = resp.getOutputStream();
byte[] buff = new byte[1024*8];
int len=0;
while ((len=fileInputStream.read(buff))!=-1){
outputStream.write(buff,0,len);
}
fileInputStream.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class DownLoadUtils {
public static String getFileName(String agent,String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")){
filename = URLEncoder.encode(filename,"utf-8");
filename = filename.replace("+"," ");
}else if (agent.contains("FireFox")){
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?"+base64Encoder.encode(filename.getBytes("utf-8"))+"?=";
}else {
filename = URLEncoder.encode(filename,"utf-8");
}
return filename;
}
}