[s2] accessing servlet context from an action
Hi, I'm familiar with Struts 1, looking at Struts 2. The web app I am working on needs to use data sources that are controlled by tomcat connection pooling and accessed through JNDI. For this I need access to the servlet context that the action is running under. Previously I had a base action class that all actions extended and provided a method : protected Connection getConnection() { Connection conn = null; try { String datasourceName = jdbc/ + getServlet().getServletContext().getInitParameter(database); Context initCtx = new InitialContext(); Context envCtx = (Context)initCtx.lookup(java:comp/env); DataSource ds = (DataSource)envCtx.lookup(datasourceName); log.debug(retrieving database connection for + datasourceName); conn = ds.getConnection(); log.debug(done); conn.setAutoCommit(false); return conn; } catch (SQLException ex) { log.error(error getting a connection, ex); return null; } catch (NamingException ex) { log.error(error finding the datasource, ex); return null; } } However, I am completely lost with Struts 2. Everything servlet dependent is abstracted away somewhere. I've been looking at the tutorials and they don't cover this kind of thing at all. How do I go about putting the equivalent in an action with Struts 2? Thanks Chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
--- Chris Cheshire [EMAIL PROTECTED] wrote: How do I go about putting the equivalent in an action with Struts 2? I don't actually know if this is still valid, and I can't test it at the moment, but: http://struts.apache.org/2.x/docs/accessing-application-session-request-objects.html I believe this issue has also been discussed in the archives and is probably more up-to-date than that page. d. Food fight? Enjoy some healthy debate in the Yahoo! Answers Food Drink QA. http://answers.yahoo.com/dir/?link=listsid=396545367 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
However, I am completely lost with Struts 2. Everything servlet dependent is abstracted away somewhere. I've been looking at the tutorials and they don't cover this kind of thing at all. How do I go about putting the equivalent in an action with Struts 2? An action class can implement ServletContextAware - you have to implement a method setServletContext(ServletContext servletContext) then. Another way is to use ServletActionContext.getServletContext(); Piero - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
--- Piero Sartini [EMAIL PROTECTED] wrote: An action class can implement ServletContextAware - you have to implement a method setServletContext(ServletContext servletContext) then. Another way is to use ServletActionContext.getServletContext(); Definitely use these over what I suggested; I totally spaced it. I will create a FAQ entry. d. Bored stiff? Loosen up... Download and play hundreds of games for free on Yahoo! Games. http://games.yahoo.com/games/front - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
I will create a FAQ entry. good idea. maybe it makes sense to merge it with this one: http://struts.apache.org/2.0.6/docs/how-can-we-access-the-httpservletrequest.html Piero - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
On 3/23/07, Piero Sartini [EMAIL PROTECTED] wrote: However, I am completely lost with Struts 2. Everything servlet dependent is abstracted away somewhere. I've been looking at the tutorials and they don't cover this kind of thing at all. How do I go about putting the equivalent in an action with Struts 2? An action class can implement ServletContextAware - you have to implement a method setServletContext(ServletContext servletContext) then. Another way is to use ServletActionContext.getServletContext(); Piero Thanks Piero, I'm looking at the javadoc for this, it doesn't say much. Do I just create a class scope variable for the session context and then set that from the argument in the set method? Is this set method called automagically by the framework when the action is first accessed or when the web app is deployed? If I put this in the base action class and extend that for my other actions, will this cause problems, or does each final action class need to implement this? Chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
--- Chris Cheshire [EMAIL PROTECTED] wrote: I'm looking at the javadoc for this, it doesn't say much. Do I just create a class scope variable for the session context and then set that from the argument in the set method? Yep. Is this set method called automagically by the framework when the action is first accessed or when the web app is deployed? If I put this in the base action class and extend that for my other actions, will this cause problems, or does each final action class need to implement this? Actions are instantiated per-request. d. Sucker-punch spam with award-winning protection. Try the free Yahoo! Mail Beta. http://advision.webevents.yahoo.com/mailbeta/features_spam.html - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
Thank you muchly! On 3/23/07, Dave Newton [EMAIL PROTECTED] wrote: --- Chris Cheshire [EMAIL PROTECTED] wrote: I'm looking at the javadoc for this, it doesn't say much. Do I just create a class scope variable for the session context and then set that from the argument in the set method? Yep. Is this set method called automagically by the framework when the action is first accessed or when the web app is deployed? If I put this in the base action class and extend that for my other actions, will this cause problems, or does each final action class need to implement this? Actions are instantiated per-request. d. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] accessing servlet context from an action
I'm looking at the javadoc for this, it doesn't say much. Do I just create a class scope variable for the session context and then set that from the argument in the set method? That is exactly how it works. You might want to look at ServletSessionAware, ServletRequestAware, ServletResponseAware as well - it works the same way. Is this set method called automagically by the framework when the action is first accessed or when the web app is deployed? I think it is called when the action is instantiated, but I am not totally sure. If I put this in the base action class and extend that for my other actions, will this cause problems, or does each final action class need to implement this? This should not make any problems. You can extend a generic action that implements all the stuff that is common to all your actions. I am doing it the same way and it works well. One thing you have to take care of is that there are issues if you want access in the constructor of an action (struts has no chance to call the set method then). Piero. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]