Sharing a connection among a bunch of JSP tags?

Hi, I am new to java, so please excuse me if this is a silly question, but here goes:
I am created a tag library with a bunch of tags in it. Basically all of these tags will perform various database queries through JDBC, so every class (which implements a tag) will have one or more methods which uses a Connection object to the database.
It would be very silly to have every method in every tag class open its own connection, do its queries, and then close the connection, because performance would be aweful.
What I would like is that the connection is established just once before any tags are displayed, and taken down just once after all the tags are finished. Also, within the tag class methods, I would not like to have a Connection argument for each one of them; they are all using the same thing, so it should be treated as a global variable basically.
Is there a way to do this?
Thanks

Pretty simple. you just have to make sure that your Connection object will exist in the context where you look for it under the exact name.
When you write a tag handler, you extend either TagSupport or BodyTagSupport (you are of course only required to implement either the Tag or BodyTag interfaces - but believe me its easier just to extend the TagSupport classes). You have then access to the implicit variable pageContext. This variable's type is javax.servlet.jsp.PageContext. You can consult the APIDOC at:
http://java.sun.com/j2ee/j2sdkee/techdocs/api/index.html
Important for you are the two versions of setAttribute:
public Object getAttribute(String name, int scope)
public Object getAttribute(String name)The first one looks for an object in a specified scope
(one of:
PageContext.REQUEST_SCOPE,
PageContext.PAGE_SCOPE,
PageContext.SESSION_SCOPE,
PageContext.APPLICATION_SCOPE;
and the second one looks in the page scope.
So if you define a variable of type Connection somewhere in your jsp:
<%
try {
    Conection conn = myDataSource.getConnection();
%>
    <myTL:myTag1/>
    <myTL:myTag2>
        <myTL:myTag1/>
    </myTL:myTag2>
<%
catch (SQLException sex) {
     //handle the exception
finally {
    try {conn.close();} catch(Exception ex) {;}
%>you can access the conn variable from the pageContext variable in the myTag1 and myTag2 tag handlers:
public class MyTag1 extends BodyTagSupport {
    private Conection conn = pageContext.getAttribute("conn");   
    public int doStartTag() throws JspException {
        // do with connection what you want...
}You can of course develop a tag for the getConnection() and conn.close() sections, so everything on your page would be tags. But you have to read further for custom tags, because when you want to define page scope variables in a custom tag you have to write a TEI class.
Hope this'll help
Anton

Similar Messages

  • Sharing JMS connections among senders

              Is it wise for a web application to store connections to particular destinations
              in a static hashtable to reduce the number of createConnection operations, assuming
              the connections will only be used for send operations? Or would this prevent
              objects from being returned to the pool and cause threads to hang?
              

    Jason Taylor wrote:
              > Hey you're really great! I don't really expect anything on a Saturday but thanks!
              >
              > Is it possible that a connection could age out after a certain time and cause
              > things to hang? (please put your wls 6.1sp3 hat on for this one please, I'm still
              > a couple of revs back ;)
              Nothing hangs, but the connection can go bad for a variety
              of reasons (WL server failure, machine failure, network
              failure). But if anything goes wrong with the connection,
              the connection listeners optional onException handler is called.
              At this point, the connection can be automatically cleaned out
              of the pool. I think the sample code in the white-paper already
              does this.
              The built-in 8.1 pools take this one step farther - they eventually
              close JMS connections if they've gone idle for a while in order
              to conserve resources. Not a strong necessity with WL JMS,
              as such resources amount to no more than a bit of extra
              memory usage, unless perhaps the pool has pooled connections
              for many remote JMS servers.
              WL will kill idle network connections after a while - but I think this
              only applies to the iiop protocol, which JMS does not support
              until 8.1, not t3. I think idle http tunnelled connections may
              also get killed (in which onException would get called).
              The idle timeout is a configurable network setting on
              a per server basis. Post to the RMI newsgroup for details.
              >
              > Tom Barnes <[email protected]> wrote:
              >
              >>Hi Jason,
              >>
              >>Caching improves performance for high-throughput applications.
              >>So yes, it may be wise. You may also want
              >>to cache your producers, JNDI lookups, etc.
              >>
              >>As for the question about threads, unlike some other
              >>JMS implementations, WebLogic JMS does NOT create a thread per
              >>connection or per consumer. In fact, WL JMS could
              >>service a few thousand connections with just one or two
              >>threads in theory - but we do steal a few more than that
              >>for performance reasons, and laziness reasons
              >>(saves thinking about how to prevent thread starvation!).
              >>
              >>If you are thinking about caching, I suggest you look into
              >>two things:
              >>
              >>1) 8.1 contains built-in caching, check out the new features
              >>section for JMS in the release notes. (The use of ejb
              >>resource refs to access JMS will implicitly create a
              >>cache for you underneath, very cool - well I think
              >>its cool anyway.)
              >>
              >>2) If 8.1 caching is not sufficient, check out the example
              >>producer pool in the WL JMS Performance Guide. Regardless
              >>you may want to read it useful reading:
              >>
              >>http://dev2dev.bea.com/resourcelibrary/whitepapers/WL_JMS_Perform_GD.jsp
              >>
              >>Tom
              >>
              >>Jason Taylor wrote:
              >>
              >>>Is it wise for a web application to store connections to particular
              >>
              >>destinations
              >>
              >>>in a static hashtable to reduce the number of createConnection operations,
              >>
              >>assuming
              >>
              >>>the connections will only be used for send operations? Or would this
              >>
              >>prevent
              >>
              >>>objects from being returned to the pool and cause threads to hang?
              >>
              >
              

  • Sharing portlet preferences among users

    Hello,
    Is it possible to share portlet preferences among users? So that the
    portlet preferences would be stored per portlet instance, and would be
    the same for every user?
    If this is not possible with Java portlets (JSR-168), can it be done
    with other portlet types (JSP portlet, page flow portlet), using the
    PortletPreferences API?
    The JSR-168 specification says:
    "Portlet Specification assumes preference attributes are user specific,
    it does not make any provision at API level or at semantic level for
    sharing preference attributes among users. If a portal/portlet-container
    implementation provides an extension mechanism for sharing preference
    attributes, it should be well documented how the sharing of preference
    attributes works."

    Betsy,
    Does WLP provide a way to allow a user to store only a subset of all
    his portlet preferences (using only JSR 168 api) ?
    My aim is to try to create a JSR 168 compliant portlet where few of
    the preferences are allowed to modified by the user (in the edit
    mode), while all other preferences may be modified only by a portal
    admin (thru admin view). However, the PortletPreferences.store()
    method seems to store values of all current preferences for the
    particular user, with the result that future changes by the admin are
    not reflected for any user who has already customized his
    preferences.You can let the admin propagate changes to the user via admin tools.
    There is an option in admin tools to do that.
    Setting the preference <read-only> attribute to 'true' does not help
    either, as the store method still attempts to store the value and
    then throws a ReadOnlyException.In this case, the setValue/setValues/reset and not the store will throw
    the ReadOnlyException.
    Why does WLP attempt to store all preference values; as opposed to
    setting only those which have been explicitly set using
    PortletPreferences.setValue(key, value)?The implementation does not keep track of changes explicitly in memory.
    Even if it keeps track of changes, I'm not sure if this would solve your
    use case.
    Will BEA be changing this in future releases, and is there a
    work-around for now?The suggested approach is to let admins propagate changes to users.
    Subbu
    >
    Thanks, Betsy
    Although WLP does not support the notion of sharing preferences,
    WLP lets you manage preferences in a hierarchy via the admin tools.
    When an admin sets preferences on a portlet, as long as users don't
    customize their portlets, all users get the same portlet instances,
    and hence share preferences.
    Subbu
    Bob said the following on 02/12/2004 04:08 AM:
    Hello,
    Is it possible to share portlet preferences amongusers? So that the
    portlet preferences would be stored per portletinstance, and would be
    the same for every user?
    If this is not possible with Java portlets(JSR-168), can it be done
    with other portlet types (JSP portlet, page flowportlet), using the
    PortletPreferences API?
    The JSR-168 specification says:
    "Portlet Specification assumes preferenceattributes are user specific,
    it does not make any provision at API level or atsemantic level for
    sharing preference attributes among users. If aportal/portlet-container
    implementation provides an extension mechanism forsharing preference
    attributes, it should be well documented how thesharing of preference
    attributes works."

  • How to print on shared printer connected by an ethernat cable to a network through an AirPort Extreme wireless system

    How do you print on a shared printer connected by an ethernet cable to a network on a AirPort Extreme unit?

    I don't believe there's a way to print to it from an iPad ... unless the printer itself has wireless capability.
    This should help:
    AirPrint basics

  • Using a shared internet connection?

    my workplace doesn't have a wireless network, but i can plug in my macbook and was hoping to use it as a wireless network for my ipod touch. i enabled a shared ethernet connection via the airport on my mac, but my ipod touch doesn't find it as a wireless network.
    am i doing something wrong?

    Airplay from what?  iTunes or iPad?
    I assume iPad - in this setup the iPad and AppleTv will be unlikely to be able to see each other on the hotel network even if both are connected wirelessly.  If they can and you don't have a user/room  specific login I'd be concerned about other users seeing my devices too.   These things are generally designed for single user access not to create your own local network.  I suspect in the T&Cs for the wi-fi service it may preclude sharing the internet connection with another device, but if it works....

  • Having problems sharing internet Connection from Macbook to Vonage Adapter

    Hello,
    I just got my Vonage adaptor and I am trying to share the Internet connection from my Macbook Pro (10.7.3) to the Adaptor to use the Vonage service bc my router is downstairs.
    I turned off my Firewall, hooked my Vonage adapter to the Macbook with the ethernet cable and then went to Sys Preferences > Sharing > Selected Internet Sharing > Share Connection from Wifi to computers using Ethernet, and it looks like everything is ok but when i try to use the phone which is connected to to Vonage Adapter, it says "Your Vonage device cannot connect to the internet, check to see if your high speed internet is down".
    I went and looked at my Network preferences and it shows Ethernet connected which is the Vonage Adapter and gives the following:
    IP Address: 169.254.163.167
    Subnet Mask: 255.255.0.0
    Is there something Im doing wrong? Ive seen on several forums that its possible to do but having bad luck. Any help is appreciated.

    All I know is that I got Ubuntu to share internet and offer IPs through DHCP by using Firestarter. I am very new to Solaris, however, and am looking forward to learning about it. I've just made it my regular desktop environment. Long as you can get Solaris to configure its network interface with DHCP after your Ubuntu is configured, you should be able to share to as many clients as you'd like. Fedora also works well for that, and in my opinion, it's a better Linux than Ubuntu, though Solaris remains a better Linux than Linux, right? Har.

  • Sharing internet connection over bluetooth

    i have 2 computers less than 10 metres apart, one of which is connected to the internet broadband. the solution for sharing the connection wirelessly with this particular setup is rather expensive.
    i was wondering if it would be possible to share the internet connection on the computer that has it over bluetooth with the other computer.
    i read an archived response suggesting that it was. can someone confirm?
    just to be clear:
    i hoping to have 2 computers bluetooth connected the second of which will be able to connect to the internet via the 1st computers connection.

    While it is not explicitly supported by OS X, both machines can be configured to do this. For lots of reasons, I don't think that this is a good idea. Your Bluetooth link is so slow compared to either a wireless—or even much faster terrestrial—ethernet link, that it's not worth the effort to enable this service.
    Save yourself a great deal of grief and disappointment, and get ahold of a relatively inexpensive ethernet gateway [wired or wireless] and set up a small local area network. You will be far more secure, and enjoy a far better level of performance than you would see if you attempted to create a Bluetooth LAN.
    That said, here is a set of instructions for enabling internet sharing using Terminal commands. It's paraphrased from…
    http://www.macgeekery.com/hacks/pppoverbluetooth
    In the Bluetooth preference pane's sharing tab, make a new Serial Port Service and name it bt-ppp and make it an RS-232 device. If it's on, disable internet sharing in the Sharing prefpane. Then create the following script [shown below.]
    The script starts pppd on the new Bluetooth serial device you've created, then sets up IP forwarding, NAT, and the like. The en0 can be replaced with en1 if your primary net connection is AirPort. You can change the IP listed to something appropriate for your network, as well. If you put it in the range of IPs you're already using for your home network, natd is smart enough to work as a router instead of a gateway. Run as root, but—for the love of all that is holy and just—use sudo instead of su.
    This is the script you must run in terminal:
    /usr/sbin/pppd /dev/tty.bt-ppp 115200 noauth local passive proxyarp asyncmap 0 silent persist :10.1.1.25 &
    /usr/sbin/sysctl -w net.inet.ip.forwarding=1
    /usr/sbin/natd -same_ports -use_sockets -log -deny_incoming -interface en0
    /sbin/ipfw add divert natd ip from any to any via en0
    If you are unfamiliar with issuing commands from the Terminal or are not familiar with what these unix commands mean, I would suggest that you proceed very cautiously, and do more research before attempting this.

  • Sharing internet connection using 4200 speedstream with airport express

    I have xp windows sp2 loaded on a desktop with ethernet connection to a modem and have a macbook pro laptop using airport express. I have to reset the modem to full bridge mode to access the internet when using airport express wireless connection,this disables the connection to the internet on my desktop ( the error displayed is low conductivity through the modem). To get my desk top to work on the internet I have to reset the modem and set it to optus bridge. This allows access to the internet but disables my airport express connecton.
    Is there a way to allow sharing my connection to the internet without reseting the modem.

    G'day mate,
    The SpeedStream 4200 supplied to Telstra BigPond customers is a single user device, connect it to a 'Switch' or 'Router' to share the Internet Service or, replace the whole thing with an ADSL2+ Wireless Modem Router and get coverage all over your house.

  • How to deal with a shared network connection when installing 10 EX

    I am in the process of setting up a Virtual PC XP environment on my Vista Business machine to load Oracle EX in the virtual environment. I am having an issue of receiving a shared network connection with my broadband wireless card within the virtual XP SP3 environment, which Internet access is required for the Oracle EX installation. I checked out a couple tech sites and could not find a solution. I might have to get an Internet access at home from TWC.
    How to resolve this conflict? Thanks.
    Edited by: user10368779 on Aug 27, 2009 2:09 PM

    I am not sure how +"I am having an issue of receiving a shared network connection with my broadband wireless card within the virtual XP SP3 environment"+ can be resolved on an Oracle forum? There are probalby Windows experts on the forum but I would personally aim for a Windows forum.
    You may want to double check if Oracle XE is even running in a Virtual PC environment before starting this exercise.
    -Andyt

  • Shared Variable connectivity in LabVIEW Mobile

    Hello,
    I'm developping an application for a portable device running Pocket PC 2003 O/S.
    I'm trying to figure out whether shared variable connectivity VIs (Open, Read, Write, etc..) are supported in LabVIEW Mobile 2010 module or not?
    Is dragging shared variables from the "My Computer" target directly under "Pocket PC 2003 Device" target the only way to use shared variables in LabVIEW Mobile?
    Thanks.

    Is NI planning to implement progamatically connecting/reading/writing/closing of SV's functionality using the PDA/LabVIEW Mobile module ?.
    In my company we have started to include a cFP 2220 + various I/O modules with one of our products - might I say thats not easy when the Siemens guys can sell their PLC's at a fraction of the NI cost- but please do read on...
    As a user-interface an 8" touch panel is used. One of the problems is that I have to "hardcode" the IP address of the SV's used on the touch panel to connect to specific variables on the cFP 2220 var. engine. This gives the tedious work of recompiling the touch panel code over and over again for specific customers needing specific IP addresses for their cFP controllers. And if the customer uses MAX to change the IP address of the cFP, then his GUI stops working.
    Initiallly I had this problem with the cFP 2120 controller, and thought that my problems would be solved when the cFP2220 came out, since it has two network cards. One card could then be used for "local" communication with the touch panel/GUI and the other for the company network of our customer. Needless to say I was sad to find out that the var. engine can only run on one of the cards - not both.
    The only other solution I see it to implement a homemade protocol via TCP/IP, but this is a lot of work and not really the intention of the Shared Variable.
    Any suggestions to a soltion of the above problem would be greatly apriciated.
    /søren jensen

  • Modify Database Shared/Dedicated Connections

    Newbie question - we're running Grid Control 10.2.0.4 on RHEL 4. I have quite a few databases on other servers configured via the Grid Control GUI. Recently we discovered Grid Control was connecting to these databases via a SHARED Oracle network connection, and I would like to change this to a DEDICATED one. However, none of these databases are listed in any of the .../network/admin/tnsnames.ora files, so I'm guessing there is somewhere else to configure this in Grid Control?
    I've looked on this forum and searched through the Grid Control manuals, but for the life of me I can't find where/how to configure this, so I'm guessing that I'm probably looking with/for incorrect terminology. I'm okay with reading up on how to do this, but am just wondering if someone can point me in the right direction of where to read on this? Thanks in advance for any input!

    Take a look at Metalink Doc ID 757339.1 "How to : Configure Grid Control Agent to Use Dedicated Server Processes".
    The agent will automatically make use of shared server connections, if the database is running in the shared server mode. But it seems you can edite the targets.xml file from the agent side edit to use dedicated.

  • Firewall blocks some services when sharing internet connection

    Hello,
    I have some issues regarding internet sharing that I hope someone could successfully troubleshoot :
    2 computer, iMac G5 2.0 and an original "17 PowerBook G4 1.0, both running 10.4.7.
    The iMac is connected to the internet via Ethernet and shares its connection with the PB using the Airport.
    The problem is that when the Firewall is enabled (just using the built-in one) the shared connection is limited for only few services :
    Web browsing, iPhoto and iTunes Bonjour sharing, Apple Remote Desktop all work smoothly while iChat, MSN Messenger, Bittorrent clients can't connect and Mail can't go Online (can't connect to my Gmail accounts). Since even enabling all default services in the list doesn't help the only solution is to completely disable the Firewall in the iMac. When the Firewall is off everything goes back to normal.
    I tried to isolate the problem but I can't get my finger on the right ports to open.
    I tested sharing the connection through Firewall instead but it's the same so It's definitely not related to the type of connection used to connect the two computers one to the other. It is strictly related to the Firewall.
    I found two Apple documents :
    http://docs.info.apple.com/article.html?artnum=107653
    http://docs.info.apple.com/article.html?artnum=107594
    I carefully followed the instructions but it didn't resolve the problem.
    From reading the first document I learnt that port 443 is related to the Secure Sockets Layer service so I searched Apple document http://docs.info.apple.com/article.html?artnum=106439 for other ports related to that service but since I'm not an expert I couldn't figured it out right by myself.
    Setting for the iMac side are as followed :
    Network panel : Airport is active. (as recommended in http://docs.info.apple.com/article.html?artnum=107594 I set Airport to the highest port priority).
    Sharing panel/Services : Personal web sharing is set to ON. As I mentioned before even enabling all default services in the list doesn't help.
    Sharing panel/Internet : Internet sharing is ON. "Share your connection from" is set to Airport. "To computers using" : Airport checkbox checked. I tried the Firewall option instead as well.
    Appleshare is ON and automatically configured (zero configuration in the Firewall) on both macs.
    Ports for iChat, MSN and Gmail on the iMac side are open. iMac has no problem to connect to these services directly.
    On the PB side turning the Firewall on or off resulted the same.
    Could someone please help me to configure the Firewall so it won't have to be always turned off?
    Your help is much appreciated
    Elad
    Original PowerBook "17/iMac G5 2.0 "20   Mac OS X (10.4.7)  

    In the Sharing pane of System Preferences, click the Advanced button under the Firewall tab, enable firewall logging, and then try using those services on the other computer. When done, check the firewall log and look for the number after the : in the logged entries; this is a port number. Knowing the IP address of the other machine will help determine which entries were produced by it as opposed to ones which came from the Internet; this is visible in the Network pane of System Preferences.
    (15371)

  • Dedicated Server Connection Vs Shared Server Connection

    Hi Gurus,
    I have few doubts regarding Dedicated Server Connection Vs Shared Server Connection -
    1) How do I know which connection mechanism is configured in my system.
    2) Which connection faster and reliable.
    3) Is there any risk to using shared server connection?
    4) Which concept is new comparatively?
    5) It is possible to restrict specific user for Dedicated Server Connection and other for Shared Server Connection in a same database.
    Regards,
    Atanu

    AChatterjee wrote:
    Thanks for your post.... but still i have doubt on below points -
    1) How do I know which connection mechanism is configured in my system.Search for dispatcher parameter (both words in the search box) in the docs.
    2) It is possible to restrict specific user for Dedicated Server Connection and other for Shared Server Connection in a same database.You might be able to do something like this by using the RULE parameter with cman, limiting specific users (via local security setups) to specific client IP's and forcing them to particular service names. This is a net services issue, so does not conflict with what Billy said. It assumes you control whether your users will query the db appropriately, too. Many apps don't do that right.

  • Share database connection among multiple crystal reports using PULL model

    Hi All,
    Is there any way to share database connection using crystal report PULL method ? I m using crystal report 2011 with visual c# (VS2010). Reports get connected to database server using Oracle Native client drivers.
    Thanks,
    Gaurav

    In crystal report 8.5, we have used "PESetNthTableLogonInfo" (after PELogOnServer got deprecated) which shares the connection among reports. Is there any way to implement the same functionality in crystal reports.
    Thanks,
    Gaurav

  • Connecting to shared internet connection through Mac osx 10.7 wifi

    I can't get my new HP touch pad to connect to my wireless network. I connect my iMac through ethernet but have a wifi network setup from my iMac so my other devices can use my iMacs shared internet connection. The touchpad will not connect, it finds the network, I put in the WEP key and it states "unable to connect, try again". I am at the setup stage on the touch pad and can't get any further!!. I have tried everything I can think of including creating a new wireless network, disabling and changing the sharing password and re-enabling. all my other devices still log on fine and can connect to the internet through the iMacs connection. anyone got any ideas? 
    Post relates to: HP TouchPad (WiFi)

    alopix wrote:
    I just remembered why I ignored your first post when reading this topic:
    Ad-hoc-sharing can't be the problem, because when I use the exact same method without any WEP encryption, so create an open network, it works perfectly - so it has to be an error in the webOS' handling of WEP or similar...
     I'm curious that the Mac offers only ASCII (key length of 5 characters) or 128 bit encryption, but not 64bit (which has a key length of 10 hexadecimal characters and works perfectly with my touchpad).  <Wiki definition of WEP.>
    This sounds like an old problem we used to have when Palm came out with WiFi-enabled devices. Users would create an ASCII text password in their router, and it would generate a 10 character hexadecimal string that was the true password.  Entering the 5-digit ASCII code would fail since it was a seed, and not the "real" code.. Entering the 10-digit key worked every time.
    By any chance is this what is occurring on the Mac? Do you see a 10 digit code generated anywhere in the setup routine?
    Was the link I provided to you of any help?
    WyreNut 
    I am a Volunteer here, not employed by HP.
    You too can become an HP Expert! Details HERE!
    If my post has helped you, click the Kudos Thumbs up!
    If it solved your issue, Click the "Accept as Solution" button so others can benefit from the question you asked!

Maybe you are looking for

  • Newb: cannot view PDF files in Web Browser

    I have a new Windows 7 computer & have installed Acrobat Standard 9.3.2 and Reader 9.3.1. Whenever I attempt to "download" (i.e. view) a PDF in IE browser (or Firefox 3.6), I get the following error message:     "The Adobe Acrobat/Reader that is runn

  • How do i create a new table from a *.sql file in JSP

    <html> <%@ page language="java" import="java.sql.*" %> <%@ page errorPage="ExceptionHandler.jsp" %> <% Class.forName("com.mysql.jdbc.Driver").newInstance(); String dbUrl = "jdbc:mysql://localhost/test?user=root"; Connection conn = DriverManager.getCo

  • MaxDB and LiveCache installation

    Hello All, Tying to install LiveCache 7.7 part SCM 5.1 = SCM 2007 I am trying to install liveCache on SuSE 10(Linux) with MaxDB. I would like to know that if I need to install MaxDB before I install the Livecache server using sapinst? Please advice!

  • HT4623 i can't the new i0s 7 update on my ipad 3! what should i do

    i waited until september 18th to download the new i0s 7, but it said that my current software, i0s 6.1.3 was up to date. WHAT SHOULD I DO!!!!?????

  • Popup crashes (hangs up) when second popup closed

    Hi experts, I've got a weird behaviour... After opening and closing a second popup (P2) in a popup (P1) (e.g. P2 = F4-help for a field of P1), P1 crashes/hangs up. Even within standard component like SALV_WD_TABLE. Weird thing is, that this only occu