Access to the ServletContext

Questions and answers on developing, deploying and using plugins and JavaBeans

Access to the ServletContext

Postby sdevlin » Wed Sep 08, 2004 12:38 am

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
sdevlin
 
Posts: 125
Joined: Tue Apr 13, 2004 3:33 am

Postby Jan Blok » Wed Sep 08, 2004 1:06 pm

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
Jan Blok
Servoy
Jan Blok
 
Posts: 2684
Joined: Mon Jun 23, 2003 11:15 am
Location: Amsterdam

Postby sdevlin » Thu Sep 09, 2004 12:25 am

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
sdevlin
 
Posts: 125
Joined: Tue Apr 13, 2004 3:33 am

Postby Jan Blok » Thu Sep 09, 2004 9:57 am

I see two possible servlet options:
Code: Select all
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
Code: Select all
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()
   }
}
Jan Blok
Servoy
Jan Blok
 
Posts: 2684
Joined: Mon Jun 23, 2003 11:15 am
Location: Amsterdam


Return to Plugins and Beans

Who is online

Users browsing this forum: No registered users and 23 guests