Expand my Community achievements bar.

SOLVED

Form data charset problem

Avatar

Level 4

I have problem with language specific symbols in my contact form. I have form, that leads to my servlet which sends email but in the time that the servlet is ran, the characters are already bad. For example if I enter this "ěščřžýáíé" to the form and submit it, the characters that the servlet gets are "Ä�Å¡Ä�Å�žýáíé".

Here is servlet code

public class ServletMailer extends SlingAllMethodsServlet { @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { log.info("ServletMailer v0.0.15-SNAPSHOT"); this.setParams(request); this.validate(request, response); this.sendEmail(request); } private void sendEmail(SlingHttpServletRequest request){ log.info("GOING TO SEND EMAIL(ServletMailer)"); ArrayList<InternetAddress> emailRecipients = new ArrayList<InternetAddress>(); MessageGatewayService messageGatewayService = Activator.getMessageGatewayService(); MessageGateway<HtmlEmail> messageGateway = messageGatewayService.getGateway(HtmlEmail.class); log.info(TEMPLATE_PATH+TEMPLATE); org.apache.sling.api.resource.Resource templateRsrc = request.getResourceResolver().getResource(TEMPLATE_PATH+TEMPLATE); try{ if (templateRsrc.getChild("file") != null) { templateRsrc = templateRsrc.getChild("file"); } if (templateRsrc == null) { log.error("Missing template: " + TEMPLATE); return; } }catch(Exception e){ log.error("ServletMailer::sendMail exception: "+e.getMessage()); } try{ MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), templateRsrc.getResourceResolver().adaptTo(Session.class)); HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(prefs), HtmlEmail.class); email.addTo(RECIPIENT); email.setCharset("UTF-8"); messageGateway.send(email); }catch(Exception e){ log.error("Error creating MailTemplate, HtmlEmail or sending mail."); } } private void validate(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException{ if (prefs.get(NAME).equals("") || prefs.get(SURNAME).equals("")) { sendError("missingName",request,response); } } private void setParams(SlingHttpServletRequest request){ prefs.put(NAME, StringEscapeUtils.escapeHtml(request.getParameter(NAME))); prefs.put(SURNAME, StringEscapeUtils.escapeHtml(request.getParameter(SURNAME))); prefs.put(EMAIL, StringEscapeUtils.escapeHtml(request.getParameter(EMAIL))); prefs.put(QUESTION, StringEscapeUtils.escapeHtml(request.getParameter(QUESTION))); log.info("QUESTION:"); log.info("ěščřžýáíé");   //this logs OK log.info(request.getParameter(QUESTION));    //this is malformed log.info(StringEscapeUtils.escapeHtml(request.getParameter(QUESTION))); } }

and this is my template

From: ${email} Subject: Contact Form Submission We have received a contact request with the information below: <br/> Email: ${email} <br/> Name: ${name} ${surname} <br/> ěščřžýáíé   //this is OK <br/> Message: ${question}    //here it is malformed

The charset I use in the page with form is UTF-8

Thanks for any help.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi.
These are some things that helped me:

1) Have you configured the OSGi Console -> Configuration -> Apache Sling Main Servlet -> Default Parameter Encoding  to UTF-8 `

2) Have you set the page encoding/content to UTF-8 on the page ?

3) Have you se the correct endoing type on the form ?
<form action="FORM_PATH_IN_SOME_WAY" enctype="multipart/form-data; UTF-8">
...
</form>


4) Also make sue that the servlet, the first thing it does, is to set the right encoding for the request/response. That was something i learned.
In my case i did some things first and then set the encoding like so: response.setCharacterEncoding("UTF-8"); but this has to be done before anything else.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 7

Hi.
These are some things that helped me:

1) Have you configured the OSGi Console -> Configuration -> Apache Sling Main Servlet -> Default Parameter Encoding  to UTF-8 `

2) Have you set the page encoding/content to UTF-8 on the page ?

3) Have you se the correct endoing type on the form ?
<form action="FORM_PATH_IN_SOME_WAY" enctype="multipart/form-data; UTF-8">
...
</form>


4) Also make sue that the servlet, the first thing it does, is to set the right encoding for the request/response. That was something i learned.
In my case i did some things first and then set the encoding like so: response.setCharacterEncoding("UTF-8"); but this has to be done before anything else.

Avatar

Level 4

Ojjis wrote...

Hi.
These are some things that helped me:

1) Have you configured the OSGi Console -> Configuration -> Apache Sling Main Servlet -> Default Parameter Encoding  to UTF-8 `

2) Have you set the page encoding/content to UTF-8 on the page ?

3) Have you se the correct endoing type on the form ?
<form action="FORM_PATH_IN_SOME_WAY" enctype="multipart/form-data; UTF-8">
...
</form>


4) Also make sue that the servlet, the first thing it does, is to set the right encoding for the request/response. That was something i learned.
In my case i did some things first and then set the encoding like so: response.setCharacterEncoding("UTF-8"); but this has to be done before anything else.

 

Thank you. The first one was the right solution.