案例:
1.完成重定向
重定向:资源跳转的方式
1. 设置状态码为302
response.setStatus(302);
2.设置响应头location
response.setHeader("location","/day15/responseDemo2");
简单的重定向方法
response.sendRedirect("/day15/responseDemo2");
重定向的特点
1.地址栏发生变化
2.重定向可以访问其他站点(服务器)的资源
3.重定向是两次请求
转发的特点
1.转发地址栏路径不变
2.转发只能访问当前服务器
3.转发是一次请求
forward 和 redirect 区别
路径写法:
2.服务器输出字符数据到浏览器
3.服务器输出字节数据到浏览器
4.验证码
重定向实现代码 package com.example.servlet; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; /* 重定向 */ @WebServlet("/demoResponse01") public class demoResponse01 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo01..."); //访问 /demoResponse01,会自动跳转到 /demoResponse02资源 //1.设置状态码为302 response.setStatus(302); //2.设置响应头为location response.setHeader("location","/demoResponse02"); } } ================================================== package com.example.servlet; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet("/demoResponse02") public class demoResponse02 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo02..."); } }
标签:重定向,javax,IOException,720,721,import,servlet,response From: https://www.cnblogs.com/agzq/p/17107887.html