重定向机制

一、目录展示

  重定向机制_第1张图片

 

 二、aindex.jsp

  重定向机制_第2张图片

三、AServlet

  

package com.zn;

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("/AServlet")
public class AServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //重定向
        /*resp.sendRedirect("/aindex.jsp");*/
        //302状态码
        resp.setStatus(302);
        resp.setHeader("Location","aindex.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

四、效果展示

  访问路径:

  

  跳转到aindex.jsp

  重定向机制_第3张图片

你可能感兴趣的:(重定向机制)