How to change context root of a dynamic web project in Eclipse?

322,473

Solution 1

I'm sure you've moved on by now, but I thought I'd answer anyway.

Some of these answers give workarounds. What actually must happen is that you clean and re-publish your project to "activate" the new URI. This is done by right-clicking your server (in the Servers view) and choosing Clean. Then you start (or restart it). Most of the other answers here suggest you do things that in effect accomplish this.

The file that's changing is workspace/.metadata/.plugins/org.eclipse.wst.server.core/publish/publish.dat unless, that is, you've got more than one server in your workspace in which case it will be publishN.dat on that same path.

Hope this helps somebody.


Not sure if this is proper etiquette or not — I am editing this answer to give exact steps for Eclipse Indigo.

  1. In your project's Properties, choose Web Project Settings.

  2. Change Context root to app.

    screen shot of Eclipse project properties Web Project Settings

  3. Choose Window > Show View > Servers.

  4. Stop the server by either clicking the red square box ("Stop the server" tooltip) or context-click on the server listing to choose "Stop".

  5. On the server you want to use, context-click to choose "Clean…".

    enter image description here

  6. Click OK in this confirmation dialog box.

    Screenshot of dialog asking to update server configuration to match the changed context root

Now you can run your app with the new "app" URL such as:

http://localhost:8080/app/

Doing this outside of Eclipse, on your production server, is even easier --> Rename the war file. Export your Vaadin app as a WAR file (File > Export > Web > WAR file). Move the WAR file to your web server's servlet container such as Tomcat. Rename your WAR file, in this case to app.war. When you start the servlet container, most such as Tomcat will auto-deploy the app, which includes expanding the war file to a folder. In this case, we should see a folder named app. You should be good to go. Test your URL. For a domain such as *example.com" this would be:

http://www.example.com/app/

Vaadin toolkit programmers may need to rebuild their widget set if using visual add ons.

Solution 2

After changing the context root in project properties you have to remove your web application from Tomcat (using Add and Remove... on the context menu of the server), redeploy, then re-add your application and redeploy. It worked for me.

If you are struck you have another choice: select the Tomcat server in the Servers view. Double clicking on that server (or selecting Open in the context menu) brings a multipage editor where there is a Modules page. Here you can change the root context of your module (called Path on this page).

Solution 3

If you are running Tomcat from Eclipse, it doesn't use the configuration from your actual Tomcat installation. It uses the Tomcat configuration that it created and stored under "Servers" project. If you view your Eclipse workspace, you should see a project called "Servers". Expand that "Servers" project and you will come across server.xml. Open this file and scroll all the way to the bottom, and you should see something like this:-

<Context docBase="abc" path="/abc" reloadable="true" source="org.eclipse.jst.jee.server:abc"/>

Here, you can just change your project context path to something else.

Hope this helps.

Solution 4

I tried out solution suggested by Russ Bateman Here in the post

http://localhost:8080/Myapp to http://localhost:8080/somepath/Myapp

But Didnt worked for me as I needed to have a *.war file that can hold the config and not the individual instance of server on my localmachine.

Reference

In order to do that I need jboss-web.xml placed in WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
 <!--
Copyright (c) 2008 Object Computing, Inc.
All rights reserved.
-->
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">

  <jboss-web>
  <context-root>somepath/Myapp</context-root>
  </jboss-web>

Solution 5

Setting the path to nothing in the Eclipse web modules edit dialog enabled me to access the project without any path component in the URL (i.e. ROOT)

You can reach the web modules edit dialog by pressing F3 if you select Tomcat in the "Servers" view or by double clicking on it.

Some screenshots:

Share:
322,473

Related videos on Youtube

Amit
Author by

Amit

Updated on June 17, 2021

Comments

  • Amit
    Amit about 3 years

    I developed a dynamic web project in Eclipse. I can access the app through my browser using the following URL:

    http://localhost:8080/MyDynamicWebApp
    

    I want to change the access URL to:

    http://localhost:8080/app
    

    To do so, I changed the context root from the project "Properties | Web Project Settings | Context Root". However, the web app still has the same access URL. I have re-deployed the application on Tomcat and re-started the Tomcat, but the access URL is the same as earlier.

    I found that there was no server.xml file attached with the WAR file. Without the server.xml file attached, how is the Tomcat determining that the context root of my web app is /MyDynamicWebApp and allowing me to access the application through this context root URL?

  • Chris
    Chris over 13 years
    I'm not sure your want to change all 3 values. Csaba_H's solution worked for me and that only changes the "path" attribute...
  • Chris
    Chris over 13 years
    Thanks! This did the trick for me. however, it even works without changing the context root in the project properties. Am I missing something here?
  • idarwin
    idarwin almost 12 years
    This one didn't work for me - the files in that directory get overwritten when you restart the server. Csaba_H's version two, Modules tab under Tomcat Server, seems like the "right" approach.
  • AndreaNobili
    AndreaNobili over 11 years
    Great answer !!! You save my life ost of the other answers here suggest you do things that in effect accomplish this
  • Steven Francolla
    Steven Francolla about 11 years
    This worked for me. Environment = Eclipse + m2e + m2e-wtp + deploying to JBoss from Servers view. Needed to deploy main web project as ROOT and no Eclipse setting did the trick. I copied Tosha's file (above) and places "ROOT" sans quotes as the context-root value. Done and done. Thanks, @Tosha!
  • Vedran
    Vedran about 11 years
    This works, but I wonder why it ignores the setting in project properties?
  • Curt
    Curt over 10 years
    Here's the genius of this reply: I Googled my way to this page, and didn't even read the tags (I am building an ASP.NET website using Visual Studio). But the solution worked anyway!
  • alexander
    alexander over 9 years
    Works with JBoss WildFly 8.1.0 Final
  • smonff
    smonff almost 9 years
    Sorry but can you give more details on this?
  • smonff
    smonff almost 9 years
    oh, thank you! On Eclipse Luna on GNU/Linux, a / is required though.
  • Ray Hulha
    Ray Hulha almost 9 years
    I used Eclipse Mars (4.5) on Win7.
  • Sai
    Sai over 8 years
    I migrated from MyEclipse 8.6 to Eclipse and could not get to deploy with '/' root context. Adding jboss-web.xml worked successfully. server: Jboss AS 5.1 Eclipse Luna.
  • IntoTheDeep
    IntoTheDeep about 8 years
    I have no such and option "Web Project Settings"
  • Griknok
    Griknok over 7 years
    This also used to work for me in previous Eclipse builds but in the current build (Luna) only the Clean option now works
  • JesseBoyd
    JesseBoyd over 6 years
    all that looks good on my project and it runs within eclipse just fine. it's when i convert it to a war file using maven that it looses it's root context and instead of localhost/events/ it runs on localhost/ which breaks all my links
  • aliopi
    aliopi over 6 years
    just saying that workspace/.metadata/.plugins/org.eclipse.wst.server.core/pub‌​lish/publish.dat is a binary file, I don't understand why you mention it up there, it gives the Impression it's the solution to the problem
  • Alexsoyes
    Alexsoyes almost 6 years
    Yes, we must. Nice sharing.
  • Giorgi Tsiklauri
    Giorgi Tsiklauri over 5 years
    But what if we don't use Eclipse.. or any IDE.. I want to programmatically configure the root path of servlet context. How shall I do that? in web.xml probably?..
  • Yogesh Sanchihar
    Yogesh Sanchihar over 4 years
    namasety, dhanywaada :) when I initially changed my context, it did not work even after server restart and clean. it worked only post, I removed project and readded.
  • Uday Kiran
    Uday Kiran almost 4 years
    @GiorgiTsiklauri - By any chance do you got the answer how to configure root path in web.xml ?