0

I build webApp . When I run app from Eclipse everything work OK. Now I build WAR file, put in tomcat root, andI want to all my system property put in context.xml in tomcat. Then, when app is start, that values from context.xml is read and use in app. Problem is I don't know how to get values from context.xml in my webapp? This is context:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="" docBase="fileupload.war" privileged="true"    antiResourceLocking="false" antiJARLocking="false">
    <Resource 
        mail.smtp_server = "127.0.0.1"
        mail.smtp_username = "[email protected]"
        mail.mailTo = "[email protected]"
        rootFolder = "D:/project/"
        rootFolderBackup = "D:/project/Backup/"
     />

Here I want to get a values from context.xml :

@RequestMapping(value="api/files")
public class FileController {

final static Logger logger = Logger.getLogger(FileController.class);
@Autowired
ApplicationContext applicationContext;

private String mailTo;
private String sender;
private String rootFolder = "";
private String rootFolderBackup = "";   

@PostConstruct
private void init(){

        try {
            InitialContext ic = new InitialContext();
            Context xmlContext = (Context) ic.lookup("java:comp/env"); // thats everything from the context.xml and from the global configuration
            DataSource myDatasource = (DataSource) xmlContext.lookup("rootFolder");
        } catch (NamingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

}

This is common error :

Name [rootFolder] is not bound in this Context. Unable to find [rootFolder].

I try few solution which I found on web but not working... My context.xml file is in C:\tomcat-7.0\conf\Catalina\localhost , and war file is in C:\tomcat-7.0\webapps

1 Answer 1

2

If you want to define application properties you should probably use Environment variables instead. Ex you can set a variable in any context.xml like this:

<Environment name="PROPERTIES_FILE" override="false" type="java.lang.String" value="C:/path/to/propfile.properties" />
<!--define more variables here -->

and then in your application you can read those environment properties from JNDI like this.

initCtx = new InitialContext();
String path = (String) initCtx.lookup("java:comp/env/PROPERTIES_FILE");
// fetch more variables here

Further reading:

https://tomcat.apache.org/tomcat-8.0-doc/config/context.html#Environment_Entries

https://tomcat.apache.org/tomcat-8.0-doc/jndi-resources-howto.html

Sign up to request clarification or add additional context in comments.

3 Comments

I put Environment in Context but now is error : Name [PROPERTIES_FILE] is not bound in this Context. Unable to find [PROPERTIES_FILE]. , in java init() I put your code InitialContext initCtx = new InitialContext(); String path = (String) initCtx.lookup("java:comp/env/PROPERTIES_FILE");
Can't say what is wrong in your particular case, "works on my machine" and that is how it is documented. Possibly you are putting the context.xml in the wrong place.
Where to put Environment , in context.xml from tomcat or in mywebapp.xml(which in tomcat / conf/catalina/localhost folder ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.