I would like to set some values in context.xml file and access the same from my Servlet like we access in JNDI:
mail.smtp.host=smtp.gmail.com
mail.smtp.port=465
Can I do this?
Yes, see above, and you can do even better than that: you can put the whole mail Session into 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="[email protected]"
      mail.user="xyz"
      mail.host="xyz.com"
      mail.debug="false"
      password="xyz"
      />
Then just look that up as java:comp/env/mail/xyz and it is a javax.mail.Session.
Note that if you provide the password attribute, Tomcat will install an Authenticator for you as well.
Yes its absolutely possible
<Environment name="testEnvEntry" value="Got It"
         type="java.lang.String" override="false"/>
Then access this like:
Object lookedUp = null;
try {
    InitialContext initialContext = new InitialContext();
    lookedUp = initialContext.lookup("java:/comp/env/testEnvEntry");
} catch (NamingException e) {
    e.printStackTrace();
}
It is similar to how you would add <env-entry> in your web.xml.
You can read the official documentation of Environment here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With