Data sharing between JSP and servlet (data passing and invocation of data) (1) JSP (page) passes data to the servlet in the most common way: using form parameters or URL hyperlink parameters to implement data delivery (previously mentioned).
(2) The servlet passes data to the JSP (page) to invoke the JSP from the servlet, there are two ways to invoke it: forwarding and redirecting (the last time we talked about how these two methods are implemented and the specific syntax), and the way the data is delivered varies according to different calls (forwarding and redirection). (3) using redirects to implement the servlet call JSP on the JSP file, you need some Java code, you must put the Java code on the program section.
If you have the return value of a variable or method, you need to display it at a specified location, using an expression.
A: Use URL hyperlink parameters to implement JSP page data to the servlet.
On the redirected JSP page, add the hyperlink parameter information.
Disadvantage: It is limited to the transmission of simple and small amounts of information (4KB) and cannot be used to pass collection information. B: Use session to implement JSP page information passed to the Servlet sessions, which are the working mechanism for data sharing between different Web components such as JSP and servlet.
Representative: A set of connections between a user and an application. The first step: Get Session,session without new.
For example, session["user"]= a value or null.
Step two: The data that will be passed (can be a string, a value can also be an object or collection) is added as a property to the session.
Session.setattribute ("Property name", value of attribute)//note syntax, and understanding is added as attributes to the session.
Sets the value of the property, which has a key (the property name), and has the value (the values of the property). Note: The value of the property must be the object type third step: The value of the attribute is fetched from the session in the servlet from the property name according to the attribute's name: Object Value=session.getattribute (property name)
);
Not only in the servlet, but in all Java programs, you can get the value of the property in this way.
(4) using forwarding to implement the servlet call JSP A: The use of URL hyperlink parameters to achieve data transfer B: Use the attributes of request to achieve data transfer supplement: Redirect: Response.sendredirect ("redirected page"); Forwarding: Request.getrequestdispatcher (JSP page"). Forward (request,response);//forward parameters must be inside.
Recommendation: When using redirect to implement page turn, it is recommended to use session for data transfer when using forwarding to implement the page turn, it is recommended to use the request property for data transmission