Got this working. Mainly just the issue of the encoding that was being used for
the response from the servlet. You need to be careful with the names of the
encoding as they can differ between operating systems. My recommendation would
be to always use UTF-8. My version of your servlet and client are below.

Hope this helps

Mark


SERVLET
=======
package test.encoding;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Echo extends HttpServlet {

    public Echo() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        doPost(request, response);
    }

    public void doPost(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

        request.setCharacterEncoding("cp1255");
        
        response.setContentType("text/html; charset=windows-1255");
    
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        out.println(name);
    }

} 

CLIENT
======
package test.encoding;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Client {

    public static void main(String[] args) {
        try {   
            String host = "localhost";
            String port = "8080";

            // the unicode for "Yair" in hebrew.
            String data = "name=" +
URLEncoder.encode("\u05d9\u05d0\u05d9\u05e8", "cp1255");
            // URL test = new
URL("http://"+host+":"+port+"/Bugfix/encoding/bug14742Output.jsp";);
            URL test = new URL("http://"+host+":"+port+"/Bugfix/servlet/Echo";);
            URLConnection req = test.openConnection();
            req.setDoOutput(true);
            req.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset=windows-1255");
            req.setRequestProperty("Content-Length",
Integer.toString(data.length()));
            OutputStream out = req.getOutputStream();
            // writing the request in Cp1255 encoding.
            out.write(data.getBytes("cp1255"));
            out.close();
    
            InputStream in = req.getInputStream();
            // reading the response in Cp1255 encoding.
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            // instead of getting my name in hebrew . I get "????" four times
0xF9 in the text file in.txt
            File f = new File("in.txt");
            FileWriter fw = new FileWriter(f);
            int code;
            while ((code = br.read()) != -1) {
                System.out.println(code);
                fw.write(code);
            }
            fw.close();                   
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

> -----Original Message-----
> From: Yair Fine [mailto:[EMAIL PROTECTED] 
> Sent: Sunday, April 25, 2004 12:16 AM
> To: [EMAIL PROTECTED]
> Subject: RE: Getting a request in a non English character 
> 
> Hi Mark,
> 
> Thanks again for you help.
> In order to help you help me (-' , I will give more details on my goal
> and my problem:
> I want to build a servlet which let application clients ( not HUMAN.a
> client application, thus HTML forms will do no use here)
> Send HTTP requests to the servlet, which queries a DB 
> (Oracle), and then
> the servlet sends back a response to the client application 
> The requests must support windows-1255 charset encoding 
> (Windows Hebrew)
> 
> At first I decided to receive the request parameters , using 
> GET request
> , and I attached previously the code, which works fine , and I could
> even for debug purpose send the request in the url of IE 
> browser (though
> the client application wouldn't use a browser ,but call GET 
> request with
> the parameters desired, using windows-1255 charset encoding)
> 
> After reading your response and other forums response, which 
> agreed that
> sending a non English characters in GET request is a bad idea,
> I decided to implement it with POST. So now I have two problems 
> 1. writing the servlet.
> 2. writing a client that will send requests using POST.
> 
> Here is a sample servlet and client. It should have worked 
> but it don't
> (-: , I figure it is a small thing, but did not found it till far.
> 
> 
> THIS IS THE SERVER'S CODE:
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.IOException;
> import java.io.PrintWriter;
> public class Hebrew3test extends HttpServlet {
>       public void doPost(HttpServletRequest request,
> HttpServletResponse response)
>               throws ServletException, IOException {
>               request.setCharacterEncoding("cp1255");
>               response.setCharacterEncoding("cp1255");
>               response.setContentType("Text/html; cp1255");
>       
>               PrintWriter out = response.getWriter();
>               String name = request.getParameter("name");
>               out.println(name);
>       }
>       public void doGet(HttpServletRequest request,
> HttpServletResponse response)
>               throws ServletException, IOException {
>                       doPost(request, response);
>       }
> }
> 
> THIS IS THE CLIENT'S CODE:
> import java.io.*;
> import java.net.*;
> public class HttpClient_cp1255_2 {
>       try {   
>               String host = "127.0.0.1"; //tomcat5 host - local
> machine
>               port = "8083" //tomcat5 port
>               String data = "name=" +
> URLEncoder.encode("\u05d9\u05d0\u05d9\u05e8", "cp1255"); // 
> the unicode
> for "Yair" in hebrew.
>               URL test = new
> URL("http://"+host+":"+port+"/userprofile/hebrew3test";);
>               URLConnection req = test.openConnection();
>               req.setDoOutput(true);
>               req.setRequestProperty("Content-Type",
> "application/x-www-form-urlencoded; charset=windows-1255");
>               req.setRequestProperty("Content-Length",
> Integer.toString(data.length()));
>               OutputStream out = req.getOutputStream();
>               out.write(data.getBytes("Cp1255"));   // writing the
> request in Cp1255 encoding.
>               out.close();
>       
>               InputStream in = req.getInputStream();
>               BufferedReader br = new BufferedReader(new
> InputStreamReader(in,"cp1255")); // reading the response in Cp1255
> encoding.
>               String line;
>               File f = new File("in.txt");    // instead of getting my
> name in hebrew . I get "????" four times 0xF9 in the text file in.txt
>               FileWriter fw = new FileWriter(f);
>               while ((line = br.readLine()) != null) {
>                       System.out.println(line);
>                       fw.write(line);
>               }
>               fw.close();                                     
>     } catch (Exception e) {
>       e.printStackTrace();
>     }
>   }
> }
> 
> 
> BTW, 
> One more thing , I modified /conf/server.xml and added attribute
> URIEncoding="cp1255" useBodyEncodingForURI="true"
> 
> It still don't work
> Yair 
> 
> -----Original Message-----
> From: Mark Thomas [mailto:[EMAIL PROTECTED] 
> Sent: ג 20 אפריל 2004 23:29
> To: 'Tomcat Users List'
> Subject: RE: Getting a request in a non English character 
> 
> 
> http://jakarta.apache.org/tomcat/tomcat-5.0-doc/config/http.html
> 
> Tomcat 4 ships with the same connector but the docs aren't quite up to
> date on the web site.
> 
> Mark
> 
> > -----Original Message-----
> > From: Yair Fine [mailto:[EMAIL PROTECTED]
> > Sent: Monday, April 19, 2004 10:48 PM
> > To: 'Tomcat Users List'
> > Subject: RE: Getting a request in a non English character
> > 
> > Hi Mark,
> > Thanks for your reply ,
> > You wrote :
> > "The Coyote HTTP/1.1 connector has a useBodyEncodingForURI 
> attribute 
> > which if set to true will use the request body encoding to decode"
> > 
> > Where can I configure the useBodyEncodingForURI attribute, 
> is it in an
> > XML file ? Which one ? Thanks
> > Yair
> > 
> > 
> > 
> > 
> > 
> > -----Original Message-----
> > From: Mark Thomas [mailto:[EMAIL PROTECTED]
> > Sent: ג 20 אפריל 2004 21:19
> > To: 'Tomcat Users List'
> > Subject: RE: Getting a request in a non English character
> > 
> > 
> > You might find the text below useful. It is my standard text on
> > character encoding.
> > 
> > Mark
> > 
> > REQUESTS
> > ========
> > 
> > There are a number of situations where there may be a 
> requirement to 
> > use non-US ASCII characters in a URI. These include:
> > - Parameters in the query string
> > - Servlet paths
> > 
> > There is a standard for encoding URIs
> > (http://www.w3.org/International/O-URL-code.html) but this 
> standard is
> > not consistently followed by clients. This causes a number of 
> > problems.
> > 
> > The functionality provided by Tomcat (4 and 5) to handle this less 
> > than ideal situation is described below.
> > 
> > 1. The Coyote HTTP/1.1 connector has a 
> useBodyEncodingForURI attribute
> > which if set to true will use the request body encoding to 
> decode the 
> > URI query parameters.
> >   - The default value is true for TC4 (breaks spec but gives
> > consistent
> > behaviour across TC4 versions)
> >   - The default value is false for TC5 (spec compliant but 
> > there may be
> > migration issues for some apps) 
> > 2. The Coyote HTTP/1.1 connector has a URIEncoding attribute which
> > defaults to ISO-8859-1. 3. The parameters class
> > (o.a.t.u.http.Parameters) has a QueryStringEncoding field 
> > which defaults
> > to the URIEncoding. It must be set before the parameters 
> are parsed to
> > have an effect.
> > 
> > Things to note regarding the servlet API:
> > 1. HttpServletRequest.setCharacterEncoding() normally only 
> applies to
> > the request body NOT the URI. 2. 
> HttpServletRequest.getPathInfo() is 
> > decoded by the web container. 3. 
> HttpServletRequest.getRequestURI() is
> 
> > not decoded by container.
> > 
> > Other tips:
> > 1. Use POST with forms to return parameters as the 
> parameters are then
> > part of the request body.
> > 
> > 
> > RESPONSES
> > =========
> > 
> > HTML META
> >  tags are ignored by Tomcat. You may use <%@ page 
> pagEncoding="..." %>
> > for JSPs.
> > 
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> > 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to