Which Tomcat 5 context file takes precedence?

277

Solution 1

For the files you listed, the simple answer assuming you are using all the defaults, the order is (note the conf/Catalina/localhost):

...tomcat/conf/context.xml
...tomcat/conf/Catalina/localhost/myapp.xml
...tomcat/webapps/myapp/META-INF/context.xml

I'm basing this (and the following discussion) on the Tomcat 5.5 official documentation for the Context Container.

So if that's the simple answer, whats the complete answer?

Tomcat 5.5. will look in a couple of other places for <Context> elements beyond those you've listed (see the official docs).

The META-INF/context.xml will never be opened if Tomcat finds a Catalina/localhost/myapp.xml. So if you actually have all the files above, its more correct to say the the META-INF/context.xml is irrelevant, not that it's the lowest precedence.

If you say <Context override="true" ...> in your Catalina/localhost/myapp.xml that will make it the highest precedence, regardless of conf/context.xml. Same thing in your META-INF\context.xml, as long as you don't have a Catalina/localhost/myapp.xml (see previous paragraph).

Also, the /Catalina/localhost/ portion of the path in the files above actually comes out of the "default" conf/server.xml and matches the <Engine name="Catalina" defaultHost="localhost">. If your server.xml uses different values for name and defaultHost in the <Engine>, that's the dir structure where Tomcat will look.

Finally, for the ...tomcat\ portion of the files you listed, Tomcat uses the dir from the $CATALINA_BASE environment variable. If that's not set, then it uses the dir from the $CATALINA_HOME environment variable, which is the directory of the Tomcat installation. I like to set and use $CATALINA_BASE so that I don't "pollute" my Tomcat installation.

Solution 2

My understanding is:

  • tomcat/conf/context.xml is the "default" context.xml whose contents are overlayed with the webapp context definitions. My TC 5 default context.xml has almost nothing in it, other than listing the web.xml as a watched resource, which supports this notion.
  • tomcat/Catalina//.xml is used for the webapp. Either it is place here manually, or is taken from your webapp at deployment time...so this is the real master that TC uses. If you edit this changes will be read next start.
  • tomcat/webapps/myapp/META-INF/context.xml - this is copied to tomcat/Catalina/ upon initial deployment if you alter this after initial deployment, I don't think that has any effect
Share:
277

Related videos on Youtube

Subhasish Bhattacharjee
Author by

Subhasish Bhattacharjee

Updated on June 01, 2020

Comments

  • Subhasish Bhattacharjee
    Subhasish Bhattacharjee almost 4 years

    I am new to lucene. I want to store and retrieve data from a database. I would also like to store the table information, so that I may be able to tell which table the data belongs to and load the table for the user.

    I am able to store data as text from database, but I am not able to figure out any approach which would let me to know the table from which it has come.

    I have reached to this level, which lets me store data into an indexed file:

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
    
                Events event=Event.getEventById(5);
    
    
                Version matchVersion= Version.LUCENE_30;
                Analyzer analyzer=new StandardAnalyzer(matchVersion);
                boolean recreateIndexIfExists = true;
                File indexDir=new File(INDEX_DIRECTORY);
                Directory fsDir = FSDirectory.open(indexDir);
                IndexWriter indexWriter= new IndexWriter(fsDir,analyzer,MaxFieldLength.UNLIMITED);
    
                if(event !=null){
                    Document doc=new Document();
                    doc.add(new Field("field", event.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
                    doc.add(new Field("field", event.getPlace(), Field.Store.YES, Field.Index.ANALYZED));
                    doc.add(new Field("field", event.getDescription(), Field.Store.YES, Field.Index.ANALYZED));
    
                    indexWriter.addDocument(doc);
    
                }
                indexWriter.optimize();
                indexWriter.close();
                searchIndex("first");
        }
    

    This is to search from the index file:

    File indexDir=new File(INDEX_DIRECTORY);
            Directory fsDir = FSDirectory.open(indexDir);
            if(indexDir.exists()){
                System.out.println("index file found");
            }else{
                System.out.println("not found");
            }
            IndexReader indexReader=IndexReader.open(fsDir);
            IndexSearcher searcher=new IndexSearcher(indexReader);
    
            Version matchVersion= Version.LUCENE_30;
            Analyzer analyzer=new StandardAnalyzer(matchVersion);
    
            QueryParser parser=new QueryParser(matchVersion, "field", analyzer);
            Query query;
    
            try {
                query = parser.parse(searchString);
                int n=10;
                TopDocs topDocs= searcher.search(query,n);
    
                ScoreDoc[] scoreDocs=topDocs.scoreDocs;
                System.out.println("hits= "+scoreDocs.length);
                for (int i = 0; i < scoreDocs.length; ++i) {
                    ScoreDoc sd = scoreDocs[i];
                    System.out.println("scoredocs: "+scoreDocs[i]);
                    float score = sd.score;
                    int docId = sd.doc;
                    Document d = searcher.doc(docId);
                    Field value = (Field) d.getField("place");
                    System.out.println("placesdfgddd: "+value);
    
                }
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
  • Ittai
    Ittai over 13 years
    Wow, a very detailed and impressive answer. Thank you.