标签:auth charset 服务 res move 动态 har 互联网通信 row
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; /** * @author 旗木五五开 * @create 2020-03-08 8:48 */ @WebServlet("/servletContextDemo1") public class ServletContextDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // servletContext获取 ServletContext context1 = request.getServletContext(); ServletContext context2 = this.getServletContext(); System.out.println(context1==context2);//true System.out.println(context1); System.out.println(context2); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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; /** * @author 旗木五五开 * @create 2020-03-08 9:30 */ @WebServlet("/servletContextDemo2") public class ServletContextDemo2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.通过 HttpServlet 获取 ServletContext context = this.getServletContext(); // 2.定义文件名称。以后是通过对应的文件动态的获取 String filename="a.jpg"; // 3.获取mime类型 String mimeType = context.getMimeType(filename); System.out.println(mimeType); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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; /** * @author 旗木五五开 * @create 2020-03-08 9:45 */ @WebServlet("/servletContextDemo3") public class ServletContextDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.通过 HttpServlet 获取 ServletContext context = this.getServletContext(); // 2.设置数据 context.setAttribute("msg","haha"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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; /** * @author 旗木五五开 * @create 2020-03-08 9:47 */ @WebServlet("/servletContextDemo4") public class ServletContextDemo4 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.通过 HttpServlet 获取 ServletContext context = this.getServletContext(); // 2.获取数据 Object msg = context.getAttribute("msg"); System.out.println(msg); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
标签:auth charset 服务 res move 动态 har 互联网通信 row
原文地址:https://www.cnblogs.com/rijiyuelei/p/12440943.html