点击这里给我发消息 点击这里给我发消息
首页 > 行业资讯 > Wap>详细内容

Servlet技术在WAP开发中的应用

添加时间:2010-1-5
    相关阅读: 软件 开发 技术 页面 程序 Wap JSP

Servlet技术在WAP开发中的应用
WAP开发中应用Servlet技术时要利用专用WAP服务器软件,如Nokia WAP Server等提供的开放程序接口来编写具体的程序。我们仍以WML为例,它应用Servlet编程的规则与应用JSP时的规则基本相同,我们不再逐条给出解释。通过下面的简单例子,结合JSP的编写规则,大家不难了解Servlet编写WML页面的基本规则:

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class WapServlet extends HttpServlet

{

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException

 {

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

  PrintWriter out = response.getWriter();

  xmlHeader(out);

  out.println("<wml>");

  out.println("<card title = \"JavaTest\">");

  out.println("<p>Hello! This comes from a servlet!</p>");

  out.println("</card>");

  out.println("</wml>");

 }

 

 public void xmlHeader(PrintWriter out)

 {

  out.println("<?xml version=\"1.0\"?>");

  out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" "+

        "\"http://www.wapforum.org/DTD/wml_1.1.xml\">");

 }

}

 

下面我们给出一个简单的Servlet程序,它可在WAP浏览器上显示“The simple HelloWorld servlet.”的信息。程序清单如下:

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class HelloWorld extends HttpServlet

{

String m_text;

 

public void init(ServletConfig config) throws ServletException

{

  super.init(config);

  m_text = config.getInitParameter("text");

  if (m_text == null)

  {

    m_text = "This is a simple test servlet.";

  }

}

 

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

{

  PrintWriter out = response.getWriter();

  out.println("<?xml version=\"1.0\"?>");

  out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML1.1//EN\"\"http://www. wapforum.org/DTD/wml_1.1.xml/">");

  out.println("<wml>");

  out.println("<card id=\"card1\" title=\"Hello World\">");

  out.println("<p>");

 

  out.println(m_text);

  out.println("</p>");

  out.println("</card>");

  out.println("</wml>");

  // Remember to close the out object

  out.close();

}

 

public String getServletInfo( )

{

  return "The simple HelloWorld servlet.";

}

}

本文作者:
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册