3

Help fix the encoding in the servlet, it does not display Russian characters in the output.I will be very grateful for answers.

That is servlet code

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static List<String> getFileNames(File directory, String extension) {

        List<String> list = new ArrayList<String>();
        File[] total = directory.listFiles();
        for (File file : total) {
            if (file.getName().endsWith(extension)) {
                list.add(file.getName());
            }
            if (file.isDirectory()) {
                List<String> tempList = getFileNames(file, extension);
                list.addAll(tempList);          
            }
        }
        return list;
    }



@SuppressWarnings("resource")
        protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException{ 
                request.setCharacterEncoding("utf8");
                response.setContentType("text/html; charset=UTF-8");
                String myName = request.getParameter("text");

                List<String> files = getFileNames(new File("C:\\Users\\vany\\Desktop\\test"), "txt");
                for (String string : files) {
                if (myName.equals(string)) {
                       try {
                            File file = new File("C:\\Users\\vany\\Desktop\\test\\" + string);
                            FileReader reader = new FileReader(file);
                            int b;
                            PrintWriter writer = response.getWriter();
                            writer.print("<html>");
                            writer.print("<head>");
                            writer.print("<title>HelloWorld</title>");
                            writer.print("<body>");
                            writer.write("<div>");
                            while((b = reader.read()) != -1) {
                                writer.write((char) b);
                            }
                            writer.write("</div>");
                            writer.print("</body>");
                            writer.print("</html>");

                        } 
                       catch (Exception ex) {

                        }
                    }

                }
               }
        }

Here is what I have displayed instead of letters

 ршншнщ олрршшш ошгншщ шгшг ороргргр Это хрень работает ура

Eric Scot
  • 197
  • 8

2 Answers2

3

You're setting the character encoding on the request instead of the response. Change request.setCharacterEncoding("utf8"); to response.setCharacterEncoding("UTF-8");

Also: if the default character encoding of your system isn't UTF-8, you should explicitly set the encoding when reading from the file. To do that, you'd need to use an FileInputStream

Chris
  • 22,923
  • 4
  • 56
  • 50
  • п»ї ршншнщ олрршшш ошгншщ шгшг РѕСЂРѕСЂРіСЂРіСЂ Это хрень работает СѓСЂР° – Eric Scot Nov 07 '12 at 17:19
  • Can you help make it, I'm just a novice in java – Eric Scot Nov 07 '12 at 17:28
  • would be very much appreciated – Eric Scot Nov 07 '12 at 17:28
  • If someone can help not difficult to do so, which is above described @Chris – Eric Scot Nov 07 '12 at 17:34
  • @EricScot: that's not how it works. You don't simply repeat how much you want it. You try to do it on your own *and* show us what you tried and what you've read. Simply nagging others to do your work like this is not productive. – Joachim Sauer Nov 16 '12 at 14:13
  • @Chris At response.setCharacterEncoding("UFT-8"); u have spell error it is "UTF-8" – Borislav Nikolaev Apr 26 '20 at 22:54
0
pRes.setContentType("text/html; charset=UTF-8");
    PrintWriter out = new PrintWriter(new OutputStreamWriter(pRes.getOutputStream(), "UTF8"), true);

use this one i got the exact result :)