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

C

cvelusc

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
 
S

Sudsy

cvelusc said:
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?
 
C

cvelusc

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
 
S

Sudsy

cvelusc said:
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.
 
C

cvelusc

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!
 
S

Sudsy

cvelusc wrote:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top