WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发MIDlet与Servlet之间传递Cookie

MIDlet与Servlet之间传递Cookie
作者:java2s  来源:java2s  发布时间:2006-7-25 8:59:29

        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 = {123456789901225701};
  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 = (intMath.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";
  }
}

上一页  [1] [2] 

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码