赞
踩
代表整个web应用,可以和程序的容器(服务器)来通信。
ServletContext是Servlet中最大的一个接口,呈现了web应用的Servlet视图。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。
request.getServletContext();
this.getServletContext();
- 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);
- }
- }
- @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);
- }
- }
- @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);
- }
- }
- @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);
- }
- }
String getRealPath(String path);
- String a = context.getRealPath("/a.txt");
- System.out.println(a);
- String b = context.getRealPath("/WEB-INF/b.txt");
- System.out.println(b);
- 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);
- }
- }
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>下载</title>
- </head>
- <body>
- <a href="/Web/servletContextDemo6?filename=1.jpg">图片下载</a>
- </body>
- </html>
这里注意需要设置response响应头的mime类型和响应头的打开方式!!!
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);
- }
- }
点击下载
文件就被默认(也可以用浏览器设置到其他地方)的下载到了这个位置
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。