应用Java技术开发WAP应用程序

类别:Java 点击:0 评论:0 推荐:
 WAP开发入门--搭建开发环境一文中介绍了如何搭建WAP开发应用程序,这篇文章将讲述如何使用Java技术开发WAP应用程序。

    我们还是采用Tomcat作为web服务器,如果你对如何使用tomcat还不熟悉请参考Tomcat入门指南。通常我们开发WAP应用程序的时候都是手写wml脚本,其实我们可以借助java的Servlet/jsp技术开发WAP应用程序。通常我都是使用Lomboz插件在eclipse的环境下写servlet/jsp文件的。先看下面的waptest.jsp文件和WapServlet.java文件
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<%
response.setContentType("text/vnd.wap.wml");
out.println("<wml>");
out.println("<card title=\"MobileDate\">");
out.println(" <p align=\"center\">");
out.println("Date and Time Service<br/>");
out.println("Date is: "+ new java.util.Date());
out.println("</p>");
out.println("</card>");
out.println("</wml>");
%>

package com.j2medev.mingjava;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class WapServlet extends HttpServlet
{

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {

        response.setContentType("text/vnd.wap.wml");

        PrintWriter out = response.getWriter();

        out.println("<?xml version=\"1.0\"?>");
        out.println("<!DOCTYPE wml PUBLIC\"-//WAPFORUM//DTD WML 1.1//EN\"");
        out.println("\"http://www.wapforum.org/DTD/wml_1.1.xml\">");
        out.println("<wml>");
        out.println("<card title=\"MobileDate\">");
        out.println(" <p align=\"center\">");
        out.println("Date and Time Service<br/>");
        out.println("Date is: " + new java.util.Date());
        out.println("</p>");
        out.println("</card>");
        out.println("</wml>");

    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request,response);
    }
}
这两个文件实现的功能是一样的,显示服务器当前的时间。我们的web.xml的内容如下:
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
  <servlet-name>WapServlet</servlet-name>
  <servlet-calss>com.j2medev.mingjava.WapServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>WapServlet</servlet-name>
  <url-pattern>/wapservlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <error-page>
  <error-code>404</error-code>
  <location>/error.jsp</location>
 </error-page>

</web-app>
使用Lomboz我们可以很方便的把这个应用程序发布到tomcat的webapps目录,通过WinWap我们可以直接通过下面两个URL访问到他们
http://localhost:8088/wap/wapservlet 
http://localhost:8088/wap/waptest.jsp

本文地址:http://com.8s8s.com/it/it15241.htm