On Wed, 10 Jul 2002, Christian J. Dechery wrote:

> Date: Wed, 10 Jul 2002 13:27:26 -0300
> From: Christian J. Dechery <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: Re: Need Ideas... big problem! (long)
>
> I'm having some difficulty understanding the solution u guys provided me... maybe I 
>explained my problem badly, so u aren't fully understanding it...
>
> but I have a question that is quite simple:  Is it possible for a class
> (or Servlet) located in $tomcat_home\common\classes - that will be
> accessed from all webapps - to know from which context a JSP/Servlet
> called it?
>
> for example... I have a class A in common\classes and it has a method 
>doSomething()... say I have a JSP $tomcat_home\webapps\test\1.jsp that looks 
>something like:
>
> <%@page import="A"%>
> <%
> String x = A.doSomething();
> %>
>
> so... would it be possible for A to know that when doSomething() was
> called, the context was "test"?
>

Sure ... that's really easy.  You've got at least the following options:

* Call request.getContextPath() and you'll get the context path of the
  web application that is responding to this request.

* The "application" object in a JSP page is in fact the ServletContext
  for the current webapp, so you can call things like

  <%
    Properties props = new Properties();
    InputStream stream =
     application.getResourceAsStream("/WEB-INF/myprops.properties");
    props.load(stream);
    stream.close();
  %>

to load a properties file from inside the WEB-INF subdirectory of your web
application.

As a general note, however, you should really be doing this sort of thing
in startup code of a servlet, which stashes the results as servlet context
parameters for everyone else to use.  Using scriptlets to mix functional
logic into your JSP pages is going to cause you maintenance nightmares
over time.

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to