Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 719
Joe Avatar asked Dec 27 '22 23:12

Joe


2 Answers

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.

like image 151
user207421 Avatar answered Feb 18 '23 16:02

user207421


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

like image 42
mprabhat Avatar answered Feb 18 '23 16:02

mprabhat