basic concepts
ServletContext, Servlet context, providing access to all Servlet shared resources and capabilities of the application, is a global space to store information, a web application corresponding to a ServletContext, so the ServletContext scope is the entire application, server start that there is, the server shuts down, it was released. To save space, improve efficiency, ServletContext, we should have to put, it is important that all users need to share the security thread is some information.
ServletContext of Access:
1. Get the request object
request.getServletContext();
2. Get through HttpServlet
this.getServletContext();
When the same two ways to get to the object:
package cn.lwl.web.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;
/**获取方式
* @author liwenlong
* @data 2020/3/23
*/
@WebServlet("/servletContext1")
public class ServletContextDemo1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc1 = req.getServletContext();
ServletContext sc2 = this.getServletContext();
//输出结果为true
System.out.println(sc1 == sc2);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
The main function
- Gets the MIME type.
- Shared Objects
- Get file of the real path (server path)
Multipurpose Internet Mail Extensions MIME type. Is a way to set some type of file extension with an application to open when the extension of the file being accessed, the browser will automatically use the specified application to open. For example:
Gets the MIME type
String getMimeType(String file);
package cn.lwl.web.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;
/**功能介绍
*
* 获取MIME类型
* 共享对象
* 获取文件的真实路径(服务器路径)
*
* @author liwenlong
* @data 2020/3/23
*/
@WebServlet("/servletContext2")
public class ServletContextDemo2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext sc = req.getServletContext();
//定义文件名称
String img = "a.png";
//获取文件类型
String type = sc.getMimeType(img);
System.out.println(type);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
Sharing data
setAttribute(String name,Object value);
getAttribute(String name);
removeAttribute(String name);
Range ServletContext objects: all users can share data.
Set it to "zhangsan" in ServletContextDemo1 in.
package cn.lwl.web.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;
/**获取方式
* @author liwenlong
* @data 2020/3/23
*/
@WebServlet("/servletContext1")
public class ServletContextDemo1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc1 = req.getServletContext();
sc1.setAttribute("name","zhangsan");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
Read value "name" in the ServletContextDemo2.
package cn.lwl.web.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;
/**功能介绍
*
* 获取MIME类型
* 共享对象
* 获取文件的真实路径(服务器路径)
*
* @author liwenlong
* @data 2020/3/23
*/
@WebServlet("/servletContext2")
public class ServletContextDemo2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext sc = req.getServletContext();
String name = (String) sc.getAttribute("name");
System.out.println(name);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
Get real path
Get a real path and c two files:
package cn.lwl.web.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;
/**获取方式
* @author liwenlong
* @data 2020/3/23
*/
@WebServlet("/servletContext3")
public class ServletContextDemo3 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
String realPath = sc.getRealPath("/a.txt");
System.out.println(realPath);
String realPath2 = sc.getRealPath("/web/c.txt");
System.out.println(realPath2);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
Output: