Note that there are some explanatory texts on larger screens.

plurals
  1. POmail/Session Resource Factory does not work in Struts application
    primarykey
    data
    text
    <p>I want to use the standard resource factory provided by Tomcat 6.0, which create javax.mail.Sessions instance for me. As described in the <a href="http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html" rel="nofollow noreferrer">JNDI Resource HOW-TO tutorial</a>.</p> <p>My META-INF/context.xml looks like: </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Context reloadable="true"&gt; &lt;Resource name="mail/Session" auth="Container" type="javax.mail.Session" mail.smtp.host="smtp.gmail.com" mail.smtp.port="587" mail.smtp.auth="true" mail.smtp.user="someone@gmail.com" mail.smtp.password="secretpassword" mail.smtp.starttls.enable="true"/&gt; &lt;/Context&gt; </code></pre> <p>I have the next resource-ref in my WEB-INF/web.xml, just before &lt;/webapps>. Web.xml validates. I validated using <a href="https://stackoverflow.com/questions/15732/whats-the-best-way-to-validate-an-xml-file-against-an-xsd-file/16054#16055">McDowell's way</a>. </p> <pre><code>&lt;resource-ref&gt; &lt;description&gt;Resource reference to a factory for javax.mail.Session instances that may be used for sending electronic mail messages, preconfigured to connect to the appropiate SMTP server. &lt;/description&gt; &lt;res-ref-name&gt;mail/Session&lt;/res-ref-name&gt; &lt;res-type&gt;javax.mail.Session&lt;/res-type&gt; &lt;res-auth&gt;Container&lt;/res-auth&gt; &lt;/resource-ref&gt; </code></pre> <p> </p> <p>I am using the next code snipett access my javax.mail.Session object. </p> <pre><code>Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); Session session = (Session)envCtx.lookup("mail/Session"); System.out.println("HERE smtp.user: " + session.getProperty("mail.smtp.user")); </code></pre> <p>I tested it in a sample app and it worked. Unfortunately when I moved the same code to a struts application, I get NULL in the above print statement. I do the context look up in singleton Class called mailer (which is defined in my WEB-INF/classes folder) but I get the same problem if i do the context look up in Struts action class.</p> <p>I have been thinking about what is different to find the problem. My struts application web.xml is more complicated than simple application's web.xml. It has security-constraint, filters and the Struts Servlet configuration. I position resource-ref just before the servlet definitions. It seems like the resource-ref is being ignored. </p> <p>I have another issue. If I have the mailapi.jar, needed by the javax.mail.Session, in the myapp/WEB-INF/lib folder I get: </p> <p>java.lang.NoClassDefFoundError: javax/mail/Authenticator</p> <p>If I put it in $CATALINA_HOME/lib is found.</p> <p>Any ideas? I use struts and hibernate. Maybe it has something to do with that.</p> <h2>Debug</h2> <p>I tried to debug it puting the debug attribut in context </p> <pre><code>&lt;Context reloadable="true" debug="99" ... </code></pre> <p>But I do not see anything interesting.</p> <pre><code>01-feb-2009 17:39:09 org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() 01-feb-2009 17:39:09 org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized() 01-feb-2009 17:39:09 org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() 01-feb-2009 17:39:09 org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized() 01-feb-2009 17:39:09 org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() 01-feb-2009 17:39:09 org.apache.catalina.core.ApplicationContext log </code></pre> <p>I tried:</p> <pre><code>Session session = (Session) initCtx.lookup("java:comp/env/mail/Session"); </code></pre> <p>instead of:</p> <pre><code>Context envCtx = (Context) initCtx.lookup("java:comp/env"); Session session = (Session)envCtx.lookup("mail/Session"); </code></pre> <p>But I am still getting a NULL Session object.</p> <h2>Partial Solution</h2> <p>When I put the Resource element inside $CATALINA_HOME/conf/context.xml file, it works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload