How to specify the encoding type used by forms that post data back to the Web application in a JSP?


The <fmt:requestEncoding> tag is used to specify the encoding type used by forms that post data back to the Web application.

Attribute

The <fmt:requestEncoding> tag has the following attributes −

AttributeDescriptionRequiredDefault
keyName of character encoding you want to apply when decoding request parameters.YesNone

You use the <fmt:requestEncoding> tag when you want to specify the character encoding for decoding data posted from forms. This tag must be used with character encodings that are different from ISO-8859-1. The tag is required since most browsers do not include a Content-Type header in their requests.

The purpose of the <fmt:requestEncoding> tag is to specify the content type of the request. You must specify the content type, even if the encoding of the page generating the response is specified via the contentType attribute of a page directive. This is because the response's actual locale (and thus character encoding) may differ from the value specified in the page directive.

If the page contains an I18N-capable formatting action that sets the response's locale (and thus character encoding) by calling ServletResponse.setLocale(), any encoding specified in the page directive will be overridden.

Example

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<html>
   <head>
      <title>JSTL fmt:message Tag</title>
   </head>
   <body>
      <fmt:requestEncoding value = "UTF-8" />
      <fmt:setLocale value = "es_ES"/>
      <fmt:setBundle basename = "com.tutorialspoint.Example" var = "lang"/>
      <fmt:message key = "count.one" bundle = "${lang}"/><br/>
      <fmt:message key = "count.two" bundle = "${lang}"/><br/>
      <fmt:message key = "count.three" bundle = "${lang}"/><br/>
   </body>
</html>

You will receive the following result −

Uno
Dos
Tres

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements