wap jsp 开发

wap 学习-3 jsp中使用wml

2010-05-03 16:44:15| 分类: WAP |字号 订阅
wml有两个版本;1.0,2.0

1.0 在JSP中,使用如下
<?xml version="1.0"?>
<%@page contentType="text/vnd.wap.wml; charset=gb2312"%>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
或者
<%@page contentType="text/vnd.wap.wml; charset=gb2312"%><?xml version="1.0"?> //这里不能换行
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
否则,使用Opera浏览器浏览无法按wml进行解析浏览

wml的content-type为:text/vnd.wap.wml
mime类型:application/vnd.wap.wml

或者在web.xml中进行全局设置:
public class SetContentTypeFilter implements Filter {
private String contentType;
private String encoding;
public void init(FilterConfig filterConfig) throws ServletException {
contentType = filterConfig.getInitParameter("contentType");
encoding = filterConfig.getInitParameter("encoding");
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;

httpServletRequest.setCharacterEncoding(encoding);
httpServletResponse.setContentType(contentType);
httpServletResponse.setCharacterEncoding(encoding);

chain.doFilter(httpServletRequest, httpServletResponse);
}
public void destroy() {
}
}
web.xml
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.infowarelab.wap.filter.SetContentTypeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>contentType</param-name>
<param-value>text/vnd.wap.wml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注:若使用了struts2+freemarker,必须设置freemarker result-type 的 content-type="text/vnd.wap.wml",否则freemarker默认会使用text/html,即使设置了过滤器也无效

2.0 在JSP中,使用如下
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<link href="/wml/info/style.css" rel="stylesheet" type="text/css" />
<title>我的网易</title>
</head>
<body>
<div id="body" style="width:100%;">

一个web应用同时使用1.0,2.0
使用不同的前缀(如/v1,/v2)来区分两个版本的访问,然后使用过滤器对每个版本统一处理
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.infowarelab.wap.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>contentTypeFilter</filter-name>
<filter-class>com.infowarelab.wap.filter.SetContentTypeFilter</filter-class>
<init-param>
<param-name>contentType</param-name>
<param-value>text/vnd.wap.wml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern> //v1,v2都需要进行编码设置
</filter-mapping>
<filter-mapping>
<filter-name>contentTypeFilter</filter-name>
<url-pattern>/v1/*</url-pattern> //只有v1需要修改content type设置
</filter-mapping>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值