應用Java技術開發WAP應用程序 – JAVA編程語言程序開發技術文章

我們還是采用Tomcat作為web服務器,如果你對如何使用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

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。