我有简单的 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>
问题是当它重定向到欢迎文件列表中的 servlet 时,url 没有改变
我的 url:http://localhost:8080/testsss_war_exploded/
但应该是:http://localhost:8080/testsss_war_exploded/hello-servlet