分享
 
 
 

MIDlet与Servlet之间传递Cookie

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

/*--------------------------------------------------

* Cookie.Java

*

* Pass a cookie (stored in rms) between the MIDlet

* and a Java servlet. The cookie is generated

* by the servlet on the first visit.

*

* Example from the book: Core J2ME Technology

* Copyright John W. MUChow http://www.CoreJ2ME.com

* You may use/modify for any non-commercial purpose

*-------------------------------------------------*/

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.rms.*;

import javax.microedition.io.*;

import java.io.*;

public class Cookie extends MIDlet implements CommandListener

{

private Display display;

private TextBox tbMain;

private Form fmMain;

private Command cmExit;

private Command cmLogon;

private String cookie = null;

private RecordStore rs = null;

static final String REC_STORE = "rms_cookie";

private String url = "http://www.mycgiserver.com/servlet/corej2me.CookieServlet";

public Cookie()

{

display = Display.getDisplay(this);

// Create commands

cmExit = new Command("Exit", Command.EXIT, 1);

cmLogon = new Command("Logon", Command.SCREEN, 2);

// Create the form, add commands, listen for events

fmMain = new Form("");

fmMain.addCommand(cmExit);

fmMain.addCommand(cmLogon);

fmMain.setCommandListener(this);

// Read cookie if available

openRecStore();

readCookie();

// System.out.println("Client cookie: " + cookie);

}

public void startApp()

{

display.setCurrent(fmMain);

}

public void pauseApp()

{ }

public void destroyApp(boolean unconditional)

{

closeRecStore(); // Close record store

}

public void openRecStore()

{

try

{

// The second parameter indicates that the record store

// should be created if it does not exist

rs = RecordStore.openRecordStore(REC_STORE, true);

}

catch (Exception e)

{

db("open " + e.toString());

}

}

public void closeRecStore()

{

try

{

rs.closeRecordStore();

}

catch (Exception e)

{

db("close " + e.toString());

}

}

/*--------------------------------------------------

* Write cookie to rms

*-------------------------------------------------*/

public void writeRecord(String str)

{

byte[] rec = str.getBytes();

try

{

rs.addRecord(rec, 0, rec.length);

}

catch (Exception e)

{

db("write " + e.toString());

}

}

/*--------------------------------------------------

* Read cookie from rms

*-------------------------------------------------*/

public void readCookie()

{

try

{

byte[] recData = new byte[25];

int len;

if (rs.getNumRecords() > 0)

{

// Only one record will ever be written, safe to use '1'

if (rs.getRecordSize(1) > recData.length)

recData = new byte[rs.getRecordSize(1)];

len = rs.getRecord(1, recData, 0);

cookie = new String(recData);

}

}

catch (Exception e) {

db("read " + e.toString());

}

}

/*--------------------------------------------------

* Send client request and recieve server response

*

* Client: If cookie exists, send it to the server

*

* Server: If cookie is sent back, this is the

* clients first request to the server. In

* that case, save the cookie. If no cookie

* sent, display server body (which indicates

* the last time the MIDlet contacted server).

*-------------------------------------------------*/

private void connect() throws IOException

{

InputStream iStrm = null;

ByteArrayOutputStream bStrm = null;

HttpConnection http = null;

try

{

// Create the connection

http = (HttpConnection) Connector.open(url);

//----------------

// Client Request

//----------------

// 1) Send request method

http.setRequestMethod(HttpConnection.GET);

// If you eXPerience connection/IO problems, try

// removing the comment from the following line

//http.setRequestProperty("Connection", "close");

// 2) Send header information

if (cookie != null)

http.setRequestProperty("cookie", cookie);

System.out.println("Client cookie: " + cookie);

// 3) Send body/data - No data for this request

//----------------

// Server Response

//----------------

// 1) Get status Line

if (http.getResponseCode() == HttpConnection.HTTP_OK)

{

// 2) Get header information

String tmpCookie = http.getHeaderField("set-cookie");

System.out.println("server cookie: " + tmpCookie);

// Cookie will only be sent back from server only if

// client (us) did not send a cookie in the first place.

// If a cookie is returned, we need to save it to rms

if (tmpCookie != null)

{

writeRecord(tmpCookie);

// Update the MIDlet cookie variable

cookie = tmpCookie;

fmMain.append("First visit\n");

fmMain.append("Client : " + cookie + "\n");

}

else // No cookie sent from server

{

// 3) Get data, which is the last time of Access

iStrm = http.openInputStream();

int length = (int) http.getLength();

String str;

if (length != -1)

{

byte serverData[] = new byte[length];

iStrm.read(serverData);

str = new String(serverData);

}

else // Length not available...

{

bStrm = new ByteArrayOutputStream();

int ch;

while ((ch = iStrm.read()) != -1)

bStrm.write(ch);

str = new String(bStrm.toByteArray());

}

// Append data to the form

fmMain.append("Last access:\n" + str + "\n");

}

}

}

finally

{

// Clean up

if (iStrm != null)

iStrm.close();

if (bStrm != null)

bStrm.close();

if (http != null)

http.close();

}

}

/*--------------------------------------------------

* Process events

*-------------------------------------------------*/

public void commandAction(Command c, Displayable s)

{

// If the Command button pressed was "Exit"

if (c == cmExit)

{

destroyApp(false);

notifyDestroyed();

}

else if (c == cmLogon)

{

try

{

// Logon to the servlet

connect();

}

catch (Exception e)

{

db("connect " + e.toString());

}

}

}

/*--------------------------------------------------

* Simple message to console for debug/errors

* When used with Exceptions we should handle the

* error in a more appropriate manner.

*-------------------------------------------------*/

private void db(String str)

{

System.err.println("Msg: " + str);

}

}

/*--------------------------------------------------

* CookieServlet.java

*

* Use a cookie to identify clients

*

* Example from the book: Core J2ME Technology

* Copyright John W. Muchow http://www.CoreJ2ME.com

* You may use/modify for any non-commercial purpose

*-------------------------------------------------*/

//package corej2me; // Required for mycgiserver.com

import java.util.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

import java.text.*;

public class CookieServlet extends HttpServlet

{

// Pool of client ID's

private static int[] clientIDs = {123, 456, 789, 901, 225, 701};

protected void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException

{

// Get cookie from the header

Cookie[] cookies = req.getCookies();

//-------------------------------------------

// If cookie passed in...

// 1) Lookup the client ID in the database

// and save the last access date

// 2) Update the last access date in database

// 3) Return to the client date from step 1

//-------------------------------------------

if (cookies != null)

{

// There will be only one cookie

Cookie theCookie = cookies[0];

String id = theCookie.getValue();

// Lookup client ID and get last access date

String strLastAccess = lookupLastAccessDate(Integer.parseInt(id));

// Update database with current date

updateLastAccessDate(Integer.parseInt(id));

// Send back the last access date to the client

res.setContentType("text/plain");

PrintWriter out = res.getWriter();

out.print(strLastAccess);

out.close();

}

else // No Cookie

{

//-------------------------------------------

// Generate a client ID. To keep the database

// from growing out of control, this will not

// generate a new ID for each client.

// Instead, grab a random ID from the array

// clientID's[]. The end result is the same

// as far as the client is concerned.

//-------------------------------------------

// Random value between 0 and the number of

// entries in the client list array

int random = (int) Math.round(clientIDs.length * Math.random());

// Get the client ID to send in the cookie

int ID = clientIDs[random];

// Update database with current date

updateLastAccessDate(ID);

// Create new cookie and send ID in the header

Cookie cookie = new Cookie("ID", Integer.toString(ID));

res.addCookie(cookie);

}

}

/*--------------------------------------------------

* Update database with last access date for client ID

*-------------------------------------------------*/

private void updateLastAccessDate(int ID)

{

Connection con = null;

Statement st = null;

StringBuffer msgb = new StringBuffer("");

try

{

// These will vary depending on your server/database

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con = DriverManager.getConnection("jdbc:odbc:acctInfo");

Statement stmt = con.createStatement();

// Create a date format

SimpleDateFormat format =

new SimpleDateFormat ("MMM dd-hh:mm aa");

String strDate = format.format(new java.util.Date());

ResultSet rs = stmt.executeQuery("UPDATE clientInfo set lastAccess = '" +

strDate + "' where clientID = " + ID);

}

catch (Exception e)

{ }

}

/*--------------------------------------------------

* Lookup the client ID in database and get the

* last access date

*-------------------------------------------------*/

private String lookupLastAccessDate(int id)

{

Connection con = null;

Statement st = null;

StringBuffer msgb = new StringBuffer("");

try

{

// These will vary depending on your server/database

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con = DriverManager.getConnection("jdbc:odbc:acctInfo");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("Select lastAccess from clientInfo where clientID = " + id);

if (rs.next())

return rs.getString(1);

else

return null;

}

catch (Exception e)

{

return e.toString();

}

}

/*--------------------------------------------------

* Information about servlet

*-------------------------------------------------*/

public String getServletInfo()

{

return "CookieServlet 1.0 - John W. Muchow - www.corej2me.com";

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有