Response_案例1_重定向_代码实现
重定向代码实现案例
案例:
1.完成重定向
重定向:资源跳转的方式
代码实现:
//访问responseDemo01,会自动跳转到/responseDemo02资源 //1.设置状态码为302; response.setStatus(302); //2.设置响应头location response.setHeader("location","/day15/responseDemo02"); //简单的重定向方法 response.sendRedirect("/day15/responseDemo02");
package qh.xueqiang.servlet; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; /** * 完成重定向 */ @WebServlet("/responseDemo01") public class ResponseDemo01 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("geigei~你来了啊"); //访问responseDemo01,会自动跳转到/responseDemo02资源 /* //1.设置状态码为302; response.setStatus(302); //2.设置响应头location response.setHeader("location","/day15/responseDemo02");*/ } }
package qh.xueqiang.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/responseDemo02") public class ResponseDemo02 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("你干嘛~"); } }
简单重定向
package qh.xueqiang.servlet; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; /** * 完成重定向 */ @WebServlet("/responseDemo01") public class ResponseDemo01 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("有请我们xxxx时代的练习生"); //访问responseDemo01,会自动跳转到/responseDemo02资源 /* //1.设置状态码为302; response.setStatus(302); //2.设置响应头location response.setHeader("location","/day15/responseDemo02");*/ //简单的重定向方法 response.sendRedirect("/day15/responseDemo02"); } }
2.服务器输出字符数据到浏览器
3.服务器输出字节数据到浏览器
4.验证码
Response_案例1_重定向_特点
重定向特点
1.地址栏发生变化
2.重定向可以访问其他站点(服务器)的资源
3.重定向是两次请求
转发的特点
1.转发地址路径不变
2.转发只能访问当前服务器下的资源
3.转发是一次请求,可以使用Request对象来共享数据
forward和redirect 区别
标签:javax,重定向,Response,案例,IOException,import,servlet,response From: https://www.cnblogs.com/x3449/p/17082441.html