Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change HTML/JS/CSS without restarting Tomcat?

Tags:

tomcat

tomcat7

Is it possible to configure Tomcat such that it looks in a separate location for my 'webapp' directory while in development?

I have my git source in one directory and tomcat installed in another. I don't want to have to make changes in two places, or manually copy files over, or rebuild/deploy every time I make a simple change to CSS or Javascript.


EDIT - 2/21/2013

Unfortunately none of the suggestions worked and I suspect it may be because I didn't provide enough information about how I have things laid out.

I have Tomcat installed in a directory of my home directory (I'm on a Mac) called "Development".

/Users/dbrogdon/Development/apache-tomcat-7.0.35

I have my git source next to that.

/Users/dbrogdon/Development/myproject

In the myproject directory, my actual web files are located in:

/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp

When I compile I put the appname.war into

/Users/dbrogdon/Development/apache-tomcat-7.0.35/webapps

Based on that updated information, what should I be providing as my docBase in either my conf/server.xml or conf/Catalina/localhost/appname.xml?

like image 797
Darrell Brogdon Avatar asked Feb 12 '13 00:02

Darrell Brogdon


People also ask

Where does tomcat put HTML files?

Now, we have confirmed that Tomcat server is running as a Web server: Default base URL: http://localhost:8080/ Folder where HTML documents are served from: C:\local\tomcat\webapps\ROOT\

Can tomcat run Javascript?

The simplest way to do this is put the html, javascript, and a jsp page in the same war file, and then run it on tomcat.


4 Answers

In your tomcat installation, under directory "conf", you will find a file server.xml. In that file search for the tag "Host". You will find something like

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

Change appbase to the parent folder of your web application directory.So say your application "myapp" directory is under say c:\git\source , put appBase as "c:\git\source"

Restart your tomcat. Now your tomcat will look inside "c:\git\source" for all the web applications it has to deploy instead of webapps

Link to tomcat configuration describing appbase usage http://tomcat.apache.org/tomcat-7.0-doc/config/host.html

The Application Base directory for this virtual host. This is the pathname of a directory that may contain web applications to be deployed on this virtual host. You may specify an absolute pathname, or a pathname that is relative to the $CATALINA_BASE directory. See Automatic Application Deployment for more information on automatic recognition and deployment of web applications. If not specified, the default of webapps will be used.

like image 168
Zenil Avatar answered Oct 08 '22 20:10

Zenil


Possible ways to do what you are looking forward to

  • Configuring Apache web server for serving static content and routing the dynamic content to Tomcat server. This is done using mod jk connector which proxies the JSP request to tomcat but by passes this proxy for serving static content like image,js etc

Done using in worker.properties

<Location *>
SetHandler jakarta-servlet
SetEnvIf REQUEST_URI \.(html|png|gif|jpg|css|js)$ no-jk
</Location>

This might not be advisable as it changes all your deployment architecture

  • The solution that Zenil has proposed configuring server.xml in your conf

The limitation will be that all the applications that is deployed in your tomcat will be served from the D:/newWebApps.This might not serve your purpose as you need only the application that is in your Source repository to be mapped with your tomcat.

  • I think the below will be more appropriate for you in the given scenario

Create a file applicationName.xml & define following

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="D:\newWebApps" path="/js/*" />

and put this file in your \conf\Catalina\localhost directory. The tomcat will pick this up automatically

Now you can access this by

http://servername:8080/applicationName/js/anything.js

Refer this http://tomcat.apache.org/tomcat-7.0-doc/config/context.html for more details on setting docBase outside tomcat default diretories

like image 23
Manish Singh Avatar answered Oct 08 '22 20:10

Manish Singh


You can use a local Apache server, and rewrite the HTML/CSS/JS/ files to be picked up from a separate location.

See this question for an example how to rewrite JS files: HOWTO: rewrite requests for images, css and js to different folders for each application?

Now you can configure Apache to go to Tomcat for the tomcat URLs, but perform these rewrites in the apache config and css/js/html files can be picked up from a different folder entirely.

You might be able to use this Tomcat Filter: http://tuckey.org/urlrewrite/, though you would still need an apache web server to host the static files, you could use Tomcat to access the main tomcat URLs and add redirects to your apache instance for all the static files. This saves having to configure mod_jk or something with Apache.

like image 26
cowls Avatar answered Oct 08 '22 21:10

cowls


Update your server.xml and modify the "host" for your application for two purposes:

1) To point to your git source directory for tomcat to start your app from. This would avoid having to make changes at two places or copying over changes.

Change Required: Update the "host" for your application and set the 'appBase' to your git source directory.

<Host name="localhost"  appBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp"
            unpackWARs="true" autoDeploy="true">

2) Add a separate location for serving your static content. This would help you to avoid restarting tomcat everytime you change your css/js

Change Required: Add contexts for serving your js and css

<Host name="localhost"  appBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp"
            unpackWARs="true" autoDeploy="true">
    <Context docBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp/js/" path="/js/*" />
    <Context docBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp/css/" path="/css/*" />
</Host>
like image 20
MickJ Avatar answered Oct 08 '22 21:10

MickJ