the "application" notation is just a shortcut to accessing the servlet context
from within a jsp page.  to get at this variable from a regular java class,
you'd have to pass a reference to the servlet context itself.  a better
strategy would be to access this object from a servlet, then pass a reference
to that object to your java class, which would perform some processing on it.
from a servlet, you can access your menu object like so:

String[] menuArray = (String[])getServletContext().getAttribute("menu");

then pass the menuArray reference wherever you want...

"struts (H2Opilot)" wrote:

> OK - I love U all!!!
> One last Question:
> How can i access the "application.getAttribute("menu")" in an java-methode?
>
> Here for everybody - my code:
>
> Struts -Web.xml:
> ***********************
>  <servlet>
>   <servlet-name>MenuInit</servlet-name>
>   <servlet-class>h2ouapp.MenuInit</servlet-class>
>   <load-on-startup>1</load-on-startup>
>  </servlet>
>
> Servlet - MenuInit
> ***********************
> package h2ouapp;
>
> import java.util.*;
> import java.io.*;
> import java.lang.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import javax.servlet.ServletConfig.*;
> import javax.servlet.ServletContext.*;
>
> public class MenuInit extends HttpServlet {
>
>       public void init(){
>                 h2ouapp.DynamicBean menu= new h2ouapp.DynamicBean();
>                 getServletContext().setAttribute("menu", menu );
>       }
> }
>
> JSP
> ***********************
> <% h2ouapp.DynamicBean menu =
> (h2ouapp.DynamicBean)application.getAttribute("menu"); %>
> <% int m1=1,m2=2,m3=2; %>
> .
> .
> <% out.println(menu.moschreib(m1,m2,m3));%>
>
> Thanks a lot!! It works fine!!!
>
> -----Urspr�ngliche Nachricht-----
> Von: Jonathan M Crater [mailto:[EMAIL PROTECTED]]
> Gesendet: Mittwoch, 12. September 2001 13:25
> An: [EMAIL PROTECTED]
> Betreff: Re: Initalizing at startup and make global usable
>
> to tell your container to load a servlet on startup, use this block in
> your web.xml file:
>
> <servlet>
>   <servlet-name>SomeServlet</servlet-name>
>   <servlet-class>fully.qualified.package.SomeServlet</servlet-class>
>   <load-on-startup>1</load-on-startup>
>  </servlet>
>
> in your startup servlet:
>
> public void init() {
>     // do some parsing and build your array
>
>     //set your array in the servlet context
>     getServletContext().setAttribute("someKey", yourArray);
> }
>
> in any jsp within this application context:
>
> <%
>     String[] yourArray = (String[])application.getAttribute("someKey");
>     //do something with yourArray
> %>
>
> hope this helps...
>
> VIAUD C�dric wrote:
>
> >
> >
> > I think you can do your initialisation in the init() method of the
> > servlet.
> >
> > C�dric
> >
> > -----Message d'origine-----
> > De : struts (H2Opilot) [mailto:[EMAIL PROTECTED]]
> > Envoy� : mercredi 12 septembre 2001 10:41
> > � : [EMAIL PROTECTED]
> > Objet : AW: Initalizing at startup and make global usable
> >
> > hi,
> > this was also my Idea, but I still down't know how to start the
> > servlet in
> > the XML.
> > And I also down't know how to set the Array's with in the java-Class
> > file.
> >
> > And later I down't know how to reuse the Array's in the jsp-File to
> > forward
> > it to the jsp-file witch is producing the menue via out.println.
> >
> > Can U give me a simple Code-Example?
> >
> > -----Urspr�ngliche Nachricht-----
> > Von: Alexander Jesse [mailto:[EMAIL PROTECTED]]
> > Gesendet: Mittwoch, 12. September 2001 10:31
> > An: struts-user; struts
> > Betreff: RE: Initalizing at startup and make global usable
> >
> > hi,
> >
> > write a little servlet that parses the config-file, set up your bean
> > and store it in the application-context.
> >
> > In your jsp's you can then access it with scope="application", which
> > is
> > fast!
> >
> > To get your servlet executed at start-time, you can define it in your
> > web.xml with a "load-at-startup"-parameter. Then it will be started by
> > the
> > servlet-engine as soon as it is ready.
> >
> > hope this helps
> > Alexander
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, September 12, 2001 10:14 AM
> > To: [EMAIL PROTECTED]
> > Subject: Initalizing at startup and make global usable
> >
> > Hi all,
> > I have to use an Array with my menuestructur global in my
> > strutsapplication.
> > Therefor I implement an Class DynamicBean which initalizes the Array
> > and
> > fills them all up by parsing an config file.
> >
> > Up to now I put this call at any *.jsp-page. This is quite slow,
> > because he
> > always should read the full config file and parse it for any jsp.
> >
> > Here is my code example:
> >
> > <% h2ouapp.DynamicBean menu= new h2ouapp.DynamicBean();%>
> >
> > The constructor of this Class is then initalizing the array..
> >
> > I then use this Array via the following call to produce the menue:
> >
> > <% out.println(menu.moschreib());%>
> >
> > So, how can I start the initalizing via startup of struts...
> > Should I use web.xml?
> > How can I make the array global accessible and can I then use it?
> >
> > Can anybody give me a code example?


Reply via email to