Is it possible to set the value and access in context.xml in tomcat7, like JNDI?

I would like to set some values ​​in the context.xml file and access the same from my servlet as in JNDI:

mail.smtp.host=smtp.gmail.com mail.smtp.port=465 

Can I do it?

+4
source share
2 answers

Yes its absolutely possible

 <Environment name="testEnvEntry" value="Got It" type="java.lang.String" override="false"/> 

Then follow these steps:

 Object lookedUp = null; try { InitialContext initialContext = new InitialContext(); lookedUp = initialContext.lookup("java:/comp/env/testEnvEntry"); } catch (NamingException e) { e.printStackTrace(); } 

This is similar to how you added <env-entry> to your web.xml .

You can read the official Environment documentation here

+5
source

Yes, see above, and you can do even better: you can put the entire mail session in context.xml:

  <Resource name="mail/xyz" type="javax.mail.Session" auth="Container" mail.pop3.connectiontimeout="60000" mail.pop3.host="pop.hhhh.net" mail.pop3.port="110" mail.pop3.timeout="60000" mail.smtp.auth="true" mail.smtp.connectiontimeout="60000" mail.smtp.host="smtpout.hhhh.net" mail.smtp.port="3535" mail.smtp.sendpartial="true" mail.smtp.timeout="60000" mail.store.maildir.autocreatedir="true" mail.store.protocol="pop3" mail.transport.protocol="smtp" mail.from=" abc@xyz.com " mail.user="xyz" mail.host="xyz.com" mail.debug="false" password="xyz" /> 

Then just look at java:comp/env/mail/xyz and this is javax.mail.Session.

Please note that if you provide the password attribute, Tomcat will also install Authenticator for you.

+5
source

All Articles