为.jsp中的Servlet传递正确的路径 [英] Pass in proper path for Servlet in .jsp
问题描述
我有一个.jsp文件,必须将请求提交给ResultServlet
I have a .jsp file which has to submit the request to ResultServlet
但是我遇到了错误
HTTP状态404-/project1/jsp//ResultServlet
HTTP Status 404 - /project1/jsp//ResultServlet
问题是我的ResultServlet在/project1/src/ResultServlet中
The problem is that my ResultServlet is in /project1/src/ResultServlet
您能告诉我我需要对.jsp中的action字段进行哪些更改,以便它开始在文件夹中查找 src而不是文件夹jsp
Can you tell me what changes i need to make to the action field in .jsp so that it starts looking in folder src instead of folder jsp
推荐答案
我知道您想在JSP的<form action>
中使用相对于servlet的相对URL.首先,您需要确定JSP和Servlet的完整 URL,然后从JSP中提取Servlet的相对URL.
I understand that you'd like to use a relative URL to the servlet in <form action>
of the JSP. First you need to determine the full URL of both the JSP and the Servlet and then extract the relative URL to the Servlet from the JSP.
假设您的JSP位于 http://example.com/context/jsp/page上.jsp ,并且您的Servlet在 http://example.com/context/src/servlet上.因此,要从JSP到达Servlet,您必须上一层../
,以便您进入 http://example. com/context ,最后从那里转到src/servlet
.
Let's assume that your JSP is on http://example.com/context/jsp/page.jsp and that your Servlet is on http://example.com/context/src/servlet. So to reach the Servlet from the JSP you have to go one level up ../
so that you lands in http://example.com/context and finally go from there to src/servlet
.
<form action="../src/servlet">
或者,如果不想依赖JSP的位置/URL,也可以使用上下文路径:
Alternatively, you can also use the context path if you don't want to be dependent on the location/URL of the JSP:
<form action="${pageContext.request.contextPath}/src/servlet">
这篇关于为.jsp中的Servlet传递正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!