Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Passing implicit (standard) "config" object from jsp to java class (not servlet)

Reply
Thread Tools

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

 
 
cvelusc
Guest
Posts: n/a
 
      05-26-2004
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
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      05-26-2004
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?

 
Reply With Quote
 
 
 
 
cvelusc
Guest
Posts: n/a
 
      05-27-2004
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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?

 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      05-27-2004
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.

 
Reply With Quote
 
cvelusc
Guest
Posts: n/a
 
      06-03-2004
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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.

 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      06-03-2004
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP and Javascripts on same page (Passing values to JSP fromJavascript) Husain Javascript 3 04-14-2008 10:36 AM
JSP Implicit Objects marcwentink@hotmail.com Java 2 07-25-2006 01:39 PM
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
(jsp) class loader not finding generated JSP class unixfreak0037@gmail.com Java 1 12-14-2005 09:35 AM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments