我有一个编译成 tryJEdit.war 文件的 Maven 项目。 当我将其复制到 tomcat 文件夹并启动 Tomcat 时,我可以从以下位置访问该应用程序:
本地主机:8080/tryJEdit
在 index.jsp 中我有:
<form method="post" action="tugay">
所以在表单发布后,我发现自己在:
localhost:8080/tryJEdit/tugay -> Which is fine..
但是当我将表单更改为:
<form method="post" action="/tugay">
我发现自己在:
localhost:8080/tugay
这不好。
有没有办法通过如下方式访问 localhost:8080/tryJEdit/tugay:
<form method="post" action="$APPLICATION_ROOT/tugay">
在 jsp 文件中?
编辑并扩展问题
在我尝试的 servlet doGet 方法中:
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/re.pdf");
文件 re.pdf 实际上是在 tryJEdit/re.pdf 中搜索的,而不是在 localhost:8080/re.pdf 中搜索的,但是对于上述情况,如果我发布到/tugay 那么我最终会在localhost:8080/tugay 不是 tryJEdit/tugay ...
问候。
最佳答案
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getContextPath()
<form method="post" action="<%=request.getContextPath()%>/tugay">
关于java - 如何在jsp中相对于应用程序根目录发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471905/