Retrieving the context path from a standalone class 2004-01-06 - By Kent Boogaart
Back Hi Shapira,
Please see below for my responses.
Regards, Kent
Howdy, You're missing a few things:
1. The context path is (according to the spirit of the servlet spec) to be decided by the server administrator, just like the port on which the webapp will run. Your application should be completely agnostic of the actual context path. This is not a design flaw in the servlet API, but a flaw in your own application's current design.
[kb] OK, but how can my app be agnostic of the context path if I need to do things like dynamically construct URLs? Why does the need to request a resource in the same web app constitute a design flaw? Surely this is a fairly common requirement . . . how else would I index dynamic resources? Also, if the web app should be completely agnostic of the context path, why does the HttpServletRequest interface define a method to retrieve it?
2. If you want to keep your current design, simply start the indexing thread when the first request comes in using its context path. You can, for example, have your servlet context listener class also be a servlet request listener, and do something on the first incoming request only.
[kb] Not entirely sure what you mean here. Are you saying I should write a servlet that also implements ServletContextListener? If so, how do I instigate the first request to the servlet (again, without knowing the context path)? Wouldn't it make more sense to implement a filter and run the index the first time the filter runs? The filter could pass the context path to the indexing thread for subsequent usage. Of course, the indexing would only take place once a request is received - I'd rather a solution that allows the indexing to run on app startup.
3. This is a user, i.e. the tomcat-user list, question, so please continue discussion there. This list is for internal tomcat development discussions.
[kb] Sorry, I knew that - just wasn't thinking straight on a Tuesday evening :-)
Thanks,
Yoav Shapira Millennium ChemInformatics
>-- --Original Message-- -- >From: Kent Boogaart [mailto:[EMAIL PROTECTED] >Sent: Tuesday, January 06, 2004 12:34 AM >To: '[EMAIL PROTECTED]' >Subject: Retrieving the context path from a standalone class > >Hi all, > >I have a class in my web app that runs in a separate thread. It's >responsibility is to re-index the site every x minutes (I am using Lucene >for site searches). It recursively loops over the relevant files within the >web app folder and attempts to index each one (the web app must be exploded >at this stage). I have this working for HTML and plain text resources. >However, I would like to include JSPs in my index. Of course, I need to >request each JSP to get any meaningful information for the index. > >I have a class that implements the ServletContextListener interface and >stores a global reference to the ServletContext object. Originally I >thought >this would be all I needed to dynamically request JSPs. I figured you could >retrieve the servlet context path from the ServletContext object in order >to >construct a URL to request - how silly of me! It seems you cannot retrieve >the context path from anywhere other than the HttpServletRequest interface. >Besides being a major design flaw (?), this complicates things for me. I >cannot dynamically construct a request URL because I don't know the context >path and my application must be able to run under any context path >including >the default one. > >I thought about using the ServletContext.getRequestDispatcher() method. >However, I needed an implementation of HttpServletRequest and >HttpServletResponse in order to use the dispatcher. HttpServletRequest, as >I >just mentioned, defines the getContextPath() method which I obviously can't >implement. The JSPs in my application already rely on this method returning >the correct context path. Hence, this was not an option. > >I also thought of having a servlet that loads on start-up set a global >variable containing the context path. However, I would need the servlet to >be requested in order to obtain an instance of HttpServletRequest from >which >I could extract the context path - the parameters to the init() method are >of no help. > >A possible solution is to define an environment entry for the web app that >duplicates the details of the context path. This is an awful solution and >should not be necessary. Can anyone shed some light on this? Am I missing >something embarrassingly obvious here? > >Thanks, >Kent
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------ To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected) For additional commands, e-mail: tomcat-user-help@(protected)
|
|