关于request.getSession())问题及获取tomcat的根目录路径
我的类继承了HttpServllet定义了HttpServlletRequest按道理可以用request.getSession().getServletContext(...
我的类继承了HttpServllet 定义了HttpServlletRequest 按道理可以用request.getSession().getServletContext().getRealPath("/")可以获取tomcat的绝对路径
但是报错
Exception in thread "main" java.lang.NullPointerException
at com.test.utils.XML.GetPath(XML.java:22)
at com.test.utils.XML.main(XML.java:45)
public class XML extends HttpServlet{
public static String GetPath(){
HttpServletRequest request = null;
//String path=request.getSession().getServletContext().getRealPath("/");
System.out.println("path1="+request.getSession());
System.out.println("path2="+request.getSession().getServletContext());
System.out.println("path3="+request.getSession().getServletContext().getRealPath("/"));
//InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
return null;
}
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("E:\\新建 文本文档 (3).xml"));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName("Header");
Element element = (Element) list.item(0);
//System.out.println(element.getChildNodes().item(0).getNodeValue());
} catch (Exception e) {
System.out.println("exception:" + e.getMessage());
}
GetPath();
}
} 展开
但是报错
Exception in thread "main" java.lang.NullPointerException
at com.test.utils.XML.GetPath(XML.java:22)
at com.test.utils.XML.main(XML.java:45)
public class XML extends HttpServlet{
public static String GetPath(){
HttpServletRequest request = null;
//String path=request.getSession().getServletContext().getRealPath("/");
System.out.println("path1="+request.getSession());
System.out.println("path2="+request.getSession().getServletContext());
System.out.println("path3="+request.getSession().getServletContext().getRealPath("/"));
//InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
return null;
}
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("E:\\新建 文本文档 (3).xml"));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName("Header");
Element element = (Element) list.item(0);
//System.out.println(element.getChildNodes().item(0).getNodeValue());
} catch (Exception e) {
System.out.println("exception:" + e.getMessage());
}
GetPath();
}
} 展开
2个回答
2013-02-04
展开全部
单继承,是不够的,还要真是一个Servlet,才能。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询