----------------------------------------------------------------
BEFORE YOU POST, search the faq at <http://java.apache.org/faq/>
WHEN YOU POST, include all relevant version numbers, log files,
and configuration files.  Don't make us guess your problem!!!
----------------------------------------------------------------

I've encounterer this problem several times......

there are 2 parts of the problem you have to solve
1) how to get correctly data posted from forms to java strings (req.getParameter()
does not return correct strings). As Martin Kuba said, the browsers do not use the
charset property in the form tag but encode according to the encoding of the web
page
2) how to write correctly data back to the user.

the first one is solved like this
a) you know the charset of the html page which had the form embedded. Lets suppose
this was "windows-1253" (greek)
b)    to get a correct java string you have to do the following
        String wrongString = req.getParameter("paramName");
        byte b[] = wrongString.getBytes("windows-1252");        // or iso-8859-1
        String correctString = new String(b,"windows-1253");

the second problem has to do with the object you use to write data back to the
user.... if you use a PrintWriter you have the problem that the printwriter uses
internally an OutputStreamWriter which is constructed with default charset... if
this default is not what you want to have, then you have bad luck!!! What you can
do is write a subclassed PrintWriter which takes as paramater an encoding and use
this as your printwriter. For example I use the following class

package msc.io;

import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;


// provides the ability to specify the encoding used to print out text (either
UTF-8 or codepage)
public class MscPrintWriter extends PrintWriter
{
    public MscPrintWriter()  throws UnsupportedEncodingException {
        this(null,null);
    }

    public MscPrintWriter(OutputStream out, String encoding) throws
UnsupportedEncodingException {
        super(new BufferedWriter(new OutputStreamWriter(out,encoding)), false);
    }
}


To use this with a servlet you have to do the following
        String encoding="some encoding";        // e.g. windows-1253, UTF-8....
        resp.setContentType( "text/html; charset=" + encoding );
        MscPrintWriter out = new MscPrintWriter(resp.getOutputStream(),encoding );


Hope this helps

Stefanos
maciek kaminski wrote:

>
> Hello,
>
> Have you used jserv with charset other than ISO-8859-1?
> I can't find the way to make browser send correct charset information to jserv.
> Setting <form ... enctype="application/x-www-form-urlencoded; charset=Cp1250">
> does not help.
>
> Do I miss something or is it browsers fault?
>
> Maciek Kaminski                  [EMAIL PROTECTED]
>
>

--
======================================================================
Stefanos Karasavvidis
Electronics & Computer Engineer
e-mail : [EMAIL PROTECTED]

Multimedia Systems Center S.A.
Kissamou 178
73100 Chania - Crete - Hellas
http://www.multimedia-sa.gr

Tel : +30 821 88447
Fax : +30 821 88427




--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html>
Problems?:           [EMAIL PROTECTED]

Reply via email to