0
votes

Here is my problem, can someone please help me....!

Present, i am migrating an application from struts to JSF2. We have a user validation link, which sent to their email for verification, after their successful registration. For example:

http://testserv:5050/bjb/page/validateAccount.faces?Email=qwerty@testsession#1.com&Code=1272

The email address contains special characters, in old links which were sent to users. In new application we are encoding them using URLEncoder.encode. i think, this is nothing to do with UTF-8, But however i set request, response, and JSF ExternalContext#setResponseCharacterEncoding to UTF-8. tomcat is also configured for UTF-8.

validateAccount.xhtml

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</h:head>
<h:body>
<h:form acceptcharset="UTF-8" name="verifyUserAccountForm" id="verifyUserAccountForm" prependId="false">
        <f:metadata>
            <f:event type="preRenderView" listener="#{verifyUserBean.execute}"></f:event>
        </f:metadata>
    </h:form>
</h:body>
</html>

In java code, FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(paramName); Could not read the URL GET Request Parameters properly.

How should i solve this for existing links. I have something in my mind like, i can write a javascript function in my xhtml page, Which could be excuted on load, so using javascript function i can encode parameters. But how to execute that javascript function before <f:event type="preRenderView"> call.

is there any better solution ?.

1

1 Answers

0
votes

The query string parameter in the outgoing URL should be encoded as follows wherein the desired charset is explicitly mentioned:

String emailParam = URLEncoder.encode(email, "UTF-8");
String url = "http://testserv:5050/bjb/page/validateAccount.faces?Email=" + emailParam + "&Code=1272";

The Tomcat server should be confidured to decode the incoming URL using the very same charset:

<Context ... URIEncoding="UTF-8">

That's all you need to do with regard to encoding/decoding GET URLs. The HTML meta tag is irrelevant to this. The HTTP response encoding is irrelevant to this. The HTML form accept charset does more worse than good.

Please note that you edit the <Context> element in the right Tomcat configuration file. If you're for example managing Tomcat in Eclipse, you'd by default need to edit the one in Eclipse's Servers project, not in Tomcat's installation folder.

See also