Servlet请求转发和重定向

标题一、请求转发

• servlet1处理不了的请求转发给指定的servlet2
• 请求转发,参数是转发的资源名,即转发地址
• 在请求转发的过程中多个资源公用一个请求和响应

//servlet1转发请求到servlet2
public class ForwardServlet1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("请求到达资源1");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
//        HttpSession httpSession = request.getSession();
//        String username = (String) httpSession.getAttribute("username");
//        writer.println("<h1>用户名:"+username+"</h1>");
        writer.println("<h1>资源1的内容</h1><br><br>");
        //请求转发,参数是转发的资源名,即转发地址
        //在请求转发的过程中多个资源公用一个请求和响应
        request.getRequestDispatcher("forward2").forward(request,response);
        System.out.println("请求离开资源1");
    }
}
public class ForwardServlet2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("请求到达资源2");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println("<h1>资源2的内容</h1>");
        System.out.println("请求离开资源2");
    }
}
 <servlet>
        <servlet-name>ForwardServlet1</servlet-name>
        <servlet-class>com.tdu.servlet.ForwardServlet1</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ForwardServlet2</servlet-name>
        <servlet-class>com.tdu.servlet.ForwardServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ForwardServlet1</servlet-name>
        <url-pattern>/forward1</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ForwardServlet2</servlet-name>
        <url-pattern>/forward2</url-pattern>
    </servlet-mapping>

标题二、重定向

• 把给servlet1的请求发给servlet2

public class RedirectServlet1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("请求到达资源1");
        response.setContentType("text/html; charset=utf-8");
        //重定向之前,余下代码会继续执行,
        response.sendRedirect("redirect2");
        System.out.println("请求离开资源1");
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
public class RedirectServlet2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("请求到达资源2");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println("<h1>请求到达资源2</h1>");
        System.out.println("请求离开资源2");
    }
}
 <servlet>
        <servlet-name>RedirectServlet1</servlet-name>
        <servlet-class>com.tdu.servlet.RedirectServlet1</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>RedirectServlet2</servlet-name>
        <servlet-class>com.tdu.servlet.RedirectServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RedirectServlet1</servlet-name>
        <url-pattern>/redirect1</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>RedirectServlet2</servlet-name>
        <url-pattern>/redirect2</url-pattern>
    </servlet-mapping>
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页