Then look at the JSP built-in objects.
Response Object
The response object is an instance of the Javax.servlet.http.HttpServletResponse class. When the server creates a Request object, it creates a
The response object that responds to this client.
The response object also defines the interface that handles the HTTP header module. With this object, developers can add new cookies, timestamps,
HTTP status code, and so on.
The Response object contains information about the response to a customer request, but it is seldom used directly in the JSP. The response object has a page scope, which is
When a page is accessed, the response object of the page is valid only for this access, and the response object for the other page is not valid for the current page.
The response object represents the response to the client, primarily by passing objects processed by the JSP container back to the client. The response object also has a
With a domain, it is only valid within the JSP page.
Common methods for response objects are as follows:
Instance:
We create a name called Response.jsp page:
Operation Result:
We see that the order of the outputs differs from the order in which we write the code, and the output of the PrintWriter object is ahead of the output of the built-in object out object, which
How do we do the normal sequential output?
We can do this by forcing the buffer to be emptied before the output:
Operation Result:
We learned the Reuqest object and the Reposnse object, then another important point of view is the request redirection and request forwarding.
Life Example:
Suppose you go to a passport:
Redirect: You went to the a innings first, the people of a bureau said: "This thing does not belong to our management, to the B Bureau", then, you have to withdraw from a, you ride to
The B-Bureau.
Forwarding: You went to a bureau, a after watching, know that this thing should actually B to tube, but he did not return you, but let you sit for a while
Son, oneself to the back office contact B's people, let them do well later, sent over.
Using code validation
Request Redirect: Client behavior, Response.sendredirect (). Essentially equivalent to two requests, the previous request object is not saved,
The URL address of the address bar will change.
Example:
We continue to use the Reg.jsp User registration page, but we need to submit a page to read:
On the response.jsp page we use the Response object to redirect to the request.jsp page:
Enter your username and preferences on the registration page:
The final result:
Request Forwarding: Server-side behavior, Request.getrequestdispatcher (). Forward (Requset,response); is a request, forwarded
After the request object is saved, the URL address of the address bar does not change.
Or the above example:
This time we comment out the request redirection in the Response.jsp page, and enter the following line to forward the code:
or enter the user name and choose a hobby, the final result:
Session Object
What is a session?
Session represents a client-to-server conversation. A session in the Web refers to when a user browses a Web site, from entering the Web site to the browser
The time it took to close, that is, the time that the user spent browsing the site. As can be seen from the definition above, the session is actually a special
The time-definite concept. A session of different users is stored in the server's memory.
Open a browser from a client and connect to the server to start, to the client close the browser to leave the end of this server, known as a session. When a
When a client accesses a server, it is possible to repeatedly connect between several pages of the server, refresh a page repeatedly, and the server should
Law knows this is the same customer, which requires a session object.
Session Object
The session object is an instance of the Javax.servlet.http.HttpSession class. Session objects are automatically created by the server that are related to user requests
Object. The server generates a Session object for each user that holds the user's information and tracks the user's operational status. Inside of Session Object
The map class is used to save the data, so the format of the saved data is "Key/value". The value of the session object can make complex object types, not just
Limited to string types only.
The ID of the session object refers to when a client accesses a JSP page on the server for the first time, the JSP engine produces a session object and
With a string ID number, the JSP engine sends this ID number to the client and stores it in a cookie so that the session object and the client
One by one corresponding relationships were established. When customers re-access other pages that connect to the server, they are no longer assigned to the customer's new session object until the customer closes
After the browser, the server side of the client's session object is canceled, and the client's conversation with the corresponding relationship disappears. When the customer re-opens the browser and then connects to
The server, the server creates a new session object for the customer. Session end refers to the close of all pages associated with the sessions.
The session object has a certain time expiration problem, so there is a name value pair in the session will be lost after a certain time, you can change the session by
Time to avoid this situation. While programming to avoid storing large amounts of useful information in the session, request is a good alternative.
The session is one of the four fields of JSP, but we often use session to do conversation management/session tracking, that is, when the user logs on, the user's login
The land information is placed inside the session and checked in some internal pages to see if it is the current session to ensure that the current user is not a visitor and has more rights
such as adding to the shopping cart, paying for purchases, etc. And usually we just set up a session so that when the user logs in, the user information is automatically
Save to the session field.
When you browse the product, click on the purchase will prompt you have not logged in, until after the login, your session to start tracking your browsing records, you have to buy
Buy the right. The session is stored on the server, the underlying is dependent on the cookie, and your entire browsing history in the browser is saved to the cookie (cache) when
When you log in, the server-side session is created (remember, the session only stores information related to the user).
The shopping process in our life:
Common methods for session objects:
Instance:
Let's create two JSP pages, one for the session_page1.jsp page:
The other is the session_page2.jsp page:
Operation Result:
In the session_page1.jsp page, we can set multiple properties and property values saved by the session:
Then the session_page2.jsp may also need to accept so much:
Last Run Result:
session life cycle
1) Create:
When a client accesses a JSP or servlet for the first time, the server creates a SessionID for the current session, each time the client to the server-side
When the request is sent, the SessionID is carried in the past and the server verifies the SessionID.
2) Activities:
A new page that is opened by a hyperlink in a session belongs to the same session.
As long as the current session page does not close all, reopen the new browser window to access the same project resource when it belongs to the same session.
Unless all the pages of this session are closed and then re-access a JSP or the servlet will create a new session.
Note: Note that the original session still exists, but this old SessionID still exists on the server, but no more clients will carry it
After delivery to the service-side check.
3) Destruction:
There are three ways to destroy a session:
1 called the Session.invalidate () method
2session expired (timed out)
3 Server Restart
Example:
We set the maximum life cycle time on the session_page1.jsp page, and then jump to the session_page.jsp page to receive the
Information is not saved.
Add a line of code to the session_page1.jsp page:
Operation Result:
The Setmaxinactiveinterval () time is set on the session_page1.jsp page, such as Setmaxinactiveinterval (10), whether you
Whether to jump to the session_page2.jsp page, after 10s, the ID of SESSION_PAGE1 session is invalid, so session_page2 behind the
SessionID is also invalid.
We can verify the conclusion of this stage of the session life cycle activity through the code.
1) Testing a session to open a new page with a hyperlink belongs to the same session
We've met this one before, and this time we're using the same code to demonstrate:
Sessionlifedemo.jsp page Code:
The session_page2.jsp page code is still unchanged.
Operation Result:
2) As long as the current session page does not close all, reopen the new browser window to access the same project resource when it belongs to the same session.
We re-open the new browser window again without closing the sessionlifedemo.jsp page and session_page2, this time we
Separate access to the SESSION_PAGE2.JSP page, ID code or the same as above, indicating that this situation is still the same session.
Please see the results:
3) All pages of this session are closed before re-accessing a JSP or the servlet will create a new session.
This time we all close all pages of this session, restart the browser, and reopen the sessionlifedemo.jsp page.
Re-open results:
4) The Invalidate () method using the Sessio object is to destroy the current session
Each time the page is refreshed, a new session is made, that is, the SessionID is different after each refresh. Session is a server-side save user letter
The server side generates the corresponding SessionID, the next customer service side and the server side of the contact identification is the SessionID. Destruction is the service
The session information of the terminal is released and the resource is recycled.
this is relatively simple and is no longer verified.
Java Web Learning (three): JSP nine built-in Objects (ii)