Servlet(ServletContext 文件读取)
文章目录
Servlet(ServletContext 文件读取)ServletContext概念方法存储所有用户共享的数据
JavaWeb中四大域对象获取文件mime类型WEB项目读取文件与普通文件不一样 因为经过编译后文件的位置会发生改变 存放在tomcat文件夹下写死文件路径 方法不可取 改变服务器位置会发生变化getMimeType(文件名称)获取文件运行的真实路径:getRealPath():ServletContext只能用于web环境。如果是非web环境,则使用ClassLoader来获取真实路径
ServletContext
概念
1全局的上下文对象
2.代表了整个web应用
3.由服务器在开启时就创建
特点
1.由服务器创建
2.单例
功能
ServletContext:全局的上下文对象 也是一个域对象
域:范围 ServletContext能在他所代表的范围内来共享数据
范围:整个web应用
方法
存储所有用户共享的数据
存储所有用户共享的数据
setAttribute(): 存储
getAttribute(): 获取
removeAttribute(): 删除域数据
代码
@WebServlet(name = "ServletSanWa",value="/die")
public class ServletSanWa extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("感时花溅泪");
String name="阿米娅";
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("name",name);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
public class HTTPService extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("GET请求");
ServletContext servletContext = this.getServletContext();
String name = String.valueOf(servletContext.getAttribute("name"));
System.out.println(name);
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("POST请求");
System.out.println("一树梨花压海棠");
}
}
运行结果
感时花溅泪
GET请求
阿米娅
POST请求
一树梨花压海棠
JavaWeb中四大域对象
ServletContext:代表整个web应用HttpSession:会话域ServletRequest:请求域PageContext:页面域
获取文件mime类型
WEB项目读取文件与普通文件不一样 因为经过编译后文件的位置会发生改变 存放在tomcat文件夹下
f1 = new FileInputStream("D:\\IDEA\\IdeaProjects\\ServletWeb2\\out\\artifacts\\ServletWeb2_war_exploded\\WEB-INF\\classes\\1.txt");
f3 = new FileInputStream("D:\\IDEA\\IdeaProjects\\ServletWeb2\\out\\artifacts\\ServletWeb2_war_exploded\\WEB-INF\\3.txt");
f2 = new FileInputStream("D:\\IDEA\\IdeaProjects\\ServletWeb2\\out\\artifacts\\ServletWeb2_war_exploded\\2.txt");
写死文件路径 方法不可取 改变服务器位置会发生变化
@WebServlet(name = "ServletFile", value = "/san")
public class ServletFile extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileInputStream f3 = null;
FileInputStream f1 = null;
FileInputStream f2 = null;
try {
f1 = new FileInputStream("D:\\IDEA\\IdeaProjects\\ServletWeb2\\out\\artifacts\\ServletWeb2_war_exploded\\WEB-INF\\classes\\1.txt");
f3 = new FileInputStream("D:\\IDEA\\IdeaProjects\\ServletWeb2\\out\\artifacts\\ServletWeb2_war_exploded\\WEB-INF\\3.txt");
f2 = new FileInputStream("D:\\IDEA\\IdeaProjects\\ServletWeb2\\out\\artifacts\\ServletWeb2_war_exploded\\2.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
}
getMimeType(文件名称)
获取文件运行的真实路径:
真实路径:指服务器路径
getRealPath():
1.webRoot下:项目根目录下2.WEB-INF下: 项目根目录下/WEB-INF3.src下: 项目根目录下/WEB-INF/classes
注意:如果getRealPath("/"),获取的路径是项目的根目录
//根目录
@WebServlet(name = "ServletGetReal", value = "/wa")
public class ServletGetReal extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/");
System.out.println(realPath);
FileInputStream f1 = new FileInputStream(realPath + "WEB-INF\\classes\\1.txt");
FileInputStream f2 = new FileInputStream(realPath + "2.txt");
FileInputStream f3 = new FileInputStream(realPath + "WEB-INF\\3.txt");
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
}
运行结果: //D:\IDEA\IdeaProjects\ServletWeb2\out\artifacts\ServletWeb2_war_exploded\
java.io.FileInputStream@562364b6
java.io.FileInputStream@50dcd3bb
java.io.FileInputStream@173dfccf
ServletContext只能用于web环境。如果是非web环境,则使用ClassLoader来获取真实路径
web项目里有一个普通的java工具类 我想获取文件的真实路径 那我就用不了 ServletContext
方法
//1.获取该类对应的类加载器对象
ClassLoader loader = this.getClass().getClassLoader();
//2.获取文件运行的真实路径
URL url = loader.getResource("a.txt");
String path = url.getPath();
//注意类加载器来获取路径也有局限性 只能获取src目录下的文件
// 方式2
InputStream stream = MyUtil.class.getResourceAsStream("/a.txt");
System.out.println(stream);
例子
public class FUtils {
public void file() {
System.out.println("普通类读取文件工具方法一调用");
// 方式一
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("1.txt");
System.out.println(resourceAsStream);
// 方式二
System.out.println("普通类读取文件工具方法二调用");
URL url = this.getClass().getClassLoader().getResource("1.txt");
String path = url.getPath();
FileInputStream f3 = null;
try {
f3 = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(f3);
}
}
测试代码 @WebServlet(name = "ServletUti", value = "/san1")
public class ServletUti extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
new FUtils().file();
}
运行结果 普通类读取文件工具方法一调用
java.io.ByteArrayInputStream@71e47211
普通类读取文件工具方法二调用
java.io.FileInputStream@1052771b