程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 使用WAP的手機監聽你的網絡

使用WAP的手機監聽你的網絡

編輯:關於JAVA

Java是一個開放、標准、通用的網絡程序運行平台,用Java編寫的應用程序可以適用於多種不同的平台和終端而無需修改程序,同樣,基於WAP的手機也可以使用Java的平台或與後端服務器進行交互來完成各種復雜的功能。你有沒有想過,坐在汽車裡或者走在路上就可以輕輕松松的管理你復雜的互聯網絡或局域網絡。使用Java Servlet就可以解決這個問題。

下面是一個可以讓你在支持WAP的手機上監聽你的網絡的簡單的Java servlet程序,類似於dos下的ping命令。這個程序相當在你的主機上執行了ping命令,並在你的手機上顯示結果。 用法如: http://host/servlet/WAPping 或者把IP地址作為一個參數傳給主機,如: http://host/servlet/WAPping?IP_address

你可以在配置文件中更改常用的設置, Servlet程序必須以配置文件名為初始的參數,參數描述了一個文本文件名,文件中存放你設定的信息。

對於 Tomcat 中的web.xml,可以設置:

<servlet>
   <servlet-name>WAPping</servlet-name>
   <servlet-class>WAPping</servlet-class>
   <init-param>
   <param-name>config</param-name>
   <param-value>config file path
   </init-param>
   </servlet>

在配置文件中,空行和以# 或 file://開頭的行都將被忽略,目前支持下列參數:

# prefix 是用於主機命令,默認值為空

# 對於基於NT體系的主機,必須設置prefix=command.com /c

prefix=command.com /c

# ping的選項,默認值為空

# 例如3 顯示請求並解釋地址到主機名:

ping=-n 3 -a

配置文件示例:

#
   # WAP ping settings
   #
   # prefix
   prefix=command.com /c
   # ping
   ping=-a -n 3
   程序清單:(編譯後文件名為WAPping.class,放入你的主機的Servlet目錄下)
   import java.io.*;
   import java.util.Hashtable;
   import java.util.Random;
   import javax.servlet.*;
   import javax.servlet.http.*;
   public class WAPping extends HttpServlet
    {
     public void init(ServletConfig servletconfig)
      throws ServletException 
       {
        super.init(servletconfig);
        NEWLINE = System.getProperty("line.separator");
        separator = System.getProperty("file.separator");
        configuration = new Hashtable();
        readConfig(getInitParameter("config"), configuration);
        }
   public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
   throws ServletException, IOException
   {
    doGet(httpservletrequest, httpservletresponse);
    }
   public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
   throws ServletException, IOException
   {
     String str= "";
     String str1 = "";
     str = HttpUtils.getRequestURL(httpservletrequest).toString();
     int i;
     if((i = str.indexOf("?")) > 0)
      str= str.substring(0, i);
      str1 = httpservletrequest.getQueryString();
      if(httpservletrequest.getHeader("Accept").indexOf("wap.wml") >= 0)
       {
         wmlClient(str, str1, httpservletrequest, httpservletresponse);
         return;
        }
      else
       {
        httpservletresponse.setContentType("text/html");
        PrintWriter printwriter = httpservletresponse.getWriter();
        printwriter.println("");
        printwriter.println("<br><br><center>不能檢測到WAP客戶機</center>");
        printwriter.println("</html>");
        printwriter.flush();
        printwriter.close();
        return;
       }
      }
   private void wmlClient(String str, String str1, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
   throws IOException
    {
     httpservletresponse.setContentType("text/vnd.wap.wml");
     PrintWriter printwriter = httpservletresponse.getWriter();
     printwriter.println("");
     if(str1 == null)
      printwriter.println(MainWmlCard(str));
     else
      if(str1.equals(""))
       {
         printwriter.println(MainWmlCard(str));
       }
      else
       {
        String str2 = getFromQuery(str1, "act=");
        if(str2.equals("3"))
        printwriter.println(writeWmlCommand(s,httpservletrequest.getParameter("cmd")));
        else
         printwriter.println(writeWmlCommand(str, str1));
        }
       printwriter.flush();
       printwriter.close();
      }
     private String MainWmlCard(String str)
   {
    StringBuffer stringbuffer = new StringBuffer("");
    stringbuffer.append("<wml>" + NEWLINE);
    stringbuffer.append("<head>" + NEWLINE);
    stringbuffer.append("<meta http-equiv=\"Cache-Control\" content=\"max-age=0\" forua=\"true\"/>" + NEWLINE);
    stringbuffer.append("</head>" + NEWLINE);
    stringbuffer.append("<card id=\"mainshell\" title=\"WAPping Card\">" + NEWLINE);
    stringbuffer.append("<do type=\"accept\" label=\"Exec\">" + NEWLINE);
    stringbuffer.append("" + NEWLINE);
    stringbuffer.append("" + NEWLINE);
    stringbuffer.append("" + NEWLINE);
    stringbuffer.append("" + NEWLINE);
    stringbuffer.append("<p align=\"center\"><b>WAP ping</b></p>" + NEWLINE);
    stringbuffer.append("<p>Host><input type=\"text\" name=\"sCmd\" value=\"\" emptyok=\"false\"/></p>" + NEWLINE);
    stringbuffer.append("</card>" + NEWLINE);
    stringbuffer.append("</wml>" + NEWLINE);
    return stringbuffer.toString();
   }
   private String getId()
   {
    String str = "";
    synchronized(SessionIdLock)
    {
     long cTM = System.currentTimeMillis();
     Random random = new Random();
     str = String.valueOf(cTM);
     for(int i = cTM; i <= 6; i++)
      str = str + (int)(1.0D + 6D * random.nextDouble());
      }
     return str;
    }
   private String getFromQuery(String str, String str1)
    {
     if(str == null)
      return "";
      int i;
      if((i = str.indexOf(str1)) < 0)
       return "";
       String str2 = str.substring(i + str1.length());
       if((i = str2.indexOf("&")) < 0)
        return str2;
       else
        return str2.substring(0, i);
       }
   private String writeWmlCommand(String str, String str1)
   throws IOException
    {
     StringBuffer stringbuffer = new StringBuffer();
     String str2;
     if(str1 == null)
      str2 = "";
     else
      str2 = str1;
      stringbuffer.append("<wml>" + NEWLINE);
      stringbuffer.append("<head>" + NEWLINE);
      stringbuffer.append("<meta http-equiv=\"Cache-Control\" content=\"max-age=0\" forua=\"true\"/>" + NEWLINE);
      stringbuffer.append("</head>" + NEWLINE);
      stringbuffer.append("<card id=\"execshell\" title=\"WAPping Card\">" + NEWLINE);
      stringbuffer.append("<do type=\"accept\" label=\"Ok\">" + NEWLINE);
      stringbuffer.append("<go href=\"" + str + "?" + "fct" + "=" + getId() + "\"/>" + NEWLINE);
      stringbuffer.append("</do>" + NEWLINE);
      str2 = str2.trim();
      if(str2.length() == 0)
       {
        stringbuffer.append("<p>空的命令行</p>");
        }
      else
       {
        str2 = executeCmd((String)configuration.get("prefix") + " ping " + (String)configuration.get("ping") + " " + str2, "<br/>");
      if(str2.length() > 900)
       str2 = str2.substring(0, 900) + "...";
       stringbuffer.append("<p>" + replaceDollar(str2) + "</p>");
      }
      stringbuffer.append("</card>" + NEWLINE);
      stringbuffer.append("</wml>" + NEWLINE);
      return stringbuffer.toString();
     }
    private String executeCmd(String str, String str1)
    {
     String str2 = "";
      String str4 = str1 + "host>" + str;
      try
      {
       Process process = Runtime.getRuntime().exec(s);
       java.io.InputStream inputstream = process.getInputStream();
       BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream));
       String str3;
       while((str3 = bufferedreader.readLine()) != null) 
        str4 = str4 + str1 + prepareMsg(str3) + NEWLINE;
         process.waitFor();
        bufferedreader.close();
        }
       catch(Exception exception)
       {
        str4 = str4 + str1 + exception;
        }
      return str4;
     }
    public String getServletInfo()
    {
     return "WAP ping 程序";
    }
    private String prepareMsg(String str)
    {
     StringBuffer stringbuffer = new StringBuffer("");
     if(str.length() == 0)
      return "";
      for(int i = 0; i < str.length(); i++)
      {
       char c = str.charAt(i);
       if(c == '>')
        stringbuffer.append(">");
       else
         if(c == '<')
          stringbuffer.append("<");
         else
          if(c == '&')
           stringbuffer.append("&");
          else
           if(c == '"')
            stringbuffer.append(""");
            else
             stringbuffer.append(c);
            }
            return stringbuffer.toString();
           }
     private String replaceDollar(String str)
      {
       StringBuffer stringbuffer = new StringBuffer("");
        for(int i = 0; i < str.length(); i++)
         {
          char c;
          if((c = str.charAt(i)) == '$')
           stringbuffer.append("$$");
          else
            stringbuffer.append(c);
           }
           return stringbuffer.toString();
          }
      private void readConfig(String str, Hashtable hashtable) file://讀入參數文件
       {
         try
          {
           BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(new FileInputStream(str)));
           String str1;
           while((str1 = bufferedreader.readLine()) != null) 
            {
              str1 = str1.trim();
              if(str1.length() > 0)
               {
                int i = str1.indexOf("=");
                if(i > 0 && i < str1.length() - 1 && str1.charAt(0) != '#' && !str1.startsWith("//"))
          hashtable.put(str1.substring(0, i).trim(), str1.substring(i + 1).trim());
          }
         }
        }
     catch(Exception _ex) { }
      if((String)hashtable.get("prefix") == null)
       hashtable.put("prefix", ""); file://如果“prefix”參數為空,則設prefix為默認值
       if((String)hashtable.get("ping") == null)
        hashtable.put("ping", ""); file://如果“ping”參數為空,則設ping為默認值
        }
       public WAPping()
       {
       }
      private static Object SessionIdLock = new Object();
      private static final int HOW_LONG = 6;
      private static final int MAX_WML = 900;
      private static final String DEFTITLE = "WAP ping";
      private static final String DEFPREFIX = "";
      private static final String DEFPING = "";
      private static final String CONFIG = "config";
      private static final String FICT = "fct";
      private static final String CMD = "cmd";
      private static final String ACTION = "act";
      private static final String UPFRAME = "1";
      private static final String DOWNFRAME = "2";
      private static final String RUNCMD = "3";
      private static final String HISTORY = "4";
      private static final String PREFIX = "prefix";
      private static final String PING = "ping";
      private static String NEWLINE = "\n";
      private static String separator = "/";
      private static Hashtable configuration;
     }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved