In this example you will learn about the JSTL
JSTL fmt Tag requestEncoding Example
In this example you will learn about the JSTL <fmt:requestEncoding> tag.
<requestEncoding> tag encodes the text into the specified request character encoding. This tag is required to use for specifying the content type because, the content-type header is not included by most of the browsers in their requests. However, the contentType is defined in the page directive it must be defined by the programmer because it may happens that the actual locale is different from the defined value in the page directive.
Attributes of <fmt:requestEncoding>
- value : This attribute is used for specifying the name of character encoding that has to be implemented when decoding request parameters.
Example :
Here an example is being given which will demonstrate you about the use of <fmt:requestEncoding> tag. In this example I have created two ListResourceBundle subclasses. Both classes contains the key-value pair. In Example_en_US.java for the specified key the value is in English language and in Example_fr_FR.java for the same key as of the Example_en_US.java, the value is in French language. Then created a html page into which I have used the <select> tag to choose one of the language into which locale the user want to see. And next created a jsp page where I have used the <fmt:requestEncoding> tag to specify the character encoding type and uses the <fmt:setLocale> tag to set the Locale and <fmt:message> tag to map the key which will return the respective value for the each locale. So as an output the respective locale that you have selected in the html page will be displayed.
Example_en_US.java
package pack; import java.util.ListResourceBundle; public class Example_en_US extends ListResourceBundle { public Object[][] getContents() { return englishLanguage; } static final Object[][] englishLanguage = { {"Name", "a"}, {"Address", "i"}, {"Number", "996677554"}, }; }
Example_fr_FR.java
package pack; import java.util.ListResourceBundle; public class Example_fr_FR extends ListResourceBundle { public Object[][] getContents() { return frenchLanguage; } static final Object[][] frenchLanguage = { {"Name", "un"}, {"Address", "je"}, {"Number", "996677554"}, }; }
selectLanguage.html
<!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=UTF-8"> <title>Select Language</title> </head> <body> <form method="get" action="JstlFmtRequestEncoding.jsp"> <select name="choose"> <option>en_US</option> <option>fr_FR</option> </select> <input type="submit" value="submit"/> </form> </body> </html>
JstlFmtRequestEncoding.jsp
<%@ page language="java" contentType="text/html;" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="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;"> <title>JSTL fmt:requestEncoding Tag</title> </head> <body> <fmt:requestEncoding value="UTF-8"/> <% String lang= request.getParameter("choose"); if(lang.equals("en_US")) { %> <b>English Locale</b><br> <%-- <fmt:setLocale value="en_US"/> --%> <fmt:bundle basename="pack.Example"> <fmt:message key="Name"/><br/> <fmt:message key="Address"/><br/> <fmt:message key="Number"/><br/> </fmt:bundle> <%} else { %> <b>French Locale</b><br> <fmt:setLocale value="fr_FR"/> <fmt:bundle basename="pack.Example"> <fmt:message key="Name"/><br/> <fmt:message key="Address"/><br/> <fmt:message key="Number"/><br/> </fmt:bundle> <%} %> </body> </html>
How to run this example
Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :
- jstl.jar
- standard.jar
After adding of these jar files you may execute your program in the following ways :
- Select selectLanguage.html file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- On the Eclipse Editor go to your selectLanguage.html -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
- A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish
NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.
Output :
When the execution process will be completed successfully an output will be displayed on your eclipse browser as :
1. When you will execute the selectLanguage.html file a page will be opened to you for selecting the language code as :
2. After selecting the language code the specified locale will be opened to you as :
[ 0 ] Comments