Servlet foundation (2)

xiaoxiao2021-03-06  52

I. Conversation Tracking Session: From the point of view of the web server, a session contains all requests performed during a single browser call, in other words, when we open the browser, the session begins, close the browser, the session ends. The appearance of the problem: a servlet is for you to use, it is impossible to use a client separately, or it can be said that it is stateless (information that cannot store a single client), it is for multiple client calls (access), and The emergence of the problem is to store information for each client's own characteristics (such as the shopping cart of the e-mail). Problem Solution: First: Do you distinguish between using each client's IP address? No, because you may run multiple customers on a machine, requests may also be through the proxy server route, in these two cases, the IP address cannot be used as a unique identifier. Second: cookie storage or URL rewrite cookies is a piece of data that can be embedded in the HTTP request or response, or it can be so understanding, it is a text file in the client memory or hard disk or a string, which initially Netscape introduced. A typical situation is that the web server embeds the cookie value in a response head, and the browser will return the same cookie value in each sub-request. Some useful code: cookie cookies [] = myHttpRequest.getCookies (); // Return is a cookie array cookie c = cookies [i]; string myname = c.getname (); string myvalue = c.getValue (); String mycomment = c.getComment (); // Return this cookie purpose note ?? int mymaxage = c.getMaxage (); // Returns the maximum validity period of this cookie, using seconds, default, -1 pointing out Cookie will always exist to the browser session termination. URL Reword: For some special users or browsers that do not support cookie, we have to use URL to override technology, all of the connection and relocation created by servlet must be encoded, and the session ID is part of the URL. The method of URL encoding is based on a particular server, but it is most likely to increase parameters or additional path information. String url = myhttpRequest.getRequestURI (); MyHttpResponse.encodeURL (url); two, Servlet API 1 ,? Interface Name: javax.servlet.HttpServletRequest methods: public String getAuthType (); // returns to protect the authentication mode servlet name. Public cooki getCookis (); // Returns an array that contains all the cookie objects sent by the client. If you don't have a cookie, return null. Public string getMethod (); // Return to process the name of the HTTP method of this request, such as GET, POST, PUT PUBLIC STRING getQueryString (); // The query string included in the request URL after the path is returned. Public httpsession getsession (boolean); // Returns the current httpsession associated with this request, or if there is no current session, and create a logo is true, return a new session.

转载请注明原文地址:https://www.9cbs.com/read-115807.html

New Post(0)