• 重定向与错误发送


    重定向状态码

    301、302

    重定向demo

    package com.test;
    
    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("/first")
    public class First extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // response.sendRedirect("https://www.baidu.com");
            response.sendRedirect("sencond"); // 重定向可以指定相对url,也可以指定绝对url
        }
    }
    First.java  
    package com.test;
    
    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("/sencond")
    public class Sencond extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.sendRedirect("https://www.sina.com.cn");
        }
    }
    Sencond.java

    发送错误

    package com.test;
    
    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("/first")
    public class First extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //response.sendError(HttpServletResponse.SC_NOT_FOUND);
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "笔记文件");
        }
    }
    View Code

    response.sendError(HttpServletResponse.SC_NOT_FOUND)响应内容如下:

    response.sendError(HttpServletResponse.SC_NOT_FOUND, "笔记文件")响应内容如下:

  • 相关阅读:
    BZOJ3105:[CQOI2013]新Nim游戏(线性基,贪心)
    BZOJ5102:[POI2018]Prawnicy(贪心,堆)
    BZOJ3533:[SDOI2014]向量集(线段树,三分,凸包)
    BZOJ3569:DZY Loves Chinese II(线性基)
    BZOJ3534:[SDOI2014]重建(矩阵树定理)
    【BZOJ 1202】 [HNOI2005]狡猾的商人
    【BZOJ 1003】 [ZJOI2006]物流运输trans
    【BZOJ 2321】 [BeiJing2011集训]星器
    【BZOJ 1010】 [HNOI2008]玩具装箱toy
    【BZOJ 2730】 [HNOI2012]矿场搭建
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/10037579.html
Copyright © 2020-2023  润新知