1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<!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">
    </head>
    <body>
        <form action="index.jsp" >
            <input type="text" name="txt">
            <input type="submit" name="btn" value="Enter">
        </form>
        <%if (request.getParameter("txt")!=null) out.print(request.getParameter("txt")); %>
    </body>
</html>

When load this page and enter a latin word like Sasan, then it redisplays Sasan which is OK. But when enter a Persian word like ساسان, then it redisplays Ø³ÙØ§Ù instead of ساسان.

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

0

Your form is sending a GET request. The entire request URI including the GET query string is processed by the servletcontainer. You need to set the request URI encoding in the servletcontainer configuration. It's unclear which one you're using, but in case of for example Tomcat, you need to open /conf/server.xml, locate the <Connector> element and add URIEncoding attribute to it with a value of UTF-8.

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

Other servletcontainers have their own configuration setting for this.

Note that when you are sending a POST request instead, then the entire request body is processed by the servlet API instead. You need to set the request body encoding by ServletRequest#setCharacterEncoding() before you ever get the first parameter.

request.setCharacterEncoding("UTF-8");

The common practice is to perform this job in a Filter.

I'd also change your JSP pageEncoding from utf-8 to UTF-8, just to be consistent.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

I faced this issue before. A simple way I'm using for displaying Farsi characters in JSP web pages correctly while browsing is:

Step 1: Save the file in Eclipse IDE You may see this warning during saving: (see the following image)

http://screenshot.net/k203c39

So to save the file, you have to select "Save as UTF-8"

Step 2: Open the page file with Notepad editor and then click "Save As...": (see the following image)

http://screenshot.net/71gkto1

And then select "UTF-8" as Encoding of the file: (see the following image)

http://screenshot.net/zw83uq0

And then click "Save" button to save the file. Now you should browse JSP page correctly with Farsi characters showing in the correct format.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mohsen Abasi
  • 2,050
  • 28
  • 30