1. 程式人生 > >servlet 相應頭重定向

servlet 相應頭重定向

pac response res 大小 請求 java status cat tex

package demoservllet;

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 Servlet01 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setStatus(302); //設置請求狀態 302重定向
  response.setHeader("Location", "/WEB14/servlet02");//設置請求頭 Location固定的L大小寫都可以 url=("/WEB14/servlet02")
  }

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

package demoservllet;

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 Servlet02 extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().write("hello haohao...");//頁眉打印的東西
  }

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

servlet 相應頭重定向