3 回答
TA贡献1836条经验 获得超3个赞
当您使用HTML中的URL时,不带前导/,它们是相对于当前URL(即显示的当前页面)的。以/开头是相对于网站根目录的:
<form action="/context-path/sampleServlet">
要么
<form action="sampleServlet">
会做你想要的。
我建议您在操作路径中动态添加上下文。示例(在JSP中):
<form action="${pageContext.request.contextPath}/sampleServlet">
有了它,您将无需更改路径,例如,如果您移动文件或复制代码,或重命名上下文!
TA贡献1846条经验 获得超7个赞
可能会帮助你
servlet配置
<servlet>
<servlet-name>sampleServlet</servlet-name>
<servlet-class>test.sampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sampleServlet</servlet-name>
<url-pattern>/sampleServlet/</url-pattern>
</servlet-mapping>
Servlet代码:
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class sampleServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet Get</h1>");
out.println("</body>");
out.println("</html>");
}
}
JSP代码:
<html>
<body>
<form action="/sampleServlet/" method="GET">
<input type="submit" value="Submit form "/>
</form>
</body>
</html>
您可以单击提交按钮,然后您可以看到servlet输出
添加回答
举报