如何在JSP或Servlet中更改请求URL

3

index.jsp

 <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%="This is First App" %> 
    </body>
    </html>

web.xml文件:

 <display-name>JSPAPP</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

当我运行应用程序http://localhost:8080/JSPAPP/index.jsp时,它可以正常工作。但是我需要将此URL更改为http://localhost:8080/MyFirstApp/index.jsp,即我需要将JSPAPP更改为MyFirstApp,请告诉我如何更改。


你是如何部署这个应用的?还是直接在Eclipse中运行? - KDP
是的,我正在使用 Eclipse 运行程序。我该如何在 Eclipse 中进行更改? - Research Development
将 index.jsp 文件移动到 webapps/MyFirstApp 文件夹。 - sprabhakaran
https://dev59.com/RHE95IYBdhLWcg3wJKh_ - KDP
在Servlet中我该如何修改它? - Research Development
http://localhost:8080/MyFirstApp/index.jsp 当我将index.jsp移动到MyFirstApp文件夹时,它无法工作。 - Research Development
1个回答

1

1) 如果您使用Eclipse运行应用程序,则需要更改项目的上下文根。具体操作为: "属性 -> Web项目设置 -> 上下文根"。

2) 如果您要将应用程序部署到服务器(例如Tomcat),请将war文件重命名为所需的上下文根。


我尝试了按照第一个方法,但是无法运行。 - Research Development
我已将Contextroot更改为MyFirstApp,但仍然在http://localhost:8080/JSPAPP/index.jsp上运行,而在http://localhost:8080/MyFirstApp/index.jsp上运行。我想要他的... - Research Development
在项目属性中更改上下文根后,您无需更改servlet。您需要从Tomcat中删除您的Web应用程序(使用服务器上下文菜单中的添加和删除...),重新部署,然后重新添加您的应用程序并重新部署。请参阅此链接https://dev59.com/RHE95IYBdhLWcg3wJKh_。 - KDP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接