亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊(cè) 如何應(yīng)用Session開(kāi)發(fā)非Web終端

如何應(yīng)用Session開(kāi)發(fā)非Web終端

Jun 21, 2016 am 09:15 AM
cookie httpclient quot session tomcat

session|web



如何應(yīng)用Session開(kāi)發(fā)非Web終端





協(xié)議s

— 作者 sunggsun @ 20:27




Session(會(huì)話)是Web上較為有效的信息交互手段。因其使用方便、穩(wěn)定、安全可靠而被眾多Web開(kāi)發(fā)者所青睞。尤其在互聯(lián)網(wǎng)身份認(rèn)證、網(wǎng)上電子購(gòu)物等方面的應(yīng)用更為廣泛。無(wú)獨(dú)有偶,筆者在開(kāi)發(fā)一個(gè)財(cái)政項(xiàng)目的數(shù)據(jù)中心平臺(tái)時(shí),覺(jué)得數(shù)據(jù)傳送部分的身份認(rèn)證和信息交互與Web領(lǐng)域的Session控制極其相似。于是就想嘗試一下這新技術(shù),通過(guò)查閱大量資料后覺(jué)得在非Web客戶端用Session進(jìn)行信息交互也切實(shí)可行。經(jīng)過(guò)反復(fù)測(cè)試成功后應(yīng)用于項(xiàng)目中,成效顯著,省去了較多的臨時(shí)數(shù)據(jù)保存以及繁鎖的狀態(tài)檢測(cè),由Session自動(dòng)維持狀態(tài)。

好東西不能獨(dú)享,筆者想把這次成功應(yīng)用Session控制進(jìn)行非Web開(kāi)發(fā)的關(guān)鍵技術(shù)點(diǎn)講述一下,來(lái)共同探討。我們知道Cookie是Web上最常用的跟蹤用戶會(huì)話方式,當(dāng)Cookie被禁止后,一般都用URL重寫(xiě)來(lái)跟蹤會(huì)話。那么Cookie到底是什么東西呢?按照定義:Cookie是一種由服務(wù)器發(fā)送給客戶的片段信息,存儲(chǔ)在客戶環(huán)境中,并且在客戶所有的對(duì)服務(wù)器的請(qǐng)求中都要發(fā)回它。舉個(gè)例子說(shuō),當(dāng)我們用IE登錄某個(gè)電子購(gòu)物商城時(shí),IE在得到商品列表頁(yè)面的同時(shí)還收到Set-Cookie應(yīng)答頭信息。這個(gè)信息的格式為“Set-Cookie:NAME=VALUE;Comment=COMMENT;Domain=DOMAINNMAE;Max-age=SECONDS;Path=PATH;secure;Version=1*DIGIT”,其中NAME值對(duì)(值對(duì)間用分號(hào)分隔)是必須的,其余都是可選的。最重要的信息當(dāng)然也在所必須的值對(duì)里了,VALUE是NAME的值,也是這個(gè)Cookie的標(biāo)識(shí),Max-age定義了Cookie的最長(zhǎng)生存時(shí)間,其它幾個(gè)可選值對(duì)可參閱http://www.faqs.org/rfcs/rfc2109.html。當(dāng)我們選購(gòu)了某種商品,向服務(wù)器發(fā)送選購(gòu)清單時(shí),會(huì)自動(dòng)在你的請(qǐng)求信息頭里加上NAME值對(duì),如果Cookie被禁止,則用URL重寫(xiě)方式在URL請(qǐng)求地址上附加NAME值對(duì)。當(dāng)Web服務(wù)器收到這個(gè)請(qǐng)求后,會(huì)檢查該Cookie是否存在,然后相應(yīng)的跟蹤會(huì)話。從以上分析不難理解,其實(shí)Web服務(wù)器跟蹤會(huì)話就靠Set-Cookie頭信息,跟蹤NAME值對(duì)進(jìn)行身份驗(yàn)證。假如我們用非Web終端接收Web服務(wù)器的響應(yīng)信息,從中解析出Cookie頭信息,當(dāng)再次向Web服務(wù)器發(fā)送請(qǐng)求時(shí)附加上解析出的Cookie信息,Web服務(wù)器據(jù)此不就可以進(jìn)行身份認(rèn)證了嗎?
有了上面的分析,我們寫(xiě)出代碼也非常方便了。下面是筆者用C++Builder 6應(yīng)用程序與Apache Tomcat 4.0服務(wù)引擎中的Servlet交互的演示代碼,僅作參考。

C++客戶端在初次向服務(wù)器發(fā)請(qǐng)求時(shí)的代碼如下:
TIdHTTP *HTTPClient = new TIdHTTP(NULL);
TIdHeaderList * hList;
String URL= "http://localhost:8080/Rev/servlet/test";
try
{
try
{
HTTPClient->Get(URL);
if (HTTPClient->Response != NULL)
{
hList = HTTPClient->Response->ExtraHeaders;
String cookie = hList->Values["Set-Cookie"];
int pos = cookie.Pos(";");
if (pos > 0)
Session_ID = cookie.SubString(1,pos-1);
else
Session_ID = cookie;
}
} catch(Exception& E)
{
}
} __finally
{
HTTPClient->Free();
}
上面代碼中變量URL指向所在Servlet的HTTP地址,根據(jù)各自情況賦值;變量Session_ID為全局變量,記錄Cookie。下次交互時(shí)只需在HTTPClient請(qǐng)求前加上“HTTPClient->Request->ExtraHeaders->Add("Cookie:" + Session_ID);”,Apache Tomcat就會(huì)自動(dòng)判別有效性了。簡(jiǎn)單嗎?

Servlet服務(wù)端的有效性驗(yàn)證也比較容易,具體的Cookie認(rèn)證過(guò)程就讓Apach Tomcat引擎去做了,如下所示:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=GBK");
PrintWriter out = response.getWriter();
out.println("");
out.println("

身份認(rèn)證");
out.println("");
HttpSession session = request.getSession(false);
if (session != null) {
out.println("

身份確認(rèn)

");
} else {
out.println("

認(rèn)證失敗

");
}
out.println("");
}
代碼中最關(guān)鍵的是“request.getSession(false);”,參數(shù)為true時(shí)Apache Tomcat建立一個(gè)新的Session;參數(shù)為false時(shí)Apache Tomcat會(huì)根據(jù)request中的信息尋找相關(guān)聯(lián)的Session。所以想要維持Session的持續(xù)性,必須用參數(shù)false調(diào)用,但如果長(zhǎng)時(shí)間沒(méi)調(diào)用該Session,Apache Tomcat為合理利用資源會(huì)自動(dòng)使該Session無(wú)效,有關(guān)Apache Tomcat的管理機(jī)制及其配置可參考http://jakarta.apache.org/。
演示代碼中用了C++Builder自帶的TIdHTTP組件,該組件嚴(yán)格按照HTTP規(guī)范實(shí)現(xiàn),Delphi中也有該組件,Visual C++也有類似的MFC,可根據(jù)各自熟悉的開(kāi)發(fā)平臺(tái)調(diào)試,調(diào)試時(shí)必須把用于認(rèn)證的Servlet程序加載到Apach Tomcat上,并重新啟動(dòng)Apach Tomcat。以上代碼只供演示而已,要實(shí)際應(yīng)用還需增加各種異常處理和HTTPClient的Request請(qǐng)求包以及Servlet的Response響應(yīng)包的處理,有興趣的朋友可通過(guò)Email:21cnDeveloper@163.com與筆者進(jìn)一步交流。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to check the number of concurrent connections in tomcat How to check the number of concurrent connections in tomcat Apr 21, 2024 am 08:12 AM

How to check the number of concurrent Tomcat connections: Visit the Tomcat Manager page (http://localhost:8080/manager/html) and enter your user name and password. Click Status->Sessions in the left navigation bar to see the number of concurrent connections at the top of the page.

Where is the root directory of the tomcat website? Where is the root directory of the tomcat website? Apr 21, 2024 am 09:27 AM

The Tomcat website root directory is located in Tomcat's webapps subdirectory and is used to store web application files, static resources, and the WEB-INF directory; it can be found by looking for the docBase attribute in the Tomcat configuration file.

How to deploy multiple projects in tomcat How to deploy multiple projects in tomcat Apr 21, 2024 am 09:33 AM

To deploy multiple projects through Tomcat, you need to create a webapp directory for each project and then: Automatic deployment: Place the webapp directory in Tomcat's webapps directory. Manual deployment: Manually deploy the project in Tomcat's manager application. Once the project is deployed, it can be accessed by its deployment name, for example: http://localhost:8080/project1.

How to configure domain name in tomcat How to configure domain name in tomcat Apr 21, 2024 am 09:52 AM

To configure Tomcat to use a domain name, follow these steps: Create a server.xml backup. Open server.xml and add the Host element, replacing example.com with your domain name. Create an SSL certificate for the domain name (if required). Add an SSL connector in server.xml, change the port, keystore file, and password. Save server.xml. Restart Tomcat.

How to run two projects with different port numbers in tomcat How to run two projects with different port numbers in tomcat Apr 21, 2024 am 09:00 AM

Running projects with different port numbers on the Tomcat server requires the following steps: Modify the server.xml file and add a Connector element to define the port number. Add a Context element to define the application associated with the port number. Create a WAR file and deploy it to the corresponding directory (webapps or webapps/ROOT). Restart Tomcat to apply changes.

How to run html and jsp on tomcat How to run html and jsp on tomcat Apr 21, 2024 am 09:04 AM

Tomcat can run HTML and JSP. The method is as follows: copy the HTML file to the corresponding subdirectory of the Tomcat directory and access it in the browser. Copy the JSP file to the corresponding subdirectory of the Tomcat directory, and use the <%@ page %> directive to specify the Java code and access it in the browser.

Tomcat maximum number of connections and maximum number of threads Tomcat maximum number of connections and maximum number of threads Apr 21, 2024 am 09:22 AM

The maximum number of Tomcat connections limits the number of clients connected at the same time, while the maximum number of threads limits the number of threads that can handle requests at the same time. These limits prevent server resource exhaustion and are configured by setting the maxConnections and maxThreads properties in server.xml to match server capacity and load.

Where is the tomcat startup error log? Where is the tomcat startup error log? Apr 21, 2024 am 10:11 AM

The Tomcat startup error log is usually located in the catalina.out file. This file contains error information that occurs during the startup process. Common errors include deployment application exceptions, configuration errors, and connection problems. Regularly checking the catalina.out file can help find potential problems.

See all articles