In JSTL, the <fmt:requestEncoding> tag is used to specify the character encoding of the request. The character encoding specified using this tag is used to decode the incoming forms data entered by the user. We must use this tag to set the character encoding if the encoding used is different from the ISO-8859-1.

Even if the contentType is defined in the JSP page directive by the programmer, the character encoding must be specified because the actual user’s locale may be different from the defined value in the page directive.

 

JSTL <fmt:requestEncoding> Syntax:

<fmt:requestEncoding value="<string>"/>

 

Attributes:

Name

Required

Type

Description

    

value

False

java.lang.String

The character encoding to be used when decoding the request parameters.

 

JSTL <fmt:requestEncoding> Example:

The following JSP code sets the character encoding to UTF-8 and displayed localized messages.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;fmt:requestEncoding&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;fmt:requestEncoding&gt; Demo</h1>
    <fmt:requestEncoding value="UTF-8" />
    <fmt:setLocale value="fr_CA"/>
    <fmt:bundle basename="net.codejava.jstl.messages">
       <fmt:message key="count.one"/><br/>
       <fmt:message key="count.two"/><br/>
       <fmt:message key="count.three"/><br/>
    </fmt:bundle>
  </body>
</html>

Output

requestencoding

 

Recommended Usage of JSTL <fmt:requestEncoding> tag:

We use <fmt:requestEncoding> to specify the character encoding to be used when decoding the incoming form data. This is particularly useful in dealing with languages like Chinese or any other Asian languages where user input comes in those languages.

 

Other JSTL Format Tags:

bundle  |  formatDate  |  formatNumber  |  message  |  param  |  parseDate  |  parseNumber  |  setBundle  |  setLocale  |  setTimeZone  |  timeZone


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment