reset password
Author Message
cbort
Posts: 95
Posted 18:44 Apr 30, 2011 |

So I want to have it output %2B instead of +

I was trying to do it using the <c:out escapeXML="true"> but i dont want to escape the necessary brackets and quotes is there away to escape the escapeXML?

dely
Posts: 32
Posted 18:57 Apr 30, 2011 |

Doesn't escapeXML escape special characters that are used in XML and replace it with the equivalent like &quote; and others.  What you are asking for is URL encoding.  I am not sure if this goes against the concept of MVC but what if you encode it using the URL encoder in java and map your tags to the encoded equivalents.

cbort
Posts: 95
Posted 19:10 Apr 30, 2011 |

I am not sure I follow. I am trying to get the c++ tag to work...

If I convert the tag itself to c%2B%2B which is easy enough than when I print it I will have to replace the %2B with a +

you think that is a better way to go?

cbort
Posts: 95
Posted 19:18 Apr 30, 2011 |

Thanks I did it with,

  ${fn:replace(tag, "+", "%2B")}

 

should we be replacing all problematic chars? is there a better way than simply nesting that function?

cysun
Posts: 2935
Posted 20:50 Apr 30, 2011 |

<c:param> will encode url parameters. It has to be nested inside a <c:url> though. For example, the following weird-looking code will create a href a.html?tag=c%2b%2b:

<a href="<c:url value="a.html"><c:param name="tag" value="c++" /></c:url>">a</a>

Last edited by cysun at 20:51 Apr 30, 2011.
abess
Posts: 26
Posted 20:14 May 01, 2011 |

That worked perfectly, thanks prof sun. However, how do we un-param it once we're done?  I'd like to place that param back into the html code, but it only shows as "C" because the parameter is C%2B%2B or w/e...

cysun
Posts: 2935
Posted 21:14 May 01, 2011 |
abess wrote:

That worked perfectly, thanks prof sun. However, how do we un-param it once we're done?  I'd like to place that param back into the html code, but it only shows as "C" because the parameter is C%2B%2B or w/e...

If the tag is a scoped variable, you would have something like

<a href="<c:url value="ListQuestionsByTag"><c:param name="tag" value="${tag}" /></c:url>">${tag}</a>

And if it's a request parameter, you can have something like

${param.tag}

In either case it should display c++ correctly.

abess
Posts: 26
Posted 23:08 May 01, 2011 |

Right, but for instance, I use the param xml encoding, and I am redirercted to the site: http://cs3.calstatela.edu:8080/cs320stu31/index.jsp?tag=c%2B%2B.

If I were to use ${param.tag} in that jsp page, it would only show as "c" in the output rather than "c++".  Is there any way to fix that?

cysun
Posts: 2935
Posted 23:33 May 01, 2011 |
abess wrote:

Right, but for instance, I use the param xml encoding, and I am redirercted to the site: http://cs3.calstatela.edu:8080/cs320stu31/index.jsp?tag=c%2B%2B.

If I were to use ${param.tag} in that jsp page, it would only show as "c" in the output rather than "c++".  Is there any way to fix that?

Are you sure? If you go to http://cs3.calstatela.edu:8080/cs320stu31/Test.jsp?tag=c%2b%2b you can see that c++ is correctly displayed using ${param.tag}. The source code of Test.jsp is attached.

Attachments:
abess
Posts: 26
Posted 23:49 May 01, 2011 |

I forward this:

<a href="<c:url value="cs320AnswersMVC"><c:param name="tag" value="${tageach}" /></c:url>">${tageach}</a>

and then I from cs320Answers MVC, I forward it like this:

String tag = request.getParameter( "tag" );

request.getRequestDispatcher("/cs320AnswersJSP.jsp?tag=" + tag).forward(request, response);

Which shows: /cs320AnswersMVC?tag=c%2b%2b in the url bar, even though it shows the proper cs320AnswersJSP.jsp page.  However, once I change that to /cs320AnswersJSP.jsp?tag=c%2b%2b It displays the "C++" correctly.

Not sure how to fix this... seems like a forwarding error.

Last edited by abess at 23:50 May 01, 2011.
cysun
Posts: 2935
Posted 00:03 May 02, 2011 |
abess wrote:

I forward this:

<a href="<c:url value="cs320AnswersMVC"><c:param name="tag" value="${tageach}" /></c:url>">${tageach}</a>

and then I from cs320Answers MVC, I forward it like this:

String tag = request.getParameter( "tag" );

request.getRequestDispatcher("/cs320AnswersJSP.jsp?tag=" + tag).forward(request, response);

Which shows: /cs320AnswersMVC?tag=c%2b%2b in the url bar, even though it shows the proper cs320AnswersJSP.jsp page.  However, once I change that to /cs320AnswersJSP.jsp?tag=c%2b%2b It displays the "C++" correctly.

Not sure how to fix this... seems like a forwarding error.

Request parameters are decoded in servlet, so when you do tag=request.getParameter("tag"), tag is c++ instead c%2b%2b, and when you do "/cs320AnswersJSP.jsp?tag="+tag it becomes /cs320AnswersJSP.jsp?tag=c++.

The error is easy to fix - just remove the line "String tag ..." and forward to /cs320AnswersJSP.jsp instead of /cs320AnswersJSP.jsp?tag=.... Note that a forwarded request includes all its parameters, so you don't need to do getParameter() then try to attach the parameter back.

Last edited by cysun at 00:04 May 02, 2011.
abess
Posts: 26
Posted 00:07 May 02, 2011 |

Oh, perfect, thanks!