简体   繁体   English

设置Tomcat默认上下文路径

[英]Set Tomcat Default Context Path

In my context.xml file I set the following to: <Context antiJARLocking="true" path="/" /> 在我的context.xml文件中,我将以下内容设置为: <Context antiJARLocking="true" path="/" />

When I run my project from NetBeans then it works correctly and goes to http://localhost:8080/login . 当我从NetBeans运行我的项目然后它正常工作并转到http://localhost:8080/login Then when I clean & build and go into Tomcat Manager and deploy the war file, for some reason it goes to http://localhost:8080/appName/login . 然后,当我清理并构建并进入Tomcat Manager并部署war文件时,由于某种原因它转到http://localhost:8080/appName/login I'm not sure why it's adding the context path or where it even gets it from but when ever I deploy it manually it does that. 我不确定为什么它会添加上下文路径或它甚至从哪里获取它,但是当我手动部署它时它会这样做。 When ever I run the project directly from Netbeans then it doesn't. 当我直接从Netbeans运行项目时,它没有。 After I run it directly from NetBeans, if I go to Tomcat Manager then it shows the app deployed under context path / which is correct. 在我直接从NetBeans运行它之后,如果我转到Tomcat Manager,它会显示在上下文路径下部署的应用程序/这是正确的。 When I deploy the .war manually then it deploys under context path /appName 当我手动部署.war时,它会在上下文路径/appName下部署

It sounds like you are building your war file as "appName.war". 听起来你正在将war文件构建为“appName.war”。 That is the reason tomcat deploys it under "/appName". 这就是tomcat在“/ appName”下部署它的原因。

If you want your application accessible at /, you can rename your war file as ROOT.war and drop it in /webapps and it should be accessible at http : //localhost:8080/ 如果您希望您的应用程序可以在/中访问,您可以将您的war文件重命名为ROOT.war并将其放在/ webapps中,它应该可以在http:// localhost:8080 /访问

Some apps are written such that changing the context path would require code changes. 编写一些应用程序,以便更改上下文路径需要更改代码。 If you have that situation here is another way to make your server default to a specific context: 如果您遇到这种情况,则另一种方法是将服务器默认为特定上下文:

Step 1) Put this in [tomcat]/conf/web.xml 步骤1)将其放在[tomcat] /conf/web.xml中

<welcome-file-list>      
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

Step 2) Add this file to your ROOT webapp folder, and leave everything else the same: index.html (for root app). 步骤2)将此文件添加到ROOT webapp文件夹,并保留其他所有内容:index.html(对于root应用程序)。 Using this javascript approach rather than a normal redirect, allows the 'redirection' to work AND KEEP the same url parameters. 使用这种javascript方法而不是正常的重定向,允许“重定向”工作并保持相同的url参数。

<!doctype html>
<html>
<head>

<script language="JavaScript">
    document.location.href = "/mycontext" + document.location.search;
</script>

</head>

</html>

Use this Maven plugin: 使用这个Maven插件:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <path>/</path>
        <warFile>${project.build.directory}/fileName.war</warFile>
    </configuration>
</plugin>

Delete the context.xml file in your META-INF folder. 删除META-INF文件夹中的context.xml文件。

Run your project with this command: mvn tomcat7:run 使用以下命令运行项目: mvn tomcat7:run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM