如何在jsp中用uri对字符串进行编码? [英] How to uri encode a string in jsp?

查看:163
本文介绍了如何在jsp中用uri对字符串进行编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个等于网址的字符串输出":

if I have a string "output" that equals a url:

${output} = "/testing/method/thing.do?foo=testing&bar=foo"

在jsp中,如何将字符串转换为:

in the jsp, how would I convert that string into:

%2Ftesting%2Fmethod%2Fthing.do%3Ffoo%3Dtesting%26bar%3Dfoo

使用

<c:out value="${output}"/>

? 我需要以某种方式在c:out中输入URLEncoder.encode(url).

? I need to URLEncoder.encode(url) in the c:out somehow.

推荐答案

使用标准JSTL标签/功能无法直接实现.这是<c:url>的帮助:

It's not directly possible with standard JSTL tags/functions. Here's a hack with help of <c:url>:

<c:url var="url" value=""><c:param name="output" value="${output}" /></c:url>
<c:set var="url" value="${fn:substringAfter(url, '=')}" />
<p>URL-encoded component: ${url}</p>

如果您想做得更整洁,请创建一个EL函数.在此答案的底部,您可以找到一个基本的启动示例.您想以以下形式结尾:

If you want to do it more cleanly, create an EL function. At the bottom of this answer you can find a basic kickoff example. You'd like to end up as:

<p>URL-encoded component: ${my:urlEncode(output, 'UTF-8')}</p>

使用

public static String urlEncode(String value, String charset) throws UnsupportedEncodingException {
    return URLEncoder.encode(value, charset);
}

这篇关于如何在jsp中用uri对字符串进行编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆