Is it possible to have a servlet forward to a jsp outside of its context root? - deployment

We currently have an appserver setup where EVERYTHING is off of one big context root, and we copy class files and restart app servers to deploy. Not ideal.
I'm trying to set up an ant script to do the build and deploy using wdeploy, and everything works, except I need my servlet to forward to jsps outside of the context root of my war file deploy. So I figure if I can put a symlink in my war file, it can point to somewhere outside of my context root space.
This is the goal I'm trying to achieve, perhaps the symlink isn't the best idea.
I just need a way to forward outside of my context root from a servlet.

I'm not sure a symlink will work and I agree it's not a good idea. Try creating a virtual directory pointing to where your jsps are located.

You can always to HttpServletResponse.sendRedirect to send the user anywhere, but if you want to use RequestDispatcher.forward or jsp:forward, it only works within the app's context root.
I don't think it's that bad to have all your code under one context root, assuming it's related to one application. Creating multiple WARs for different parts of the same application seems to increase your maintenance cost with little gain.

It is possible, to forward to resources outside of the context of your webapp, if the other webapp is running in the same servlet-container.
For details see: Servlets: forwarding to a resource in a different webapp

Related

Changes in conf/server.xml does not seem to have any effect during runtime

Here's what I know:
When uploading files given by users, we should put them in a folder
outside the deployment folder. Let me call it D:\uploads.
We should (somehow) add that folder (D:\uploads) as a web app context.
Here's what I did:
I upload my files to the folder D:\uploads.
I tried adding the web app context as it's mentionned here by adding the following row to TOMCAT_DIR/conf/server.xml:
<Context docBase="D:\uploads" path="/uploads"/>
But that doesn't have any effect. When consulting http://localhost:8080/uploads/file.png or http://localhost:8080/uploads I get a HTTP Status 404 error.
So what I want to know:
What did I do wrong ? How can I add my upload folder to Tomcat?
Is there any better approach when it comes to uploading files ?
Because I'm wondering what should I change if I want to deploy my
application to another server where there's no D:\uploads.
Change the docBase attribute. Use D:/uploads (with slash) instead of D:\uploads (with backslash).
When dealing with files in Java, you can safely use / (slash, not backslash) on all platforms.
Regarding the differences you mentioned in the comments when starting the Tomcat from the IDE and from bin/startup.bat: It's very likely when you start the Tomcat from the IDE, it is not using the same context.xml your Tomcat is using. Just review the Tomcat settings in the IDE.
How to store uploaded files is a common topic at Stack Overflow. Just look around and you'll get surprised in how this topic is popular.
If you aren't happy enough in storing your files in D:/uploads or you'll have other servers accessing the files, you could consider storing them in some location in your network. Depending on your requirements, you can have one dedicated server to store your files or just share the folder which contains the files in your current server. The right decision will always depend on your requirements.

IBM Liberty issue

An architect is having issues bringing Liberty up. Currently, an individual is running a server on his local computer and they want to move it to a shared server. When he tries to deploy a simple “helloworld” it’s failing and he is receiving an error “Context Root Not Found”. He is not sure what to set in server.xml file to have wlp recognize the application. They have ODM 8.5 on the mainframe. He thinks it might help if he saw an example of an EAR or WAR file deployed. Any ideas or suggestions?
Either put your application in the dropins folder, it will be detected and started automatically, or put it in the apps folder and configure in server.xml like this:
<webApplication id="HelloApp" location="HelloApp.war" name="HelloApp"/>
by default context root is application file name without extension, but you can change it by adding contextRoot="mycontext" attribute.

Tomcat 6 Eclipse root deployment

I have a Java web application running on Tomcat6 built with Eclipse. It has always been run in a subdirectory:
/webapps/appDIR
As a result, the URL is:
www.application.com/appDIR
I now want it to just be deployed to the top level. I rename the directory to ROOT and extract the WAR. Set the permissions, and it doesn't work. It goes to an infinite redirect of the error page, and then the page dies.
I can rename the the directory to anything else and it works. For example I could call it test, in which case the URL becomes:
www.application.com/test
.....works fine. I just can't use "ROOT", which would allow the wwww.application.com URL to work.
I did some searching, and decided it was related to the context.xml file. It is currently sitting in the /WEB-INF directory (wrong?), and it only contains an end tag of "" (more wrong?). I moved that file to the /META-INF directory, and it didn't work. I figured that maybe it couldn't be empty and I added the contents of the following link to it:
http://www.wellho.net/resources/ex.php4?item=a654/6_context.xml
And still no go.
Any ideas? For a little more background, I'm now deploying it to Amazon's Beanstalk whereas before it was self hosted. Beanstalk defaults to deploying to ROOT, and I didn't see a reason to fight them on it, whereas Eclipse feels differently.
You mentioned: "Beanstalk defaults to deploying to ROOT, and I didn't see a reason to fight them on it".
If you care to fight -
A hack to be sure, but to get our app to deploy under a subdirectory, we did the following with a beanstalk config file (.ebextensions)
commands:
# This modified the default beanstalk deploy script so that our WAR file sits in appropriate subdirectory.
fixdeployscript:
command: sed -i 's/webapps\/ROOT/webapps\/MyAppName/g' /opt/elasticbeanstalk/hooks/appdeploy/enact/03deploy.sh

Is there a way to get the absolute path of the context root in tomcat?

I have a problem that, after a lot of reading and research, seems like tomcat is running another instance of itself and thus serving an old version of my updated app (or somehow has cached an older version of my webapp somewhere only serves that.)
I work on the app in eclipse on a windows machine and deploy it on a Linux server as a ROOT app (Renaming the war file to a ROOT.war).
What I'd like to know is if there's a way to locate the older version that tomcat is serving by getting tomcat to log an output of the context root of the servlet that's serving the older version of the app.
As it stands it the moment any files created by the updated app get created in the right directory but because the app instances are different it can't access the files shortly after they're created.
Any help/hints would be welcomed
To answer the question in the title, let your code basically do the following:
System.out.println(getServletContext().getRealPath("/"));
To solve the problem described in the question, shutdown Tomcat and delete everything in its /work directory, delete the expanded WAR in /webapps and remove the /Catalina subdirectory of /conf directory (if any) and then restart.

Unity Configuration using multiple config files

Is there a way to configure a container in multiple configuration files?
For instance, I want to register types for a container in a web.config file located at the root and also register types for the same container (and others containers) in the web.config file of sub-folders.
And also register other types for others containers in a company.config file in path C:\Company\Framework\Configs.
When I try to do this I get a ConfigurationError stating that the entry for the container has already been added.
EDIT: any suggestions here
http://unity.codeplex.com/Thread/View.aspx?ThreadId=23230
any more suggestions for sample code ?
I have a similar question posted here: WCF + Unity nested web.config problem
I have a web application with a subdirectory 'Services' which contains WCF services.
This services folder also has a web.config file containing my WCF configuration. That doesn't seem to be a problem for WCF.
The same web.config also has unity configuration for use inside my services. But i'm unable to load this configuration using 'GetSection'
If I move my unity configuration to the root web.config, everythings works fine.