开发者

Replace "#" in query string without changing the actual value

I have one query string in my JSP:

href='www.me-me.com/index.jsp?application=<%=
  myBean.getApplication().replace("&","\\")
%>&appId=<%=
  myBean.getAppId()
%>'>LINK  

Now I am picking up the开发者_运维问答 appID using request object in the index.jsp and using that appId I am pulling up the data from my DB to fill the page.

But I am getting a blank screen when myBean.getApplication is returning something like Application ##1, certainly because query is getting banged after encountering the "#".

I could replace "#" using the replace() method, but I do not find it reliable and good, as I am pulling that application name using the request object again in my index.jsp page, and the replacement of hashes would make it ugly. So is there a way where I can retain the "#"?

Any other out-of-the-box solution would be appreciated.


Use JSTL c:url with c:param.

<c:url var="url" value="http://www.me-me.com/index.jsp">
    <c:param name="application" value="${myBean.application}" />
    <c:param name="appId" value="${myBean.appId}" />
</c:url>

<a href="${url}">LINK</a>

No need to hassle with old fashioned scriptlets.


URL-encoding the values would be the right thing to do.

href='www.me-me.com/index.jsp?application=<%=
  URLEncoder.encode(myBean.getApplication(), "UTF-8")
%>&appId=<%=
  URLEncoder.encode(myBean.getAppId(), "UTF-8") 
%>'>LINK 

You can transport any string value as an URL parameter, but you must properly encode it or your URLs will break, just like you are experiencing it.

You'd need a

<%@ page import="java.net.*" %>

statement in your page, because that's where URLEncoder is defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜