Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Character encoding problem in JSP textarea

843842Apr 27 2010 — edited Apr 28 2010
Hello,

I have a problem with character encoding. I'm using servlet to read file and write its content to jsp textarea.

This is my read file method:
    public void readFilesText(String inPutFile){
        getTextBetweenBody().clear();
        BufferedReader in1 = null;
        String eil = "";
        try{
            File file = new File(inPutFile);
            in1 = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            while ((eil = in1.readLine()) != null){
                getTextBetweenBody().add(eil);
            }
            in1.close();
        } catch (Exception e){
            log.error(e);
        }
    }
When read file and write file's content to textarea, it is ok. But when I write that content from textarea to another file, my symbols are encoded in UTF-8. But they look like spesific UTF-8 simbols.

There are some words that written from textarea to the file:
Nežinomas
sutartį
teisės į viltį
There are same words that suposed to be written to the file like below. It is lithuanian symbols.
Nežinomas
sutartį
teisė į viltį
Here is my writing to the file servlet:
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        realPath = getServletContext().getRealPath(EDITED_FILES_DIR);

        StringBuffer text = new StringBuffer(request.getParameter("iterpimas"));
        String homeDir = realPath + "/Edited.xml";
        BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(homeDir), "UTF-8"));

        br.write(text.toString());
        br.flush();
        br.close();

        request.setAttribute("HomeDir", homeDir);

        RequestDispatcher rd = request.getRequestDispatcher("/inserted.jsp");
        rd.forward(request, response);
    }
And this is jsp textarea code:
            <form name="form" action="XMLInserter.srv" method="post">
                <div id="content">
                    <textarea spellcheck="false" class="textarea2" name="iterpimas" cols=140 rows=20><%for (String text : rf.getTextBetweenBody()){out.println(text);}%></textarea>
                    <input type="submit" name="pakeitimai" class="iterpti" value="Atlikti pakeitimus">
                </div>
            </form>
And a question: How can write a text from textarea to the file without this UTF-8 spesificly encoded symbols?

Any help would be very appreciate.

Edited by: peliukasss on Apr 28, 2010 12:15 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 26 2010
Added on Apr 27 2010
1 comment
1,150 views