Passing implicit (standard) "config" object from jsp to java class (not servlet)

Discussion in 'Java' started by cvelusc, May 26, 2004.

  1. cvelusc

    cvelusc Guest

    Morning!

    Here's the situation: I have two servlet's. The first is initialized
    once the Websphere project is started, the second is initialized once
    the webpage has been visited.

    I need to get the ServletConfig from the first servlet and pass that
    object to the init method of the second servlet. How can I do this?

    Is it possible to pass the implicit "config" object of jsp pages to
    the java class that would then initialized the second servlet?

    Could I grab the init parameters from the web.xml file and define my
    own ServletConfig object?

    The first servlet does allow me access to it's ServletContext. Could
    I use this object somehow to define a ServletConfig?

    Warm regards,


    Christopher
     
    cvelusc, May 26, 2004
    #1
    1. Advertising

  2. cvelusc

    Sudsy Guest

    Re: Passing implicit (standard) "config" object from jsp to javaclass (not servlet)

    cvelusc wrote:
    > Morning!
    >
    > Here's the situation: I have two servlet's. The first is initialized
    > once the Websphere project is started, the second is initialized once
    > the webpage has been visited.
    >
    > I need to get the ServletConfig from the first servlet and pass that
    > object to the init method of the second servlet. How can I do this?


    Are you sure? The reason I ask is that it appears you've arrived at a
    non-standard (some would say convoluted) solution to a perceived
    problem.
    Perhaps it would be wiser to state precisely what you're trying to
    accomplish. I'm sure there are nicer (cleaner) ways of doing it.
    What is it in the ServletConfig that you require?
     
    Sudsy, May 26, 2004
    #2
    1. Advertising

  3. cvelusc

    cvelusc Guest

    Sudsy, to start off thanks for responding. You may be entirely
    correct that the problem is entirely perceived, and as such, I would
    be thankful to know why.

    I am trying to accomplish the initialization of a second bean after
    the WebSphere project has started. The required initialization
    parameter is a ServletConfig object.

    The ServletConfig holds init params, i.e., database userid's/pw's,
    logging directores, ... The first bean is defined within the web.xml
    file and performs the bulk of processing for the application. Because
    it's within the web.xml file, it's loaded upon the WebSphere project
    starting.

    Presently there is a jsp file that loads the second bean after
    navigation to it, i.e.:
    <jsp:useBean id="registry" scope="application"
    class="com.hilton.utils.RegistryBean"/>
    <%registry.init(config);%>

    The goal here is to eliminate the initialization of the bean within
    the jsp file, and delegate this duty to an action class. My thought
    was that this could be done either by passing the ServletConfig from
    the jsp file to the class, or gathering the ServletConfig from the
    already initialized Servlet.

    What do you think?

    Thanks,



    Christopher


    Sudsy <> wrote in message news:<>...
    > cvelusc wrote:
    > > Morning!
    > >
    > > Here's the situation: I have two servlet's. The first is initialized
    > > once the Websphere project is started, the second is initialized once
    > > the webpage has been visited.
    > >
    > > I need to get the ServletConfig from the first servlet and pass that
    > > object to the init method of the second servlet. How can I do this?

    >
    > Are you sure? The reason I ask is that it appears you've arrived at a
    > non-standard (some would say convoluted) solution to a perceived
    > problem.
    > Perhaps it would be wiser to state precisely what you're trying to
    > accomplish. I'm sure there are nicer (cleaner) ways of doing it.
    > What is it in the ServletConfig that you require?
     
    cvelusc, May 27, 2004
    #3
  4. cvelusc

    Sudsy Guest

    Re: Passing implicit (standard) "config" object from jsp to javaclass (not servlet)

    cvelusc wrote:
    > Sudsy, to start off thanks for responding. You may be entirely
    > correct that the problem is entirely perceived, and as such, I would
    > be thankful to know why.
    >
    > I am trying to accomplish the initialization of a second bean after
    > the WebSphere project has started. The required initialization
    > parameter is a ServletConfig object.
    >
    > The ServletConfig holds init params, i.e., database userid's/pw's,
    > logging directores, ... The first bean is defined within the web.xml
    > file and performs the bulk of processing for the application. Because
    > it's within the web.xml file, it's loaded upon the WebSphere project
    > starting.


    Take a look at the ServletContext class. An instance exists for every
    app. What you could do is invoke setAttribute( name, value ) and then
    retrieve the values using getAttribute( name ). Here's what the code
    could look like in the servlet which is loaded on startup:

    public void init( ServletConfig conf ) throws ServletException {
    String s = conf.getInitParameter( "aName" );
    conf.getServletContext().setAttribute( "org.myorg.aName", s );
    ...
    }

    In your other servlets (non-Action) you would do this:

    String s = (String) getServletContext().getAttribute(
    "org.myorg.aName" );

    In your classes which extend ActionServlet:

    String s = (String) getServlet().getServletContext().getAttribute(
    "org.myorg.aName" );

    It becomes part of the application context.
     
    Sudsy, May 27, 2004
    #4
  5. cvelusc

    cvelusc Guest

    Sudsy,

    Took your comments into consideration, and believe they would have
    worked if I had the option to modify the main Servlet the website runs
    on. However, because were using a proprietary commerce package, I did
    not have this option. If I made the situation more precise, I'm sure
    you would of had a solution that would have worked.

    Here's an illustration of the situation:

    WebSphere starting...
    ....load main servlet (proprietary, compiled classes, can't change
    methods)
    ....load a second servlet (our classes, can add methods)

    Navigate to website page
    ....load a third servlet (another of our classes, can add methods)

    The key here is that I need to initialize the third Servlet based on
    the ServletConfig of the Servlets that have already been initialized.

    This is how I resolved the issue (2 seperate methods--either of them
    works):

    1. Add a static method to the second Servlet that returns it's
    ServletConfig.
    2. Add init params (in the WebSphere xml descriptor) to the main
    Servlet that matches those of the second Servlet.

    Ended up going with option 2.

    Thanks for your interest!

    Sudsy <> wrote in message news:<>...
    > cvelusc wrote:
    > > Sudsy, to start off thanks for responding. You may be entirely
    > > correct that the problem is entirely perceived, and as such, I would
    > > be thankful to know why.
    > >
    > > I am trying to accomplish the initialization of a second bean after
    > > the WebSphere project has started. The required initialization
    > > parameter is a ServletConfig object.
    > >
    > > The ServletConfig holds init params, i.e., database userid's/pw's,
    > > logging directores, ... The first bean is defined within the web.xml
    > > file and performs the bulk of processing for the application. Because
    > > it's within the web.xml file, it's loaded upon the WebSphere project
    > > starting.

    >
    > Take a look at the ServletContext class. An instance exists for every
    > app. What you could do is invoke setAttribute( name, value ) and then
    > retrieve the values using getAttribute( name ). Here's what the code
    > could look like in the servlet which is loaded on startup:
    >
    > public void init( ServletConfig conf ) throws ServletException {
    > String s = conf.getInitParameter( "aName" );
    > conf.getServletContext().setAttribute( "org.myorg.aName", s );
    > ...
    > }
    >
    > In your other servlets (non-Action) you would do this:
    >
    > String s = (String) getServletContext().getAttribute(
    > "org.myorg.aName" );
    >
    > In your classes which extend ActionServlet:
    >
    > String s = (String) getServlet().getServletContext().getAttribute(
    > "org.myorg.aName" );
    >
    > It becomes part of the application context.
     
    cvelusc, Jun 3, 2004
    #5
  6. cvelusc

    Sudsy Guest

    Re: Passing implicit (standard) "config" object from jsp to javaclass (not servlet)

    cvelusc wrote:
    <snip>
    > This is how I resolved the issue (2 seperate methods--either of them
    > works):
    >
    > 1. Add a static method to the second Servlet that returns it's
    > ServletConfig.
    > 2. Add init params (in the WebSphere xml descriptor) to the main
    > Servlet that matches those of the second Servlet.
    >
    > Ended up going with option 2.
    >
    > Thanks for your interest!


    No problem. And now you have familiarity with ServletContext,
    ServletConfig and the use of initialization parameters in the
    web.xml file. Sounds good!
     
    Sudsy, Jun 3, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    2,018
    Jack Jia
    Apr 4, 2004
  2. chamikara
    Replies:
    1
    Views:
    49,875
  3. Andrew Purser
    Replies:
    3
    Views:
    5,090
    Alex Kay
    Nov 4, 2004
  4. javadev
    Replies:
    5
    Views:
    12,936
    javadev
    Nov 16, 2006
  5. Replies:
    4
    Views:
    794
Loading...

Share This Page