Hi Again,
How can I get Access to the ServletContext object for the servoy-services web application?
Calling ‘super.getServletContext()’ in the init() method of my servlet throws a null pointer exception.
Ultimately, I want to get a system path for the servoy-services application directory to be able to write files there at application startup.
example:
String path = servletContext.getRealPath(“/”);
File mainDir = new File(path);
File subDir = new File(mainDir, “subDirectory”);
// starting writing files
// etc..
Any Ideas?
Thanks,
Sean
The servlet has a virtual path, there is no real path… but why generate files ? all request are dispatched to your servlet so you could return the content from the “files” realtime
The content being served is image data. I require that the images be generated dynamically when the application starts before they are ever requested. I do not need servlets, but I do need the image files to be nested somewhere in the Tomcat web app hierarchy so they are accessible through the web-server. What I need specifically is a real(system) path and a web server-relative path to the same location so I can write the files using the system path and reconstruct the image urls to pass to the client so the image data can be retrieved directly by url request and processed by the client plugin. Can I get this information through the server access interface? A servlet context would be enough (that is why I thought a servlet could do this from its init method) but perhaps there is a more direct way that I don’t know about. The image contents may change as often as every application reboot, so they must be produced dynamically, but I want this to be able to happen independant of the system that the application is deployed on. I hope this explanation is more clear than the last.
Thanks,
Sean
I see two possible servlet options:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
//we assume imagedata is in a memory byte array
String path = request.getPathInfo(); //without servlet name
if (path.equals("/my_image.gif"))
{
reposonse.setConentType("image/gif");
OutputStream os = response.getOutputStream();
os.write(my_image_data_array);
os.close();
}
}
or
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
//we assume imagedata is written into tmp file via: File temp_my_image = File.createTempFile("my_image",".gif"); and the file object is passed to this servlet
String path = request.getPathInfo(); //without servlet name
if (path.equals("/my_image.gif"))
{
reposonse.setConentType("image/gif");
FileInputStream fis = new FileInputStream(temp_my_image);
OutputStream os = response.getOutputStream();
Utils.streamCopy(is,os);
os.close();
is.close()
}
}