URL rewriting in tomcat 7 when hitting domain name - java

I need help in redirecting URL for an application deployed in tomcat7.
When we use "www.portal.com/portal", am able to view the site.
I want to view the page by just entering the URL as "www.portal.com". So, i need help in what need to be modified in tomcat7 configuration to get this done.

You can name your application ROOT or change the path to your application in tomcat's server.xml file.
This is an example of a mapping in the server.xml file:
<Context docBase="portal" path="/portal" reloadable="true"
source="org.eclipse.jst.jee.server:portal" />
To load the portal application without using /portal you need to change it to:
<Context docBase="portal" path="" reloadable="true"
source="org.eclipse.jst.jee.server:portal" />

Related

Problem with Tomcat9 Context's docBase attribute

I am new to Tomcat deployment.
I found in Tomcat's document and it says this about <Context> tag's docBase attribute:
The value of this field must not be set unless the Context element is defined in server.xml or the docBase is not located under the Host's appBase.
Here is my problem:
I create a <Host> tag inside server.xml and set appBase="C:/xxx/"
create a xml file named ROOT.xml inside C:/xxx/ and add <Context docBase="../yyy" />
put index.html inside C:/yyy/
Why is it doesn't work when I try to browser localhost:8080/ ?
Doesn't it supposed to show the index.html inside C:/yyy ?
Summary
When you add appBase="C:/xxx" to server.xml, that means all your web appplications will be found in that directory. At that point you cannot then have a specific web app which defines its own redirection to another directory (a custom docBase) from within its own web application location. At that point, its docBase location has already been set to be the appBase directory.
A Different Approach
The ROOT.xml file that you created can be used to point to a location which is different from the appBase - but you cannot place that file inside the appBase directory (as noted above).
Instead you have to place it in $CATALINA_BASE/conf/[enginename]/[hostname]/ as described in this documentation page.
So, for me, that would be:
C:\tomcat_9_test\conf\Catalina\localhost\ROOT.xml
And its contents would simply be:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:/yyy" />
Because this is outside of the docBase, this specific application can have a different location from all the other web applications in docBase.
Now, your C:\yyyy\index.html file will be served at http://localhost:8080/, as expected.
A Note on context.xml
What if you do not use this ROOT.xml file at all, but instead you create a ROOT folder in C:\xxx and then create a META-INF folder there, containing a context.xml file?
You could define <Context docBase="../yyy" /> in there. But it would be ignored (again, because the docBase is already set).

Deploying War. Tomcat not taking context.xml from META-INF in consideration

I do have a Spring Boot Application which uses custom context.xml for the tomcat.
The context.xml contains property, defining the spring active profile
<Context>
<Environment name="spring.profiles.active" value="profileName" type="java.lang.String" override="false" />
</Context>
File location is the /src/main/webapps/META-INF
I was expecting that after the file is deployed to the tomcat, context xml will be automatically picked by tomcat and thrown to the conf/catalina/localhost/
As it turned out, the war was deployed, but the conf/catalina/localhost stayed empty.
After reading the docs i've found out that the server.xml has to be updated with the copyXML parameter as Host container.
The documentation says:
copyXML
Set to true if you want a context XML descriptor embedded inside the application (located at /META-INF/context.xml) to be copied to xmlBase when the application is deployed. On subsequent starts, the copied context XML descriptor will be used in preference to any context XML descriptor embedded inside the application even if the descriptor embedded inside the application is more recent. The flag's value defaults to false. Note if deployXML is false, this attribute will have no effect.
My Host looks like
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" copyXML="true">
</Host>
After restarting the server and redeploying the app, the /conf/Catalina/localhost still stayed empty.
Do you have any suggestions, what actions have to be taken in order to use custom context.xml?
I've figured it out.
My build script did not copy the file correctly, the file name was not "context.xml" but "appName.xml" and tomcat didn't pick it up.

Context path configuration in Apache tomcat 8.0.30

I currently have an apache tomcat application running on my machine. I need to make this application accessible over an external URL.
I can access the application locally at http://localhost/c. I want to be able to access the application externally at https://example.com/b/001/c. TO achieve this, I made the below change to my conf/server.xml file
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="" path="/b/001" reloadable="true" />
After making this change, my local set up works fine when I test with the below URL
http://localhost/c/1.jsp (works fine)
When I try to load the external URL (https://example.com/b/001/c/1.jsp) however, I get the below error
An error occurred at line: [15] in the generated java file: [......./1_jsp.java]
Only a type can be imported. a.c.d.e.database.DB resolves to a package
What change should I make to make this publicly available on the external server?

Jboss image upload and http access to show image

I am uploading images to jboss server by getting the absolute path using the following code
getServletContext().getRealPath("");
The uploaded image is moved to the absolute path and I can access the image using http://test.com:8080/image.jpg
My problem is the image is being uploaded to the tmp directory of jboss server, so i am losing the uploaded images in the next deployment.
I tried uploading the image to various paths to make it work
\jboss-5.0.1.GA\server\default\deploy
and here \jboss-5.0.1.GA\server\default\work\jboss.web\localhost as well
But fails, I cannot access the image using http://test.com:8080/image.jpg
Kindly help me out in this...
You can add a new context to specify a path to access an external folder.
Steps for Jboss 4 and older versions:
Open your file /YOURINSTANCE_JBOSS/deploy/jboss-web.deployer/server.xml.
Define a new Context in the tag <Host name=”localhost” ...>
Example:
<Host name=”localhost” ...>
<Context path=”/myfolder” docBase=”/home/username/my_images” reloadable=”true”></Context>
Where /myfolder will be the path that you are going to use to access your files, and /home/username/my_images the folder where you are going to upload your pictures.
Restart JBoss
Now you will be able to access your files with the next path:
http://yourserver:yourport/myfolder/filename
Steps for Jboss 5:
Create a new file named context.xml into your WEB-INF folder with the next content:
<?xml version="1.0" encoding="UTF-8"?>
<Context allowLinking="true" cookies="true" crossContext="true" override="true">
<Resources allowLinking="true" className="YOUR_PACKAGE.MyResources" homeDir="/home/username/my_images" />
</Context>
Where className is the java class that will access the resources and homeDir your external directory.
According to this link create a new class to access your resources defined in the file context.xml
Example:
public class MyResources extends FileDirContext {
}
Now you will be able to access your files with the next function:
request.getServletContext().getResourceAsStream(uri);
Steps for Jboss 5 and older versions:
Create a new file named context.xml into your WEB-INF folder with the next content:
<?xml version="1.0" encoding="UTF-8"?>
<Context allowLinking="true" cookies="true" crossContext="true" override="true">
<Resources allowLinking="true" homeDir="/home/username/my_images" />
</Context>
Where homeDir is your external directory.
Create a symbolic link: YourDeployedProject.war/myfolder linked to /home/username/my_images
Windows:
mklink /D C:\YOUR_JBOSS_SERVER\server\default\deploy\YourDeployedProject.war\myfolder C:\users\YOURUSER\my_images
Linux:
YourDeployedProject.war# ln -s /home/username/my_images myfolder
Now you will be able to access your files with the next path:
http://localhost:8080/DeployedProject/myfolder/filename
Steps for Jboss 7:
JBoss 7 doesn't allow any of the methods for the previous JBoss versions, so the easiest solution is to implement a Servlet to access your files like in the next link.

Tomcat does not recognize my context

I have a web application and use tomcat-5.5.27, eclipse 3.4 on open SUSE. I use it from eclipse (deploy, run, debug, etc...)
I have to set some JNDI resources (datasources) that Tomcat will expose them.
These resources are defined now in \META-INF\context.xml. This works fine.
But, when I move this file (context.xml) in tomcat-5.5.27/conf/context.xml or
tomcat-5.5.27/conf/Catalina/localhost/myapp.xml, it does not load the resources defined in the file.
What can be the problem?
My context.xml:
<Context path="/myapp" docBase="/myapp" debug="5" reloadable="true" crossContext="true">
<Resource name="jndi_name" auth="Container"
type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="COM.ibm.db2.jdbc.app.DB2Driver"
url="jdbc:path_to_db"
username="username"
password="password"
validationQuery="SELECT 1 FROM SYSIBM.SYSDUMMY1"
defaultTransactionIsolation="READ_COMMITTED"
removeAbandoned="true" logAbandoned="true" >
</Context>
If you're using the web tools project feature in Eclipse to manager your Tomcat instance you need to know that it does change Tomcat base directories.
Try using a separate Tomcat install and see if that helps.
This could be a classpath issue when tomcat comes to load the JDBC driver, (ie. before it loads your webapp).
Do you have the JAR file for the DB2 driver in tomcat-5.5.27/common/lib ? It must be there for
the class loader to find it, and not in your webapp's WEB-INF/lib directory.
where is your webapp stored ? It's own folder or under webapps ? check if your docBase is valid, if your app is under webapps I don't think you need the "/"
Any relevant info from the Tomcat logs ?
Open the tomcat server in the server view, modify anything like the timeout, save it and your context will be recognized.
What do the tomcat logs show when you start it up?
That tag is not closed, check if you copied it to question correctly.

Categories

Resources