XML配置
<servlet>
<servlet-name>ServletA</servlet-name>
<servlet-class>indi.jaywee.ServletA</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletA</servlet-name>
<url-pattern>/a</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletB</servlet-name>
<servlet-class>indi.jaywee.ServletB</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletB</servlet-name>
<url-pattern>/b</url-pattern>
</servlet-mapping>
ServletA
没有在映射路径之前添加工程路径
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/b"); //
}
在地址栏输入 http://localhost:8080/02_response/a, 点击跳转
跳转后,URL变成 http://localhost:8080/b,而实际上要访问的URL应是 http://localhost:8080/02_response/b
所以需要在映射路径之前加工程路径,即 "/02_response / b" (02_response是我的工程名)
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// resp.sendRedirect("/b"); // 错误写法
resp.sendRedirect("/02_response/b");
}
跳转后,成功跳转到ServletB。空白页面是因为ServletB没有向页面输出内容
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("B.jsp");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/02_response/static/A.jsp");
}
原文:https://www.cnblogs.com/secretmrj/p/14851825.html