1. Understand the ServletContext object
Represents the entire web application and can communicate with the program's container (server) .
1. Concept
ServletContext is the largest interface in Servlet, presenting the Servlet view of a web application. The server will create an object for each project, which is the ServletContext object. This object is globally unique and shared by all servlets within the project. So it is called global application shared object .
2. Get
1. Obtain through request object
request.getServletContext();
2. Obtain through HttpServlet
this.getServletContext();
3. Test
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "servletContextDemo1", value = "/servletContextDemo1")
public class servletContextDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context1 = request.getServletContext();
ServletContext context2 = this.getServletContext(); //httpServlet下的servletContext
System.out.println(context1);
System.out.println(context2);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
3. Function
1. Get the MIME type:
- MIME type: A file data type defined during Internet communication
- Format: large type/small type text/html image/jpeg
- Get: String getMimeType(String file)
@WebServlet(name = "servletContextDemo2", value = "/servletContextDemo2")
public class servletContextDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
String filename = "a.jpg";
String mimeType = context.getMimeType(filename); //获取mime类型
System.out.println(mimeType); //mime类型为 image/jpeg
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
2. Domain objects: shared data
- Set data field: setAttribute(String name,Object value)
@WebServlet(name = "servletContextDemo3", value = "/servletContextDemo3")
public class servletContextDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
context.setAttribute("msg","ohmyga"); //设置共享数据,给整个服务器共享
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
- Get the data field: getAttribute(String name)
@WebServlet(name = "servletContextDemo4", value = "/servletContextDemo4")
public class servletContextDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
Object msg = context.getAttribute("msg");//设置共享数据
System.out.println(msg);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
2. How to get the real path of the file
String getRealPath(String path);
1. Access resources under the web directory
String a = context.getRealPath("/a.txt");
System.out.println(a);
2. Resource access under the WEB-INF directory
String b = context.getRealPath("/WEB-INF/b.txt");
System.out.println(b);
3. Resource access under the src directory
String c = context.getRealPath("/WEB-INF/classes/c.txt");
System.out.println(c);
@WebServlet(name = "servletContextDemo5", value = "/servletContextDemo5")
public class 获取文件真实路径 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
String a = context.getRealPath("/WEB-INF/classes/a.txt"); //src目录下的a.txt
System.out.println(a);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
3. Implement file download
1. html file
1. Write a simple html code, such as downloading this 1.jpg file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下载</title>
</head>
<body>
<a href="/Web/servletContextDemo6?filename=1.jpg">图片下载</a>
</body>
</html>
2. Preview effect
2. web server code
Note here that you need to set the mime type of the response header and the opening method of the response header ! ! !
response.setHeader(String name, String value);
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.FileInputStream;
import java.io.IOException;
@WebServlet(name = "servletContextDemo6", value = "/servletContextDemo6")
public class 文件下载 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("filename"); //获取请求参数
//使用字节输入流加载文件进内存
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/WEB-INF/classes/servletContext/" + filename); //找到文件的服务器路径
FileInputStream fis = new FileInputStream(realPath); //使用字节输入流读取文件
//设置response响应头
String mimeType = servletContext.getMimeType(filename); //获取mime类型
response.setHeader("content-type",mimeType); //设置响应头类型
response.setHeader("content-disposition","attachment;filename=" + filename); //设置响应头打开方式
//将输入流的事件写出到输出事件
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[1024 * 8];
int length = 0;
while((length = fis.read(buffer)) != -1){
sos.write(buffer,0,length);
}
sos.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
3. Preview effect
click to download
The file is downloaded to this location by default (you can also use the browser to set it to other places).