1. Concept
Representative of the web application, the container may program (server) to communicate
2. Obtain
1. Get request object
request.getServletContext ();
2. obtained by the HttpServlet
this.getServletContext ();
package cn.itcast.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; @WebServlet("/servletContextDemo1") public class ServletContextDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { / * the ServletContext object acquisition: 1. Get request object request.getServletContext (); 2. obtained by the HttpServlet this.getServletContext (); * / // 1. Get request object the ServletContext context1 = request.getServletContext ( ); // 2 obtained by the HttpServlet the ServletContext = context2 the this .getServletContext (); System.out.println (context1); System.out.println (context2); System.out.println (context1 == context2); // to true } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
3. Function
1. Get MIME types:
MIME Type: Internet communication process defined in the file of one type of data
format: Type large / small type text / html image / jpeg
obtain: String getMimeType (String file)
package cn.itcast.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; @WebServlet("/servletContextDemo2") public class ServletContextDemo2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { / * the ServletContext functions: 1. Obtain the MIME types: * MIME types: A document type definition of the Internet data communication process * Format: Type large / small type text / Image HTML / JPEG * Get: String getMimeType (String file) 2. domain objects: data sharing real 3. get the file (server) path * / // 2. get through HttpServlet the ServletContext context = the this .getServletContext (); // 3. Definition file name String filename = " a.jpg "; // Image / JPEG // 4. Get MIME type String mimeType = context.getMimeType (filename); System.out.println (mimeType); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
Shared data: 2. domain object
1. the setAttribute (String name, Object value)
2. the getAttribute (String name)
3. removeAttribute (String name)
the ServletContext object range: all requested data for all users
package cn.itcast.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; @WebServlet("/servletContextDemo3") public class ServletContextDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { / * the ServletContext functions: 1. Get MIME type: 2. domain objects: shared data 3 acquired real (server) path to the file * / // 2. HttpServlet obtaining the ServletContext context = the this .getServletContext (); // setting data context.setAttribute ( "MSG", "haha" ); } protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { the this .doPost (Request, Response); } }
package cn.itcast.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; @WebServlet("/servletContextDemo4") public class ServletContextDemo4 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { / * the ServletContext functions: 1. Get MIME type: 2. domain objects: shared data 3 acquired real (server) path to the file * / // 2. HttpServlet obtaining the ServletContext context = the this .getServletContext (); // Get data Object MSG = context.getAttribute ( "MSG" ); System.out.println (MSG); } protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { the this .doPost (Request, Response); } }
3. Get real files (server) path
method: String getRealPath (String path)
package cn.itcast.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.File; import java.io.IOException; @WebServlet("/servletContextDemo5") public class ServletContextDemo5 extends HttpServlet { protected voidthe doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { / * the ServletContext functions: 1. Get MIME type: 2. domain objects: shared data 3 acquired real (server) path to the file * / // acquired by the HttpServlet the ServletContext context = the this .getServletContext (); // get the file server path String b = context.getRealPath ( "/ b.txt"); // web directory resource access System.out.println (b); // file file = File new new (realpath); String C = context.getRealPath ( "/ the WEB-INF / C.txt"); //Resources under WEB-INF directory access System.out.println (c); String A = context.getRealPath ( "/ WEB-INF / classes / a.txt"); // resources under the src directory access System.out. the println (A); } protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { the this .doPost (Request, Response); } }