假设您有以下简单的应用程序:
<form action="helloServlet" method="post">
Give name:<input type="text" name="name" />
<input type="submit" value="Send"/>
</form>
以及处理该表单的 servlet:
package org.servlets.hello;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="helloServlet", urlPatterns={"/helloServlet"})
public class helloServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet helloServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello " + name + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
现在,当它从位于“网页”文件夹中的网页运行时,它可以完美运行,但是当我尝试在网页的子文件夹中执行相同操作时,出现 404 错误(请求的资源()不可用。) . 我是否应该更改某些内容以使子文件夹成为调用 servlet 的有效位置?