In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Cookie technology in J2ME platform. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Application and implementation of Cookie Technology in J2ME platform
Cookie is widely used in Web applications to maintain the state between the browser and the server. Unfortunately, this feature is not supported in the JavaME platform. Therefore, URL rewriting must be used to maintain the client-side and server-side state. URL rewriting is troublesome to operate, so it is a good attempt to study the principle of cookie and implement cookie on the JavaME platform.
First, let's take a look at the principle of cookie. When the server needs to maintain a certain state with the browser, for example, it needs to record the items that have been purchased in the user's shopping cart. At this point, the server can create a new Cookie and write it to the response, and the browser receives the cookie from the response and saves it. When the browser sends the request to the server again, the browser will check whether there is a matching cookie according to the domain (domain) and path (path). If so, send the cookie to the server in the form of "name = value", and the server will know the user's status by parsing the cookie from the request.
So, according to what rule does the browser decide to send cookie to the server, the first thing to do is to match domain. If the domain property of cookie is .google.com, then cookie will not be sent when the request is directed to j2medev.com. If the conditions for the domain match are met, it is determined whether the path matches, and if the path attribute of the cookie is the parent directory of the requested uri, then the cookie is sent to the server. Cookie has a survival cycle, and expired cookie will be automatically cleared by the browser. If the server does not set the life cycle when creating the cookie, the browser deletes the cookie at the end of the session. If you do not specify the path attribute for cookie, the default is the path of this request.
Cookie is used in many web applications, such as remembering passwords, shopping carts, etc. When developing MIDlet, you can also make your application support cookie, which makes it easy to maintain the client-side and server-side state, laying the foundation for you to focus on other business methods. Now that you know how cookie works, you should consider whether it is feasible to implement cookie on the JavaME platform. I will analyze it from the following three aspects.
*: get cookie
When the server-side response arrives, we should be able to read cookie. If the server writes Cookie to the client, the HTTP header "Set-Cookie" in the response contains a string that represents the cookie information. Fortunately, we use the HttpConnection.getHeaderFiled ("Set-Cookie") method to get the cookie, but it's important to note that only one cookie is read here, and if the response contains multiple cookie, you need to read it in a loop. This code is similar to the following
StringsCookie=null; Stringkey=null; inti=0; / / if key exists, query the key of header. If key equals SET_COOKIE, store while ((key=connection.getHeaderFieldKey (I))! = null) {if (key.equals (SET_COOKIE) | | key.equals (SESSIONID)) {sCookie=connection.getHeaderField (I); saveCookie (sCookie,url);} iTunes;}
The above code reads down the cookie content where header is Set-Cookie and SesssionID.
Second: save cookie
Once you have obtained the cookie, you need to store the cookie. The storage is divided into two parts. First, you need to parse the cookie. We define a JavaBean to represent the cookie.
Packagecom.j2medev.lomol.model; importcom.j2medev.lomol.util.StringUtil; importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.io.IOException; importjava.util.Date; / * acookiestoredonthemobiledevice, cookieisusedtomaintainthestatesbetweenclientandserver * @ authormingjava * @ version0.105/06/2006 * / publicclassCookie {privateStringpath= "; privateStringname="; privateStringvalue= ""; privatelongexpire=SESSION_COOKIE; publicstaticlongSESSION_COOKIE=0;//sessioncookie,onlyvalidthissession publicCookie () {} publicStringgetPath () {returnpath } publicvoidsetPath (Stringpath) {this.path=path;} publicStringgetName () {returnname;} publicvoidsetName (Stringname) {this.name=name;} publicStringgetValue () {returnvalue;} publicvoidsetValue (Stringvalue) {this.value=value;} publicvoidserialize (DataOutputStreamdos) throwsIOException {dos.writeUTF (name); dos.writeUTF (value); dos.writeUTF (path); dos.writeLong (expire);} publicstaticCookiedeserialize (DataInputStreamdis) throwsIOException {Cookiecookie=newCookie (); cookie.name=dis.readUTF (); cookie.value=dis.readUTF () Cookie.path=dis.readUTF (); cookie.expire=dis.readLong (); returncookie;} publiclonggetExpire () {returnexpire;} publicvoidsetExpire (longexpire) {this.expire=expire;} / / fordebug publicStringtoString () {returnname+ "=" + value+ "; expires=" + newDate (expire). ToString () + "; path=" + path;} publicbooleanisExpired (longnow) {returnexpire-now
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.