using a String to reference an already existing Object

A

Andrew Purser

Hello,

I am working with JSP Custom tags.

I am hoping that my question has a relativly simple answer and I
suspect it has been asked here before but I haven't been able to find
it by searching the archive.

I have a com.xxx.Connection object which I need to reference in my JSP
tag.

Hence I have

private com.xxx.Connection myConnection;

public void setMyConnection(com.xxx.Connection conn)
{
myConnection = conn;
}
public com.xxx.Connection getMyConnection()
{
return myConnection;
}

The problem I have is with the Setter. It needs to refer to an
instance of com.xxx.Connection which has previously been setup. The
problem is myConnection is passed as a String by the tag, and I need
to use that String to reference the instance of com.xxx.Connection
which has already been setup.

=====Sample JSP=======

<myTags:makeConnection callIt="connectionToUse" />

<myTags:useConnection myConnection="connectionToUse" />

=====Sample End========

To cut to the chase I need a setter which looks like the following

public void setMyConnection(String myConnection)
{
com.xxx.Connection connection
= referenceTheInstanceOfConnectionWhichAlreadyExistsWithTheName(myConnection)
}

Firstly, does anyone out there understand what I am on about?
Secondly does anyone have a solution?

Your thoughts are much appreciated.

Cheers,

Andrew.
 
K

kaeli

}

Firstly, does anyone out there understand what I am on about?

Yes. Unfortunately. I had a similar problem.
Secondly does anyone have a solution?

I ended up having to use a bean instead, which doesn't have the String
restrictions of the taglib.


--
 
M

Murray

Andrew Purser said:
Hello,

I am working with JSP Custom tags.

I am hoping that my question has a relativly simple answer and I
suspect it has been asked here before but I haven't been able to find
it by searching the archive.

I have a com.xxx.Connection object which I need to reference in my JSP
tag.

Hence I have

private com.xxx.Connection myConnection;

public void setMyConnection(com.xxx.Connection conn)
{
myConnection = conn;
}
public com.xxx.Connection getMyConnection()
{
return myConnection;
}

The problem I have is with the Setter. It needs to refer to an
instance of com.xxx.Connection which has previously been setup. The
problem is myConnection is passed as a String by the tag, and I need
to use that String to reference the instance of com.xxx.Connection
which has already been setup.

=====Sample JSP=======

<myTags:makeConnection callIt="connectionToUse" />

<myTags:useConnection myConnection="connectionToUse" />

=====Sample End========

To cut to the chase I need a setter which looks like the following

public void setMyConnection(String myConnection)
{
com.xxx.Connection connection
= referenceTheInstanceOfConnectionWhichAlreadyExistsWithTheName(myConnection)
}

Firstly, does anyone out there understand what I am on about?
Secondly does anyone have a solution?

Your thoughts are much appreciated.

Cheers,

Andrew.

Where does this instance of Connection exist exactly? Are you creating it in
a jsp/servlet? For a webapp, it probably needs to exist in some context:
either the page, request, session or application context. Look at the
set/getAttribute methods of PageContext, HttpServletRequest, HttpSession,
ServletContext (respectively). You'll need to need to decide which is most
appropriate, I don't know exactly what you're trying to achieve ;-) But if
you're working with tags, pageContext might be what you're after.
 
A

Andrew Purser

Thanks Murray, your pointers helped out.

The solution I ended up with is as follows.

For some reason I had to force my object into the Page Scope by using:
pageContext.setAttribute("myConnection",myConnection,pageContext.PAGE_SCOPE);

But, once it was there I could pick it up from within the custom tag.
The setter now looks like:
public void setConnection(String s)
{
myConnection = s;

// Setup the actual connection
connection = (com.xxx.Connection)pageContext.findAttribute(myConnection);
}

I decided on using the findAttribute method then it is up to the
implementer of the tag as to where the connection is maintained. Ie,
within Page, Request, Session or Application.

Cheers,

Andrew.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top