This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Running JSP from outside tomcat\webapps\ROOT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Running JSP from outside tomcat\webapps\ROOT" Watch "Running JSP from outside tomcat\webapps\ROOT" New topic
Author

Running JSP from outside tomcat\webapps\ROOT

abhijeet srivastava
Greenhorn

Joined: Aug 22, 2006
Posts: 26
Hi!

To run jsp applications, we save jsp files in tomcat\webapps\ROOT folder.
But if I want to execute jsp files from anywhere but not in ROOT folder, let in c:\Test folder then what should I do? what would be changes in configuration? and which file configuration should be changed?

Bye....
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

To run jsp applications, we save jsp files in tomcat\webapps\ROOT folder.

No, you usually make a new folder in webapps. For example :
webapps/myapp/hello.jsp

And access it (after rebooting Tomcat) via localhost:8080/myapp/hello.jsp


[My Blog]
All roads lead to JavaRanch
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by abhijeet srivastava:

But if I want to execute jsp files from anywhere but not in ROOT folder



Please Check this Link to Set Up Your Development Environment


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are several ways you can do this and they are container specific.
Moving to the Apache Tomcat forum.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Tomcat doesn't run "JSPs".
It runs web applications (which can contain JSPs).

So, the first thing you need to do is create a proper web application directory structure.
Suppose your app is to be named "myApp".
Create a directory named "myApp" and put it wherever you like.
Then, inside the myApp directory create another directory named "WEB-INF" (case sensitive).
OK, now you have a proper web application directory structure.
Put your JSPs in the myApp directory.

Now there are several ways you could deploy your web app.
1.) Drop the myApp directory (and all of its contents) in the {Tomcat install}/webapps directory.
Tomcat will automatically deploy the application for you .
You can then access your app with the following URL:
http://localhost:8080/myApp

2.) (this is probably what you're looking for)
Leave the myApp directory where is is and create a context fragment file
that lets Tomcat know where your app is.
Context fragment files go under:
{tomcat install}/conf/Catalina/localhost
The name of the file should match your contextPath ("myApp") with
a ".xml" extension.

Assuming you've created the myApp directory under "C:\Test", here
is an example context fragment file (named myApp.xml).


Once you've created this file and placed it in the tomcat/conf/Catalina/localhost directory, Tomcat will deploy your app.
You will be able to access it with:
http://localhost:8080/myApp


Hope this helps.
[ September 27, 2006: Message edited by: Ben Souther ]
Adam Scheller
Ranch Hand

Joined: Feb 21, 2015
Posts: 138
    
    4

I am sorry for excavation of 9 year old thread, but I just found it in Google and I have a useful comment for future fellow Googlers.

The Context can be also added directly in server.xml inside <Host> element, like this:



In this example, the folder C:\Test\images will be accessible under example.com/images/.


Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17149
    
  27

Adam Scheller wrote:
The Context can be also added directly in server.xml inside <Host> element, like this:





Nonononono!

Do not put Context elements in server.xml.

That practice has been discouraged for many, many years. In fact, I think it was deprecated even at the time this thread was originally started.

If you place a Context element in server.xml, it requires a Tomcat restart to update the context. Plus you're at risk for corrupting the server.xml file and thereby making Tomcat completely unable to start.

You can place a Context in the webapp's META-INF/context.xml file, or as a file (with the same name as the context) under TOMCAT_HOME/conf/Catalina/localhost (assuming you're not using a custom Host configuration).

But don't put it in server.xml!


An IDE is no substitute for an Intelligent Developer.
Adam Scheller
Ranch Hand

Joined: Feb 21, 2015
Posts: 138
    
    4

Thank you very much Tim for informing me about that.

The information about the possibility of placing a Context in META-INF is wonderful. Now I have to remove all the instructions about additional configuration of Host from readme file, because I can easily "embed" the Context in my webapp!

Your arguments certainly show it is not the best practice to nest Context in Host, but I am not sure if it is deprecated at all. Neither Tomcat 7 nor 8 show any deprecation messages. The docs don't say anything about deprecation as well. Docs for Tomcat 8 say that I can nest Context in Host, and there is nothing about deprecation:

Inside the Host element, you can nest Context elements for the web applications associated with this virtual host.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17149
    
  27

The docs probably don't say "deprecated". But if you read them in detail, you will see that they do say NOT to do it. The reason being - like I said (and so should the Tomcat docs) - that placing a Context in server.xml means that you cannot dynamically update the webapp.

The server.xml file only gets digested when Tomcat starts up. so any changes to it will not be detected until the next Tomcat startup.

Context.xml files in conf/Catalina/localhost are normally scanned every few seconds, so changes to those Context definitions get applied fairly rapidly. Without the need to restart Tomcat.
Adam Scheller
Ranch Hand

Joined: Feb 21, 2015
Posts: 138
    
    4

And due to this argument I got my Context moved away from server.xml. Thank you again for the tip.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running JSP from outside tomcat\webapps\ROOT
 
It's not a secret anymore!