Response对象实现请求重定向

ResponseDemo6.java文件:

package com.neu;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * response实现请求重定向
 * @author wym
 *
 */
public class ResponseDemo6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        test(response);
    }

    private void test(HttpServletResponse response) throws IOException {
        response.sendRedirect("/Response/servlet/ResponseDemo7");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

ResponseDemo7.java文件:

package com.neu;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.getWriter().write("I am ResponseDemo7");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

运行:http://localhost:8080/Response/servlet/ResponseDemo6
结果:I am ResponseDemo7
地址栏信息变为:http://localhost:8080/Response/servlet/ResponseDemo7

你可能感兴趣的:(response)