//开始访问的页面
@WebServlet("/SixthServlet")
public class SixthServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置响应状态码
// response.setStatus(302);
//2.定向到哪里去: 其实就是设置响应消息头,Location
// response.setHeader(“Location”, “SenvthServlet”);
//使用重定向方法
resp.sendRedirect(“http://localhost:8080/SenvthServlet”);
}
}
//重定向的位置
@WebServlet("/SenvthServlet")
public class SenvthServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType(“txt/html;charset=utf-8”);
resp.getWriter().write(“welcome to ResponseDemo7”);
}
}
这样在地址输入后,
原因在于:
在 SenvthServlet 类中, 设置错了 txt/html 浏览器无法识别,正确为 : text/html
这样改正之后,就能正常访问了。



