how to obtain the relative path of a resource in a j2ee project
I have a Dynamic Web Project having a flat file (or say text file). I have created a servlet in which I need to use this file.
My code is as following:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String resource = request.getParameter ("json") ;
if ( resource != null && !resource.equals ( "" ) ) {
//use getResourceAsStream ( ) to properly get the file.
InputStream is = getServletContext ().getResourceAsStream ("rateJSON") ;
if ( is != null ) { // the resource exists
response.setContentType("application/json");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
StringWriter sw = new StringWriter ( ) ;
for ( int c = is.read ( ) ; c != -1; c = is.read ( ) ) {
sw.write ( c ) ;
}
PrintWriter out = response.getWriter();
开发者_如何学Python out.print (sw.toString ()) ;
out.flush();
}
}
}
The problem is that the InputStream is has null value.
I'm not sure how to get the correct relative path. I'm using JBOSS as the app server.
I have added the resource file in the WebContent directory of a Dynamic Web Project. As a different approch, I tried this:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletConfig config = getServletConfig();
String contextName = config.getInitParameter("ApplicationName");
System.out.println("context name"+ contextName);
String contextPath = config.getServletContext().getRealPath(contextName);
System.out.println("context Path"+contextPath);
//contextPath = contextPath.substring(0, contextPath.indexOf(contextName));
contextPath += "\\rateJSON.txt";
System.out.println(contextPath);
String resource = request.getParameter ("json") ;
System.out.println("Hi there1"+resource);
if ( resource != null && !resource.equals ( "" ) ) {
System.out.println("Hi there");
//use getResourceAsStream ( ) to properly get the file.
//InputStream is = getServletContext ().getResourceAsStream (resource) ;
InputStream is = getServletConfig().getServletContext().getResourceAsStream(contextPath);
if ( is != null ) { // the resource exists
System.out.println("Hi there2");
response.setContentType("application/json");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
StringWriter sw = new StringWriter ( );
for ( int c = is.read ( ) ; c != -1; c = is.read ( ) ) {
sw.write ( c ) ;
System.out.println(c);
}
PrintWriter out = response.getWriter();
out.print (sw.toString ()) ;
System.out.println(sw.toString());
out.flush();
}
}
}
The value of contextPath is now: C:\JBOSS\jboss-5.0.1.GA\server\default\tmp\4p72206b-uo5r7k-g0vn9pof-1-g0vsh0o9-b7\Nationwide.war\WEB-INF\rateJSON
But at this location the rateJSON file is not there? It seems JBOSS is not putting this file in the App.war or doesn't deploy it??? Could someone please help me?
First, check Nationwide.war to make sure rateJSON.txt is included. If it is, then try:
String rootPath = getServletConfig().getServletContext().getRealPath("/");
File file = new File(realPath + "WEB-INF/rateJSON.txt");
InputStream is = new FileInputStream(file);
I think this one (getServletContext().getRealPath()) is in the docs...
This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
精彩评论