当出现在欢迎文件列表中的 运行 servlet 时,为什么 url 没有改变
Why doesn`t url change when running servlet that presents in welcome-file-list
我有一个简单的 servlet,可以打印一些内容来响应
@WebServlet(name = "helloServlet", value = "/hello-servlet" , s )
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
web.xml 的样子:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>hello-servlet</welcome-file>
</welcome-file-list>
</web-app>
问题是 url 在重定向到欢迎文件列表中的 servlet 时没有改变
我的 url : http://localhost:8080/testsss_war_exploded/
但应该是:http://localhost:8080/testsss_war_exploded/hello-servlet
这就是欢迎资源的工作方式:
The container may send the request to the welcome resource with a forward, a redirect, or a container specific mechanism that is indistinguishable from a direct request.
Tomcat 在内部重定向请求。如果你想发送 HTTP 重定向,你需要自己做。您可以查看原始 URI 以查看请求是否通过欢迎文件机制转发:
@WebServlet(name = "helloServlet", urlPatterns = {"/hello-servlet"})
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getRequestURI().endsWith("/")) {
resp.sendRedirect("hello-servlet");
return;
}
}
另一种解决方案是放弃欢迎文件机制并将您的 servlet 显式绑定到应用程序根目录:
@WebServlet(name = "helloServlet", urlPatterns = {"", "/hello-servlet"})
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getServletPath().isEmpty()) {
resp.sendRedirect("hello-servlet");
return;
}
}
我有一个简单的 servlet,可以打印一些内容来响应
@WebServlet(name = "helloServlet", value = "/hello-servlet" , s )
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
web.xml 的样子:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>hello-servlet</welcome-file>
</welcome-file-list>
</web-app>
问题是 url 在重定向到欢迎文件列表中的 servlet 时没有改变
我的 url : http://localhost:8080/testsss_war_exploded/
但应该是:http://localhost:8080/testsss_war_exploded/hello-servlet
这就是欢迎资源的工作方式:
The container may send the request to the welcome resource with a forward, a redirect, or a container specific mechanism that is indistinguishable from a direct request.
Tomcat 在内部重定向请求。如果你想发送 HTTP 重定向,你需要自己做。您可以查看原始 URI 以查看请求是否通过欢迎文件机制转发:
@WebServlet(name = "helloServlet", urlPatterns = {"/hello-servlet"})
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getRequestURI().endsWith("/")) {
resp.sendRedirect("hello-servlet");
return;
}
}
另一种解决方案是放弃欢迎文件机制并将您的 servlet 显式绑定到应用程序根目录:
@WebServlet(name = "helloServlet", urlPatterns = {"", "/hello-servlet"})
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getServletPath().isEmpty()) {
resp.sendRedirect("hello-servlet");
return;
}
}