Hi all,

I am still having trouble with character encoding in servlets.

I want to convert all data from getParameter("form_param") to UTF-8

I have this servlets
-------------------------------------------------
import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException
  {

    // this line desn't work it needs servlet 2.3!
    file://req.setCharacterEncoding("UTF-8");

    res.setContentType("text/html;charset=UTF-8;");
    PrintWriter pw = res.getWriter();

    String par = req.getParameter("text");

    // What to write here to convert the par String ?? (from iso-8859-2 and
Cp1250)
    // TODO
    String convertedPar = new String(par.getBytes(),"UTF-8"); // but it
doesn't work

    pw.println("<head><meta http-equiv='Content-Type'
content='text/html;charset=UTF-8;'></head>");

    pw.println("Hi");
    pw.println("<form method=\"POST\"><textarea cols='50' rows='8'
name='text'></textarea><br><input type='Submit'></form>");
    pw.println("<hr> Parameter : " + par);
    pw.println("<hr> ConvertedParameter : " + convertedPar);

    pw.close();

/*
// this code will write my parameter to the file in good encoding, but I
need to have par string converted
// to UTF-8 before that to display it on the page

    try {
        FileOutputStream fos = new FileOutputStream("/tmp/1.1");
        Writer out = new OutputStreamWriter(fos , "UTF-8");
        out.write(par);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
*/

  }

  public void doPost (HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException
  {
   doGet(req,res);
  }
}

------------------------------------------------

Could anybody help me, what code add to this servlet to convert all
characters properly ?
(I am looking for toUTF8(String s) function)

Thanks a lot

Tomas Zeman
email: [EMAIL PROTECTED]

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to