Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getRealPath("/") not working in spring boot [duplicate]

Am working on spring boot applicaiton. I want to write a jsp files dynamically into the web content. So i took the real path from the ServletContext and write into it. It is working fine in eclipse.

@autowired
ServletContext context 

or

ServletContext context = request.getSession().getServletContext();
String targetPath = context.getRealPath("/default/pages");

But I try to deploy a war file on linux machine, the path is empty. Please help me to fix this or let me know if it possible to do ?

spring boot embedded tomcat
like image 474
Shankar Avatar asked Jan 21 '26 03:01

Shankar


1 Answers

There are lots of reasons not to rely on getRealPath() in real-world deployments. There are several better approaches, depending on your use-case; but the one that worked best for me was to use a file path from the application.properties file using @Value

like image 96
Black Avatar answered Jan 23 '26 19:01

Black



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!