You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Richard Sand <rs...@vgalleries.com> on 2002/02/22 09:52:05 UTC

CkassFormatError when calling getBytes? (was Re: still suffering character-encoding woes)

Hi all,

I have a simple servlet that at some point retrieves a parameter from the
request.  In a (thusfar futile) effort to get western europe characters to
work, I tried playing with getBytes to convert the encoding.  At the
suggestion of multiple people on the list, I added the following to my code:

   String paramValue = request.getParameter(paramName);
   if (paramValue != null) {
     try {
       byte[] bytes = paramValue.getBytes("ISO-8859-1");
       paramValue = new String(bytes, "ISO-8859-1");
     }
     catch (java.io.UnsupportedEncodingException uee) {
       throw new com.ttg.ParameterNotFoundException("Error encoding " +
parameterName + ": " + uee.toString());
     }
  }

I've also tried the above with "UTF-8".

But the simple act of putting this code in causes my servlet context to fail
to load!  I cannot understand it.  I've actually gotten two different
exception message as I've played around with this, either:

java.lang.ClassFormatError: com/ttg/TtgServlet (Illegal UTF8 string in
constant pool)

or

java.lang.ClassFormatError: com/ttg/TtgServlet (Illegal constant pool type)

Can anyone help??

Thanks!

-Richard


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: changing the default character encoding

Posted by Richard Sand <rs...@vgalleries.com>.
Hi Markus,

No, that's not it.  I see my servlet responses fine.  The issue is that when
my servlets do logging, i.e. they open an output stream and write to it, or
when they update my database, the data that gets written is us-ascii.

When I receive a parameter in a JSP page, i.e.

<% String param = request.getParameter("someparam"); %>

and then later write the param to the output, such as

You entered: <%=param%>

I correctly see what I entered, including western europe characters.

But when I write the string to a log file or a JDBC connection from within
the page, it comes out as us-ascii with '?' for the characters.  The reason
this happens is that the *default* encoding is us-ascii instead of
iso-8859-1.

So again, according to the package description for java.lang, the JVM
determines the default encoding by looking at the OS environment.  On my
development machine (win2k), all of my code functions properly because the
machine, the environment is set to a european locale.  So my question is:

What settings are necessary (shell environment, others?) on Linux to get the
JVM (sun jdk131) to use iso8859-1 as default instead of us-ascii??

Thanks for your help!

Best regards,

Richard

----- Original Message -----
From: "Markus Spath" <ms...@arcor.de>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Monday, February 25, 2002 6:30 AM
Subject: Re: changing the default character encoding


> Richard,
>
> Richard Sand wrote:
>
> > Hi all-  Does anyone know how to specify the default encoding for the
JVM in
> > a linux environment?
> >
> > I'm having difficulty with my servlets because the JVM on my system (sun
> > jdk1.3.1 on linux) is defaulting to us-ascii encoding instead of using
> > iso-8859-1.  I know I can override the encoding inside my code, but
> > according to the package description for java.lang, the JVM determines
the
> > default encoding by looking at the OS.  I tried setting both LANG and
> > LC_CTYPE in my shell before launching catalina.sh, but I still couldn't
get
> > the servlets to not default to US-ASCII.
>
>
> i guess you want to set the encoding of your servlets response.
>
> you can do this, calling the setContentType() method of the
> (Http)ServletResponse object - before getting a Writer or Stream.
>
> ...
> public void doGet(HttpServletRequest request, HttpServletResponse
response) {
>
>      response.setContentType("text/html; charset=ISO-8859-1");
>
>      PrintWriter blabla = response.getWriter();
>      ....
> }
>
> regards,
> Markus
>
>
>
>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: changing the default character encoding

Posted by Markus Spath <ms...@arcor.de>.
Richard,

Richard Sand wrote:

> Hi all-  Does anyone know how to specify the default encoding for the JVM in
> a linux environment?
> 
> I'm having difficulty with my servlets because the JVM on my system (sun
> jdk1.3.1 on linux) is defaulting to us-ascii encoding instead of using
> iso-8859-1.  I know I can override the encoding inside my code, but
> according to the package description for java.lang, the JVM determines the
> default encoding by looking at the OS.  I tried setting both LANG and
> LC_CTYPE in my shell before launching catalina.sh, but I still couldn't get
> the servlets to not default to US-ASCII.


i guess you want to set the encoding of your servlets response.

you can do this, calling the setContentType() method of the 
(Http)ServletResponse object - before getting a Writer or Stream.

...
public void doGet(HttpServletRequest request, HttpServletResponse response) {

     response.setContentType("text/html; charset=ISO-8859-1");

     PrintWriter blabla = response.getWriter();
     ....
}

regards,
Markus





--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: changing the default character encoding

Posted by Joel Rees <jo...@alpsgiken.gr.jp>.
Java news groups archives can be accessed at

http://groups.google.com/groups?group=comp.lang.java

but see below:

Richard Sand asked:

> Hi all,
>
> I am still beating my head against a brick wall trying to get the default
> encoding changed for my Tomcat server on Linux (slackware 2.4.17 if it
> matters).
>
> The problem is actually outside of Tomcat so please excuse the slightly
> off-topic question.  But basically, I've run the following simple test:
>
> import java.io.InputStreamReader;
> import java.io.ByteArrayInputStream;
> public class Encoding {
>   public static void main(String[] args){
>     byte[] bytes = new byte[0];

Mind if I ask why you have a 0 byte buffer here? (Although perhaps it
wouldn't matter for what you're testing.)

>     ByteArrayInputStream bs = new ByteArrayInputStream(bytes);
>     InputStreamReader in = new InputStreamReader(bs);

I tried this:

      InputStreamReader in = new InputStreamReader(bs, "US-ASCII");

(Shift_JIS is the usual for me.) I had to declare the main method as "throws
UnsupportedEncodingException" to get it to compile, but the responded with
the changed encoding when run from the command line.

So it looks like it might be a Tomcat question? But I think I recall seeing
some answer on the list or in the docs. It may come down to having to
declare the encoding from your web pages, or read it from the headers the
browser sends or something.

Not much help, I suppose.

Joel Rees

>     System.out.println("file.encoding is " +
> System.getProperty("file.encoding"));
>     System.out.println("default encoding is " + in.getEncoding());
>   }
> }
>
> And no matter what I try, it always says "default encoding is ASCII", no
> matter what I've specified file.encoding as.  I'm invoking the class as:
>
> java -cp $CLASSPATH:. -Dfile.encoding=ISO8859_1 Encoding
>
> I've tried the following values for file.encoding with no changes:
>
> ISO8859_1
> ISO-8859-1
> 8859_1
> iso-8859-1
> iso8859-1
> iso8859_1
> en_US
> no_NO
> norwegian
>
> I've also tried every one of those values for LANG and LC_ALL in my
> environment variables.
>
> Please, someone help me! How do I change the damned default encoding????
> Why isn't it picking up on my environment variables or the file.encoding
> property? Running "java -version" tells me:
>
> java version "1.3.1_02"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_02-b02)
> Java HotSpot(TM) Client VM (build 1.3.1_02-b02, mixed mode)
>
> Can anyone help? I'm totally stymied here!
>
> Best regards,
>
> Richard




--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: changing the default character encoding

Posted by Richard Sand <rs...@vgalleries.com>.
Hi all,

I am still beating my head against a brick wall trying to get the default
encoding changed for my Tomcat server on Linux (slackware 2.4.17 if it
matters).

The problem is actually outside of Tomcat so please excuse the slightly
off-topic question.  But basically, I've run the following simple test:

import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
public class Encoding {
  public static void main(String[] args){
    byte[] bytes = new byte[0];
    ByteArrayInputStream bs = new ByteArrayInputStream(bytes);
    InputStreamReader in = new InputStreamReader(bs);
    System.out.println("file.encoding is " +
System.getProperty("file.encoding"));
    System.out.println("default encoding is " + in.getEncoding());
  }
}

And no matter what I try, it always says "default encoding is ASCII", no
matter what I've specified file.encoding as.  I'm invoking the class as:

java -cp $CLASSPATH:. -Dfile.encoding=ISO8859_1 Encoding

I've tried the following values for file.encoding with no changes:

ISO8859_1
ISO-8859-1
8859_1
iso-8859-1
iso8859-1
iso8859_1
en_US
no_NO
norwegian

I've also tried every one of those values for LANG and LC_ALL in my
environment variables.

Please, someone help me! How do I change the damned default encoding????
Why isn't it picking up on my environment variables or the file.encoding
property? Running "java -version" tells me:

java version "1.3.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_02-b02)
Java HotSpot(TM) Client VM (build 1.3.1_02-b02, mixed mode)

Can anyone help? I'm totally stymied here!

Best regards,

Richard




--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: changing the default character encoding

Posted by Martin Sandiford <Ma...@aus.viptone.com>.
Hi Richard,

If you specifically want the log/other files to be in ISO-8859-1
encoding, might it not be a good idea to create your OutputStreamWriters
(or whatever) with the encoding that you want, rather than rely on the
JDK doing the right thing via defaults?

I am not sure what is going on with your JDBC driver/database encoding.
I think this might be a separate problem from the file encoding issue.
Which database and JDBC drivers are you using?

Martin

"Richard Sand" <rs...@vgalleries.com> writes:
> Hi Andrew, Tomcat list-
>
> I tried setting file.encoding directly on the command-line for launching
> tomcat by doing
>
> export CATALINA_OPTS=-Dfile.encoding=iso-8859-1
>
> before I launched the catalina.sh script.  The catalina.sh script does:
>
>     $JAVA_HOME/bin/java $CATALINA_OPTS -classpath $CP \
>      -Dcatalina.base=$CATALINA_BASE \
>      -Dcatalina.home=$CATALINA_HOME \
>      org.apache.catalina.startup.Bootstrap "$@" start \
>      >> $CATALINA_BASE/logs/catalina.out 2>&1 &
>
> which seems to me properly will put CATALINA_OPTS onto the java command
> line.
>
> Of course, this had no effect, just like every other attempt I've made to
> fix this. :)  Is "file.encoding" exactly the property that needs to be set?
> Will this work?
>
> Thanks!
>
> -Richard
>
> ----- Original Message -----
> From: "Andrew B. Sudell" <as...@acm.org>
> To: "Richard Sand" <rs...@vgalleries.com>
> Sent: Monday, February 25, 2002 5:48 AM
> Subject: changing the default character encoding
>
>> Richard Sand writes:
>>  > Hi all-  Does anyone know how to specify the default encoding for the
> JVM in
>>  > a linux environment?
>>  >
>>  > I'm having difficulty with my servlets because the JVM on my system
> (sun
>>  > jdk1.3.1 on linux) is defaulting to us-ascii encoding instead of using
>>  > iso-8859-1.  I know I can override the encoding inside my code, but
>>  > according to the package description for java.lang, the JVM determines
> the
>>  > default encoding by looking at the OS.  I tried setting both LANG and
>>  > LC_CTYPE in my shell before launching catalina.sh, but I still couldn't
> get
>>  > the servlets to not default to US-ASCII.
>>  >
>>
>> Haven't tried this on Linux -- let me know it it dosen't work, I can
>> play around with a system at work, that isn't accessible just this
>> moment at home -- but in doing some experimentation on different
>> versions of Solaris and different JDK versions, the startup logic in
>> the JVM went something like
>>
>> - Examine the C local you are running in
>> - Find the character encoding for that locale
>> - map that to a Java encoding as best it can (exact scheme seemed to
>>   vary by JDK release)
>> - set file.encoding system property to that
>>
>> >From there, the Java implementations of String, InputStreamReader, and
>> OutputStreamWriter, base their default on file.encoding.
>>
>> So, try setting the LANG environment variable to someting that dosen't
>> use ASCII. Running 'locale -a' gives a list of choices.  Something
>> like en_US would be a good choice.  I expect you have LANG unset, and
>> are defaulting to either the POSIX or C locales, which are basically
>> the same local and imply ASCII only.  Running 'locale' with no
>> argument will give you an idea what the system is up to.  As you play
>> with setting LANG, you may want to write a short java program to print
>> the default encoding.
>>
>> If I remember when I get to work, I'll mail you a java program to
>> check the encoding, a C program to get the system charset -- assuming
>> it ports to linix quickly -- and a table of example results.
>>
>> Drew
>>
>> --
>>         Drew Sudell     asudell@acm.org      http://www.op.net/~asudell
>>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: changing the default character encoding

Posted by Richard Sand <rs...@vgalleries.com>.
Hi Andrew, Tomcat list-

I tried setting file.encoding directly on the command-line for launching
tomcat by doing

export CATALINA_OPTS=-Dfile.encoding=iso-8859-1

before I launched the catalina.sh script.  The catalina.sh script does:


    $JAVA_HOME/bin/java $CATALINA_OPTS -classpath $CP \
     -Dcatalina.base=$CATALINA_BASE \
     -Dcatalina.home=$CATALINA_HOME \
     org.apache.catalina.startup.Bootstrap "$@" start \
     >> $CATALINA_BASE/logs/catalina.out 2>&1 &

which seems to me properly will put CATALINA_OPTS onto the java command
line.

Of course, this had no effect, just like every other attempt I've made to
fix this. :)  Is "file.encoding" exactly the property that needs to be set?
Will this work?

Thanks!

-Richard

----- Original Message -----
From: "Andrew B. Sudell" <as...@acm.org>
To: "Richard Sand" <rs...@vgalleries.com>
Sent: Monday, February 25, 2002 5:48 AM
Subject: changing the default character encoding


> Richard Sand writes:
>  > Hi all-  Does anyone know how to specify the default encoding for the
JVM in
>  > a linux environment?
>  >
>  > I'm having difficulty with my servlets because the JVM on my system
(sun
>  > jdk1.3.1 on linux) is defaulting to us-ascii encoding instead of using
>  > iso-8859-1.  I know I can override the encoding inside my code, but
>  > according to the package description for java.lang, the JVM determines
the
>  > default encoding by looking at the OS.  I tried setting both LANG and
>  > LC_CTYPE in my shell before launching catalina.sh, but I still couldn't
get
>  > the servlets to not default to US-ASCII.
>  >
>
> Haven't tried this on Linux -- let me know it it dosen't work, I can
> play around with a system at work, that isn't accessible just this
> moment at home -- but in doing some experimentation on different
> versions of Solaris and different JDK versions, the startup logic in
> the JVM went something like
>
> - Examine the C local you are running in
> - Find the character encoding for that locale
> - map that to a Java encoding as best it can (exact scheme seemed to
>   vary by JDK release)
> - set file.encoding system property to that
>
> >From there, the Java implementations of String, InputStreamReader, and
> OutputStreamWriter, base their default on file.encoding.
>
> So, try setting the LANG environment variable to someting that dosen't
> use ASCII. Running 'locale -a' gives a list of choices.  Something
> like en_US would be a good choice.  I expect you have LANG unset, and
> are defaulting to either the POSIX or C locales, which are basically
> the same local and imply ASCII only.  Running 'locale' with no
> argument will give you an idea what the system is up to.  As you play
> with setting LANG, you may want to write a short java program to print
> the default encoding.
>
> If I remember when I get to work, I'll mail you a java program to
> check the encoding, a C program to get the system charset -- assuming
> it ports to linix quickly -- and a table of example results.
>
> Drew
>
> --
>         Drew Sudell     asudell@acm.org      http://www.op.net/~asudell
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


changing the default character encoding

Posted by Richard Sand <rs...@vgalleries.com>.
Hi all-  Does anyone know how to specify the default encoding for the JVM in
a linux environment?

I'm having difficulty with my servlets because the JVM on my system (sun
jdk1.3.1 on linux) is defaulting to us-ascii encoding instead of using
iso-8859-1.  I know I can override the encoding inside my code, but
according to the package description for java.lang, the JVM determines the
default encoding by looking at the OS.  I tried setting both LANG and
LC_CTYPE in my shell before launching catalina.sh, but I still couldn't get
the servlets to not default to US-ASCII.

Thanks!

Best regards,

Richard




--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>