package cn.loubth.web;
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(name = "ServletTest") public class ServletTest extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//-------------------------------第一类-------------------------------------------------------- String path1=this.getClass().getResource("").getPath(); //结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/cn/loubth/web/ //String path2=this.getClass().getResource(".").getPath(); //结果: 报错,空指针异常 String path3=this.getClass().getResource("/").getPath(); //结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/ String path4=this.getClass().getResource("\\").getPath(); //结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/cn/loubth/web/
//-------------------------------第二类--------------------------------------------------------
String path5=this.getClass().getClassLoader().getResource("").getPath(); //结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/ String path6=this.getClass().getClassLoader().getResource(".").getPath(); //结果: /C:/apache-tomcat-7.0.77-windows-x64/apache-tomcat-7.0.77/lib/ String path7=this.getClass().getClassLoader().getResource("/").getPath(); //结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/ String path8=this.getClass().getClassLoader().getResource("\\").getPath(); //结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/
//-------------------------------第三类--------------------------------------------------------
String path9=new File("").getAbsolutePath(); //结果: C:\apache-tomcat-7.0.77-windows-x64\apache-tomcat-7.0.77\bin String path10=new File(".").getAbsolutePath(); //结果: C:\apache-tomcat-7.0.77-windows-x64\apache-tomcat-7.0.77\bin\. String path11=new File("/").getAbsolutePath(); //结果: C:\ String path12=new File("\\").getAbsolutePath(); //结果: C:\
//-------------------------------输出结果-------------------------------------------------------- System.out.println(path1); //System.out.println(path2); //因为报错所以就不输出了 System.out.println(path3); System.out.println(path4); System.out.println(path5); System.out.println(path6); System.out.println(path7); System.out.println(path8); System.out.println(path9); System.out.println(path10); System.out.println(path11); System.out.println(path12);
} }
|