Servlet技术在WAP开发中的应用

      手机 2004-8-30 20:57
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.";

}




}

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}