欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

request重定向_重定向和转发的特点

程序员文章站 2024-03-24 08:07:46
...
request重定向_重定向和转发的特点
package cn.itcast.servlet.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 重定向 * Servlet implementation class ResponseDemo1 */@WebServlet("/ResponseDemo1")public class ResponseDemo1 extends HttpServlet {private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public ResponseDemo1() {        super();        // TODO Auto-generated constructor stub    }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("demo1...........");//访问/ResponseDemo1,会自动跳转到/ResponseDemo2资源1.设置状态码为302//response.setStatus(302);2.设置响应头location//response.setHeader("location", "/Project3/ResponseDemo2");//存值request.setAttribute("msg", "response");//简单的重定向方法response.sendRedirect("/Project3/ResponseDemo2");//response.sendRedirect("https://www.baidu.com/baidu?tn=monline_7_dg&ie=utf-8&wd=%E7%99%BE%E5%BA%A6+%E6%9C%80%E4%BD%B3%E5%A5%B3%E5%A9%BF%E5%85%A8%E6%96%87%E5%85%8D%E8%B4%B9+%E9%98%85%E8%AF%BB%E5%B0%8F%E8%AF%B4%E6%9C%80%E6%96%B0");}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
package cn.itcast.servlet.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ResponseDemo1 */@WebServlet("/ResponseDemo2")public class ResponseDemo2 extends HttpServlet {private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public ResponseDemo2() {        super();        // TODO Auto-generated constructor stub    }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("demo2...........");Object msg = request.getAttribute("msg");System.out.println(msg);}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);//this.doGet(request, response);}}
相关标签: request重定向