Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:27:19+00:00 2026-06-14T02:27:19+00:00

I, the following servlet code does not display the characters, place them, he says

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:27:20+00:00Added an answer on June 14, 2026 at 2:27 am

    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:

    • Unicode – How to get the characters right?

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my servlet I have the following code: response.setContentType(application/json);//set json content type PrintWriter out
I'm writing my Servlet application and would like to use the following static method
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
There's a piece of code that looks like this. The problem is that during
I have a servlet which does some error checking and if something is wrong
I have the following servlet code public void doPost(HttpServletRequest request, HttpServletResponse response){ Backup bup
So I wanted to do something like this: @Component @Scope(value=request, proxyMode=ScopedProxyMode.INTERFACES) public class MyBean
As mentioned below I have made changes to the code which looks like following
We have some code on production which is effectively doing what this code does:
Why does config.getInitParameter(String) always return null in the following code example? public void init(ServletConfig

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.