一、服务器内部转发以及客户端重定向
? ?1.1?服务器内部转发
? ? ? request.getRequestDispatcher("...").forward(request,response); ? ? ? - 一次请求响应的过程,对于客户端而言,内部经过了多少次转发,客户端是不知道的 ? ? ? - 地址栏没有变化
测试代码:
public class Test01 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("test01...");
//服务器会内部自动转发到test2
request.getRequestDispatcher("test2").forward(request,response);
}
}
public class Test02 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("test02");
}
}
?web.xml配置
<servlet>
<servlet-name>Test01</servlet-name>
<servlet-class>com.cjc.servlets.Test01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test01</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Test02</servlet-name>
<servlet-class>com.cjc.servlets.Test02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test02</servlet-name>
<url-pattern>/test2</url-pattern>
</servlet-mapping>
1.2客户端重定向
? ? ? response.sendRedirect("...."); ? ? ? - 两次请求响应的过程。客户端肯定知道请求URL有变化 ? ? ? - 地址栏有变化
public class Test01 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("test01...");
//客户端重定向
response.sendRedirect("test2");
}
}
?
|