You need to programmatically runtime run script.sql, which is located in the webapp folder. Tell me how to do this? The application runs on servlets.

  • What is the webapp folder? And what about the ideas? In the war-archive? On the server disk? - Sergey
  • yes, I cant talk about the idea, tomcat server. should end up with a war-file. - Mihail Gogol
  • is there a DB description? or what? - Mikhail Vaysman
  • @ Mikhail Vaysman thanks, they gave me the answer below. - Mihail Gogol

1 answer 1

Need to know the path of the file inside the war-archive

String path = "/script.sql"; // String path = "/WEB-INF/script.sql"; 

At the same time, we immediately get a ServletContext , because all the useful methods in this class

 ServletContext sc = request.getServletContext(); 

Then you can find out the way

 String rp = sc.getRealPath(path); 

If you want to open this file, then you should not look for its real path (and it is not always possible in fact). And we must immediately open it

 InputStream is = sc.getResourceAsStream(path); 

You can get the URL of the file (if you suddenly need to transfer to some method that accepts the URL)

 URL url = sc.getResource(path); 
  • Thank you, they answered at once. please tell me. I understand that the path ${pageContext.request.contextPath} returns the project root from which it starts. I asked to access css ${pageContext.request.contextPath} /file.css. But when I deploy my application to TOMCAT through the application manager, I get a level 2 application localhost: 8080 / app and I don’t see these paths anymore. The CSS is no longer in the root, but in "/app/file.css". Is it possible to somehow specify the path differently so that the server sees it correctly in both cases? - Mihail Gogol
  • ${pageContext.request.contextPath} or shorter than ${request.contextPath} this is /app . /app is the root. ${request.contextPath}/file.css will respectively /app/file.css . All right This is the only sure way. Should work. The point is something else or confusing with some other root. Make sure that file.css is in the root of the war-archive. Clean browser cache. - Sergey
  • I, probably, incorrectly expressed. My application does not run on localhost: 8080 / , but on localhost: 8080 / app . Due to this, as I understand it, and get an error, because ${request.contextPath} searches for a file not in localhost: 8080 / app but in localhost: 8080 / . those. when I run it from an idea on tomkete, everything works well (because the application is at the root), but when I see it directly in tomkete, I don’t see files in the specified paths. - Mihail Gogol
  • ${request.contextPath} - the actual root of the application, the so-called context root . The idea will be / in tomcat-e /app (by the name of your war-archive). That's all right. You can be sure to display the path to the page. Right in jsp place somewhere <div>${request.contextPath}</div> and see the path in the browser. And judge for yourself, the fig would need a variable that is always equal to / ? - Sergey
  • If the idea of ${request.contextPath}/file.css works, but not in the volume, then either there is no /file.css in the war archive, or one of the two. Check the contents of app.war . This is an ordinary zip archive, it opens with 7zip and winrar winrar. - Sergey