1

I, the following servlet code does not display the characters, place them, he says something like this:  ршншнщ олрршш. Could you help fix it, I will be very grateful, I beginner in java so you can please send me the code to encoded everything was fine, it is advised to use:

.getBytes("UTF-8");

Here's the 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) {

                        }
                    }

                }
               }
        }

all I solved the problem, close all the giant question thanks.Special thanks to @BalusC put him pluses)

Eric Scot
  • 197
  • 8
  • Sorry for the design if that is not the case – Eric Scot Nov 09 '12 at 13:06
  • Java programmers for you it's probably a no-brainer, but for me this is a beginner so hard and I ask to write the code, please – Eric Scot Nov 09 '12 at 13:08
  • Is that supposed to be `ршншнщ олрршш` – Esailija Nov 09 '12 at 13:13
  • Then it means UTF-8 being misinterpreted as Windows-1251. Check in your browser that the server is sending header properly. In google chrome developer tools, check the headers from network tab. – Esailija Nov 09 '12 at 13:17
  • I have no need to address this issue is the software is not sure that anybody will change the coding, so please help me with the code – Eric Scot Nov 09 '12 at 13:19
  • what a strange name for russian, Eric Scot. And answer is simple - replace `request.setCharacterEncoding("utf8");` to `response.setCharacterEncoding("utf8");` – user1516873 Nov 09 '12 at 13:22
  • Yes, I have a name I just came from Russia but it is not live, I tried too it is impossible to replace – Eric Scot Nov 09 '12 at 13:24
  • again displays the stupid squiggles – Eric Scot Nov 09 '12 at 13:25
  • @EricScot well I am trying to help you, right now you are just going in blind. You need to see what headers the browser sees to debug this. – Esailija Nov 09 '12 at 13:27
  • I unfortunately do not know how to do this can you just give a bit of code please – Eric Scot Nov 09 '12 at 13:29
  • You don't need any code to see what headers the browser sees, just open up developer tools in google chrome, go to network tab, start recording, load up your page and see what `Content-Type` header is sent. – Esailija Nov 09 '12 at 13:30
  • The network does not write, but I have seen the following: Chrome-ekstension :/ / llfbts ..... – Eric Scot Nov 09 '12 at 13:41
  • That's what passes: content-type text / html; charset = utf-8 – Eric Scot Nov 09 '12 at 13:44
  • Eric, if you use chrome, please check what encoding using browser to show content. Hit your servlet, press wrench button, then tools->encoding and in this menu what radiobutton is checked? – user1516873 Nov 09 '12 at 13:57
  • all I solved the problem, close all the giant question thanks – Eric Scot Nov 09 '12 at 14:03

1 Answers1

2

This problem is two-fold.

First, you forgot to set the response encoding. This way the response is written with server platform default encoding. Add the following line before writing any byte/character to the response.

response.setCharacterEncoding(StandardCharsets.UTF_8.name());

Second, you're reading the file using server platform default encoding.

Reader reader = new FileReader(file);

You should be reading the file using an explicitly specified encoding matching the encoding actually used by the text file itself. This can be done with help of InputStreamReader.

Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);

See also:


Unrelated to the concrete problem, HTML code doesn't belong in a servlet. It belongs in a JSP. Continue here to learn how to deal with it: Generate an HTML Response in a Java Servlet.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you, and where do I enter it? – Eric Scot Nov 09 '12 at 13:31
  • I believe it's just the content-type header being sent, `п»ї ршншнщ олрршш` are already the correct bytes but just misinterpreted. – Esailija Nov 09 '12 at 13:33
  • @Esailija: the content type header doesn't tell the server what encoding to use to write the response. It tells the client what encoding to use to read the response. But if the server itself didn't write the response in UTF-8 at all, then the client would of course misinterpret it, which is exactly what is happening here. The server should have been told to write the response in UTF-8. The content type header should of course be kept there, I have nowhere said to remove it. I recommend to read the "See also" link to understand the stuff better. – BalusC Nov 09 '12 at 13:35
  • @BalusC the server is writing the response `п»ї ршншнщ олрршш`, in other words, the raw bytes in the response are `EF BB BF 20 D1 80 D1 88 D0 BD D1 88 D0 BD D1 89 20 D0 BE D0 BB D1 80 D1 80 D1 88 D1 88`. When these are interpreted in UTF-8, one will see `ршншнщ олрршш` in the browser. When they are interpreted in windows-1251, one sees `п»ї ршншнщ олрршш`. So the browser was interpreting them in windows-1251, so the content-type header must have been wrong and setting it to utf-8 is enough to fix it. – Esailija Nov 09 '12 at 13:38
  • @Esailija: Those garbled characters are in first place not caused by wrong response encoding, but by wrong `FileReader` encoding. It is reading an UTF-8 file using the platform default encoding (which in turn is also written to the response using the platform default encoding). If the `FileReader` encoding was been fixed, but the response encoding not, then you would have seen the problem the other way round. – BalusC Nov 09 '12 at 13:40
  • @SuppressWarnings("resource") ... String myName = request.getParameter("text"); myName.getBytes("UTF-8"); .... for (String string : files) { if (myName.equals(string)) { FileReader reader = null; try { File file = new File("C:\\Users\\ваня\\Desktop\\asksearch\\" + string ); reader = (FileReader) new InputStreamReader(new FileInputStream(file), "UTF-8"); – Eric Scot Nov 09 '12 at 13:56