Article Directory
First, the request is forwarded
Forwards the request : an in 服务器内部的资源跳转方式
.
Suppose you want to complete a very complex operation, we visited AServlet class to complete, but only to make a method AServlet class to complete this complex operation and unreasonable. Try a single class of functional refinement, we define two classes AServlet and BServlet, when AServlet executed after the jump to BServlet. This is the way of resources to jump.
(1) the step of forwarding the request
1. Get Object request object request forwarder: RequestDispatcher getRequestDispatcher(String path);
2. The method of requestDispatcher forward by forward objects:forward(ServletRequest request,ServletResponse response);
(2) request the forwarding of the request characteristics ☆
- ① browser address bar does not change the path.
- ② can only be forwarded to the current internal server resources.
- ③ is a forwarding request.
Second, data sharing
(1) request the object field
Domain objects : a scope of the object, the range data can be shared.
request field : a request on behalf of a range, generally a plurality of resource requests forwarded shared data.
(2) providing a method
void setAttribute(String name,Object obj) | Storing data |
Object getAttribute(String name) | Get the value through the key |
removeAttribute(String name) | By removing the key-value pair key |
Third, the request is forwarded presentation and data sharing
Write two classes inherit the HttpServlet, forwarded to RequestDemo8 in RequestDemo7.
package com.hudie.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 类说明:
* 请求转发演示
* @author qianliangguo
*/
@WebServlet("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("Demo7...");
request.setAttribute("msg", "hello");
//转发到Demo8资源
//先获得请求转发器对象,再通过对象的forward方法转发
request.getRequestDispatcher("/requestDemo8").forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}
package com.hudie.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 类说明:
* 请求转发演示
* @author qianliangguo
*/
@WebServlet("/requestDemo8")
public class RequestDemo8 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("Demo8...");
Object msg = request.getAttribute("msg");
System.out.println(msg);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}
Address bar http://localhost:8080/Servlet&http&request/requestDemo7
auto-forwarding After visiting RequestDemo7 to RequestDemo8: